-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.go
233 lines (204 loc) · 5.75 KB
/
merge.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package merger
import (
"fmt"
"reflect"
)
// Merge merges a and b together where b has precedence.
// It is not destructive on their parameters, but these must not be modified while
// merge is in progress.
func Merge(a interface{}, b interface{}) (interface{}, error) {
aKind := reflect.ValueOf(a).Kind()
bKind := reflect.ValueOf(b).Kind()
if kindContains(overwriteables, aKind) && kindContains(overwriteables, bKind) {
// Is overwriteable
return b, nil
} else if kindContains(mergeables, aKind) && kindContains(mergeables, bKind) {
// Is mergeable
if aKind != bKind {
return nil, &MergeError{
errType: ErrDiffKind,
errString: fmt.Sprintf(errDiffKindText, aKind, bKind),
}
}
if aKind == reflect.Array {
return arrayMerge(a, b)
} else if aKind == reflect.Slice {
return sliceMerge(a, b)
} else if aKind == reflect.Struct {
return structMerge(a, b)
} else if aKind == reflect.Map {
return mapMerge(a, b)
}
}
return nil, &MergeError{
errType: ErrMergeUnsupported,
errString: fmt.Sprintf(errMergeUnsupportedText, aKind, bKind),
}
}
// Field order is taken from a, additional fields of b are appended.
// No type assertion is made
// BUG(djboris): When merging structs fields are appended (A.Fields ∥ B.Fields).
// But field types should be taken from b as it has precedence.
func structMerge(a, b interface{}) (interface{}, error) {
aV := reflect.ValueOf(a)
bV := reflect.ValueOf(b)
var commonFields []reflect.StructField
// Add fields of A to commonFields
for i := 0; i < aV.NumField(); i++ {
x := aV.Type().Field(i)
commonFields = append(commonFields, x)
}
// Add fields of B to commonFields
for i := 0; i < bV.NumField(); i++ {
x := bV.Type().Field(i)
if !structFieldContains(commonFields, x.Name) {
commonFields = append(commonFields, x)
} else {
// TODO Overwrite type, according to BUG for this function
}
}
// Construct output struct
resType := reflect.StructOf(commonFields)
resValue := reflect.New(resType).Elem()
for i := range commonFields {
fieldName := commonFields[i].Name
fieldA := aV.FieldByName(fieldName)
fieldB := bV.FieldByName(fieldName)
if fieldA.IsValid() && fieldB.IsValid() {
// Field exists in A and B, do a merge
m, err := Merge(fieldA.Interface(), fieldB.Interface())
if err != nil {
return nil, err
}
resValue.FieldByName(fieldName).Set(reflect.ValueOf(m))
} else if fieldA.IsValid() {
// Field exists in A
resValue.FieldByName(fieldName).Set(fieldA)
} else if fieldB.IsValid() {
// Field exists in B
resValue.FieldByName(fieldName).Set(fieldB)
} else {
return nil, &MergeError{
errType: ErrInvalidFields,
errString: fmt.Sprintf(errInvalidFieldsText, fieldName),
}
}
}
return resValue.Interface(), nil
}
// arrayMerge returns effectively a slice
// Result: a ∥ b
// Type assertion only on Elem type
func arrayMerge(a, b interface{}) (interface{}, error) {
aV := reflect.ValueOf(a)
bV := reflect.ValueOf(b)
if aV.Type().Elem() != bV.Type().Elem() {
return nil, &MergeError{
errType: ErrDiffArrayTypes,
errString: fmt.Sprintf(errDiffArrayTypesText, aV.Type().Elem(), bV.Type().Elem()),
}
}
resType := reflect.ArrayOf(aV.Len()+bV.Len(), aV.Type().Elem())
resValue := reflect.New(resType).Elem()
for i := 0; i < aV.Len(); i++ {
resValue.Index(i).Set(aV.Index(i))
}
for i := 0; i < bV.Len(); i++ {
resValue.Index(aV.Len() + i).Set(bV.Index(i))
}
return resValue.Interface(), nil
}
// Result: a ∪ b
// Type assertion on indexing and value type
func mapMerge(a, b interface{}) (interface{}, error) {
aV := reflect.ValueOf(a)
bV := reflect.ValueOf(b)
if aV.Type().Key() != bV.Type().Key() {
return nil, &MergeError{
errType: ErrDiffMapKeyTypes,
errString: fmt.Sprintf(errDiffMapKeyTypesText, aV.Type().Key(), bV.Type().Key()),
}
}
if aV.Type().Elem() != bV.Type().Elem() {
return nil, &MergeError{
errType: ErrDiffMapValueTypes,
errString: fmt.Sprintf(errDiffMapValueTypesText, aV.Type().Elem(), bV.Type().Elem()),
}
}
res := reflect.MakeMap(aV.Type())
// Copy everything from a
for _, k := range aV.MapKeys() {
res.SetMapIndex(k, aV.MapIndex(k))
}
for _, k := range bV.MapKeys() {
if res.MapIndex(k).Kind() == reflect.Invalid {
// If key was not in a already, add it from b
res.SetMapIndex(k, bV.MapIndex(k))
} else {
// Need to merge both values
resVal := res.MapIndex(k).Interface()
bVal := bV.MapIndex(k).Interface()
m, err := Merge(resVal, bVal)
if err != nil {
return nil, err
}
res.SetMapIndex(k, reflect.ValueOf(m))
}
}
return res.Interface(), nil
}
// Type assertion only on Elem type
func sliceMerge(a, b interface{}) (interface{}, error) {
aV := reflect.ValueOf(a)
bV := reflect.ValueOf(b)
if aV.Type().Elem() != bV.Type().Elem() {
return nil, &MergeError{
errType: ErrDiffSliceTypes,
errString: fmt.Sprintf(errDiffSliceTypesText, aV.Type().Elem(), bV.Type().Elem()),
}
}
return reflect.AppendSlice(aV, bV).Interface(), nil
}
// Boolean, numeric and string types (overwrite)
var overwriteables = []reflect.Kind{
reflect.Bool,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr,
reflect.Float32,
reflect.Float64,
reflect.Complex64,
reflect.Complex128,
reflect.String,
}
// Array and slice types (concat) + map and struct types (union)
var mergeables = []reflect.Kind{
reflect.Array,
reflect.Map,
reflect.Slice,
reflect.Struct,
}
func structFieldContains(s []reflect.StructField, n string) bool {
for _, z := range s {
if z.Name == n {
return true
}
}
return false
}
func kindContains(s []reflect.Kind, k reflect.Kind) bool {
for _, z := range s {
if z == k {
return true
}
}
return false
}