-
Notifications
You must be signed in to change notification settings - Fork 21
/
setter.go
82 lines (74 loc) · 2.26 KB
/
setter.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package carta
import (
"errors"
"reflect"
)
func setDst(m *Mapper, dst reflect.Value, rsv *resolver) error {
// dst is always a pointer
dstIndirect := reflect.Indirect(dst)
// post order traversal, first set all submap structs, then the struct itself
for _, uid := range rsv.elementOrder {
elem := rsv.elements[uid]
//set childeren first
for fieldIndex, subMapRsv := range elem.subMaps {
var (
subMap *Mapper
childTyp reflect.Type
childDst reflect.Value
newChildElem reflect.Value
ok bool
)
if subMap, ok = m.SubMaps[fieldIndex]; !ok {
// this should never happen
return errors.New("carta: sub map not found")
}
if f, ok := m.SubMaps[fieldIndex]; ok {
childTyp = f.Typ
} else {
// this should never happen
return errors.New("carta: field not found")
}
if subMap.Crd == Collection {
capacity := len(subMapRsv.elements)
if subMap.IsTypePtr {
newChildElem = reflect.New(reflect.SliceOf(reflect.PtrTo(childTyp))).Elem()
newChildElem.Set(reflect.MakeSlice(reflect.SliceOf(reflect.PtrTo(childTyp)), 0, capacity))
} else {
newChildElem = reflect.New(reflect.SliceOf(childTyp)).Elem()
newChildElem.Set(reflect.MakeSlice(reflect.SliceOf(childTyp), 0, capacity))
}
if subMap.IsListPtr {
elem.v.Field(int(fieldIndex)).Set(newChildElem.Addr())
childDst = elem.v.Field(int(fieldIndex))
} else {
elem.v.Field(int(fieldIndex)).Set(newChildElem)
childDst = elem.v.Field(int(fieldIndex)).Addr()
}
} else if subMap.Crd == Association {
newChildElem = reflect.New(childTyp).Elem()
if subMap.IsTypePtr {
elem.v.Field(int(fieldIndex)).Set(newChildElem.Addr())
childDst = elem.v.Field(int(fieldIndex))
} else {
elem.v.Field(int(fieldIndex)).Set(newChildElem)
childDst = elem.v.Field(int(fieldIndex)).Addr()
}
}
// setting the child
setDst(subMap, childDst, subMapRsv)
}
}
for _, uid := range rsv.elementOrder {
elem := rsv.elements[uid]
if m.Crd == Collection {
if m.IsTypePtr {
dstIndirect.Set(reflect.Append(dstIndirect, elem.v.Addr()))
} else {
dstIndirect.Set(reflect.Append(dstIndirect, elem.v))
}
} else if m.Crd == Association {
dstIndirect.Set(elem.v)
}
}
return nil
}