-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap.go
More file actions
55 lines (44 loc) · 1.35 KB
/
map.go
File metadata and controls
55 lines (44 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package remapper
import (
"reflect"
)
type MapMapper mapperType
func newMapMapper(dataType reflect.Type, normalizedType reflect.Type, names []string) (mapperType) {
m := MapMapper{
fields: map[string]*mapperField{},
dataType: dataType,
normalizedType: normalizedType,
}
for fieldIndex, fieldName := range names {
m.fields[NameMapper(fieldName)] = &mapperField{
id: fieldIndex,
convert: ValueConverter,
reverseId: -1,
}
}
m.mapperTypeI = &m
return mapperType(m)
}
// creates a new instance of map of required type.
func (m *MapMapper) create() (reflect.Value, error) {
return reflect.MakeMap(m.dataType), nil
}
// sets a value to a map at index with name
func (m *MapMapper) set(to reflect.Value, i int, name string, value reflect.Value) {
name = NameMapper(name)
if _, ok := m.fields[name]; ok {
to.SetMapIndex(reflect.ValueOf(name), value)
}
}
// gets a value from a map at index with name
func (m *MapMapper) get(from reflect.Value, i int, name string) (reflect.Value) {
name = NameMapper(name)
if _, ok := m.fields[name]; ok {
v := from.MapIndex(reflect.ValueOf(name))
if !v.IsValid() {
v = reflect.Zero(m.dataType.Elem())
}
return v
}
return reflect.Value{}
}