-
Notifications
You must be signed in to change notification settings - Fork 119
/
merge.go
191 lines (182 loc) · 5.91 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
/*
* Copyright IBM Corporation 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package deepcopy
import (
"reflect"
"github.com/sirupsen/logrus"
)
// MergeInterface can be implemented by types to have custom deep copy logic.
type MergeInterface interface {
Merge(interface{}) interface{}
}
// Merge returns a merge of x and y.
// Supports everything except Chan, Func and UnsafePointer.
func Merge(x interface{}, y interface{}) interface{} {
return mergeRecursively(reflect.ValueOf(DeepCopy(x)), reflect.ValueOf(DeepCopy(y))).Interface()
}
func mergeRecursively(xV reflect.Value, yV reflect.Value) reflect.Value {
if !yV.IsValid() {
logrus.Debugf("invalid value given to merge recursively value: %+v", yV)
return xV
}
if !xV.IsValid() {
logrus.Debugf("invalid value given to merge recursively value: %+v", xV)
return yV
}
xT := xV.Type()
xK := xV.Kind()
yK := yV.Kind()
if xK != yK {
logrus.Debugf("Unable to merge due to varying types : %s & %s", xK, yK)
return yV
}
if xV.CanInterface() {
if mergable, ok := xV.Interface().(MergeInterface); ok {
nV := reflect.ValueOf(mergable.Merge(yV.Interface()))
if !nV.IsValid() {
return yV
}
return nV
}
}
switch xK {
case 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:
return yV
case reflect.Array, reflect.Slice:
{
// make a copy of xV
mergedSlice := copyRecursively(xV)
// mergedSlice := reflect.MakeSlice(xT, xV.Len(), xV.Cap())
// for xIdx := 0; xIdx < xV.Len(); xIdx++ {
// mergedSlice.Index(xIdx).Set(copyRecursively(xV.Index(xIdx)))
// }
// for each element in yV
for yIdx := 0; yIdx < yV.Len(); yIdx++ {
merged := false
yVElem := yV.Index(yIdx)
// try to merge with an existing element in the merged slice
for mergedIdx := 0; mergedIdx < mergedSlice.Len(); mergedIdx++ {
existingElem := mergedSlice.Index(mergedIdx)
if !compare(existingElem, yVElem) {
// can't merge
continue
}
mergedElem := mergeRecursively(existingElem, yVElem)
mergedSlice.Index(mergedIdx).Set(mergedElem)
merged = true
break
}
if !merged {
// append if we can't merge
mergedSlice = reflect.Append(mergedSlice, copyRecursively(yVElem))
}
}
return mergedSlice
}
case reflect.Interface:
return mergeRecursively(xV.Elem(), yV.Elem())
case reflect.Map:
nV := reflect.MakeMapWithSize(xT, xV.Len()+yV.Len())
for _, key := range xV.MapKeys() {
if yV.MapIndex(key) == reflect.Zero(reflect.TypeOf(xV)).Interface() {
nV.SetMapIndex(copyRecursively(key), copyRecursively(xV.MapIndex(key)))
continue
}
nV.SetMapIndex(copyRecursively(key), mergeRecursively(xV.MapIndex(key), yV.MapIndex(key)))
}
for _, key := range yV.MapKeys() {
if xV.MapIndex(key) == reflect.Zero(reflect.TypeOf(yV)).Interface() {
nV.SetMapIndex(copyRecursively(key), copyRecursively(yV.MapIndex(key)))
continue
}
nV.SetMapIndex(copyRecursively(key), mergeRecursively(xV.MapIndex(key), yV.MapIndex(key)))
}
return nV
case reflect.Ptr:
if xV.IsNil() {
return xV
}
nV := reflect.New(xV.Elem().Type())
nV.Elem().Set(mergeRecursively(xV.Elem(), yV.Elem()))
return yV
case reflect.Struct:
nV := reflect.New(xT).Elem()
for i := 0; i < xV.NumField(); i++ {
if !nV.Field(i).CanSet() {
continue
}
nV.Field(i).Set(mergeRecursively(xV.Field(i), yV.Field(i)))
}
return nV
default:
logrus.Debugf("unsupported for deep copy kind: %+v type: %+v value: %+v", xK, xT, xV)
return xV
}
}
// CompareInterface can be implemented by types to have custom deep copy logic.
type CompareInterface interface {
Compare(interface{}, interface{}) bool
}
func compare(xV reflect.Value, yV reflect.Value) bool {
if !yV.IsValid() {
logrus.Debugf("invalid value given to merge recursively value: %+v", yV)
return false
}
if !xV.IsValid() {
logrus.Debugf("invalid value given to merge recursively value: %+v", xV)
return false
}
xT := xV.Type()
xK := xV.Kind()
yK := yV.Kind()
if xK != yK {
logrus.Debugf("Unable to merge due to varying types : %s & %s", xK, yK)
return false
}
if xV.CanInterface() {
if comparable, ok := xV.Interface().(CompareInterface); ok {
return comparable.Compare(xV.Interface(), yV.Interface())
}
}
switch xK {
case 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:
return xV == yV
case reflect.Array:
// TODO: Check this is the required behaviour
return true
case reflect.Slice:
// TODO: Check this is the required behaviour
return true
case reflect.Interface:
return compare(xV.Elem(), yV.Elem())
case reflect.Map:
// TODO: Check this is the required behaviour
return true
case reflect.Ptr:
return compare(xV.Elem(), yV.Elem())
case reflect.Struct:
nV := reflect.New(xT).Elem()
for i := 0; i < xV.NumField(); i++ {
if nV.Type().Field(i).Name == "name" || nV.Type().Field(i).Name == "id" {
return compare(xV.Field(i), yV.Field(i))
}
}
return true
default:
logrus.Debugf("unsupported for deep compare kind: %+v type: %+v value: %+v", xK, xT, xV)
return true
}
}