-
Notifications
You must be signed in to change notification settings - Fork 2
/
group.go
73 lines (70 loc) · 1.9 KB
/
group.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
package m2obj
// GroupForeach **!!! ONLY FOR GROUP OBJECT**
//
// Loop for all key-value pairs in the group, foreach calls `do`.
//
// Stops when do returns a non-nil err and return it.
func (o *Object) GroupForeach(do func(key string, obj *Object) error) (err error) {
switch o.val.(type) {
case *groupData:
for k, obj := range *o.val.(*groupData) {
if err = do(k, obj); err != nil {
break
}
}
o.buildParentLink(o.parent)
default:
panic(invalidTypeErr(""))
}
return
}
// GroupMerge **!!! ONLY FOR GROUP OBJECT**
//
// Merges two GROUP Object recursively. All already exists array and value objects in o will be replaced (forced == true and there is a key with the same name exists in o2) or reserved (forced == false), other object in o2 will be added into o.
func (o *Object) GroupMerge(o2 *Object, forced bool) (err error) {
return o.groupMerge(o2, forced, true)
}
func (o *Object) groupMerge(o2 *Object, forced bool, needCallOnChange bool) (err error) {
switch o.val.(type) {
case *groupData: // Group
switch o2.val.(type) {
case *groupData: // Group
newObj := o.Clone()
err = o2.GroupForeach(func(key string, o2obj *Object) error {
if newObj.Has(key) {
o1obj := newObj.MustGet(key)
// o1obj type check
switch o1obj.val.(type) {
case *groupData:
// o2obj type check
switch o2obj.val.(type) {
case *groupData:
// Merge two sub group
return o1obj.GroupMerge(o2obj, forced)
default:
if forced {
return newObj.Set(key, o2obj)
}
}
default:
if forced {
return newObj.Set(key, o2obj)
}
}
} else {
return newObj.Set(key, o2obj)
}
return nil
})
if err == nil {
o.setVal(newObj, needCallOnChange)
o.buildParentLink(o.parent)
}
return
default: // Array or Value
return invalidTypeErr("")
}
default: // Array or Value
return invalidTypeErr("")
}
}