-
Notifications
You must be signed in to change notification settings - Fork 0
/
synon.go
147 lines (140 loc) · 3.64 KB
/
synon.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
package synon
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"reflect"
)
func Merge(l, r interface{}) interface{} {
lvt := reflect.TypeOf(l)
rvt := reflect.TypeOf(r)
// Ensure both interfaces are of the same type
if lvt != rvt {
return nil
}
lv := reflect.ValueOf(l)
return merge(l, r, lv.Kind() == reflect.Pointer)
}
func merge(l, r interface{}, wasPointer bool) interface{} {
lvt := reflect.TypeOf(l)
rvt := reflect.TypeOf(r)
// Ensure both interfaces are of the same type
if lvt != rvt {
return nil
}
lv := reflect.ValueOf(l)
rv := reflect.ValueOf(r)
switch lv.Kind() {
case reflect.Map:
var keys []reflect.Value
keys = append(keys, lv.MapKeys()...)
keys = append(keys, rv.MapKeys()...)
for _, k := range keys {
if !rv.MapIndex(k).IsValid() {
continue
}
if !lv.MapIndex(k).IsValid() {
lv.SetMapIndex(k, rv.MapIndex(k))
continue
}
v := merge(lv.MapIndex(k).Interface(), rv.MapIndex(k).Interface(), false)
lv.SetMapIndex(k, reflect.ValueOf(v))
}
return lv.Interface()
case reflect.Slice:
if rv.IsNil() {
return lv.Interface()
}
values := reflect.AppendSlice(lv, rv)
newValues := reflect.MakeSlice(values.Type(), 0, values.Len())
var elements = reflect.MakeMap(reflect.TypeOf(map[interface{}]interface{}{}))
for i := 0; i < values.Len(); i++ {
key := values.Index(i)
if elements.MapIndex(key).IsValid() {
continue
}
elements.SetMapIndex(key, values.Index(i))
newValues = reflect.Append(newValues, values.Index(i))
}
return newValues.Interface()
case reflect.Chan:
if rv.Len() > 0 {
return rv.Interface()
}
return lv.Interface()
case reflect.Ptr:
if lv.IsNil() {
return rv.Interface()
}
if rv.IsNil() {
return lv.Interface()
}
lv = lv.Elem()
rv = rv.Elem()
return merge(lv.Interface(), rv.Interface(), true)
case reflect.Struct:
newStr := reflect.New(lv.Type()).Elem()
for i := 0; i < lv.NumField(); i++ {
vtl := lv.Type()
if !vtl.Field(i).IsExported() {
continue
}
ls := lv.Field(i)
rs := rv.Field(i)
v := reflect.ValueOf(merge(ls.Interface(), rs.Interface(), false))
if v.IsValid() {
newStr.Field(i).Set(v)
}
}
if wasPointer {
return newStr.Addr().Interface()
}
return newStr.Interface()
default:
if !rv.IsValid() && !lv.IsValid() {
return nil // Both values are not valid, return nil
}
if !rv.IsValid() && lv.IsValid() {
return lv.Interface() // Only lv is valid and not zero, return lv
}
if rv.IsValid() && !lv.IsValid() {
return rv.Interface() // Only rv is valid and not zero, return rv
}
// Both rv and lv are valid
if !isZero(rv) {
return rv.Interface() // rv is valid and not zero, return rv
}
if !isZero(lv) {
return lv.Interface() // lv is valid and not zero, return lv
}
return nil
}
}
func isZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.String:
return v.String() == ""
case reflect.Complex64, reflect.Complex128:
return v.Complex() == 0
case reflect.Array:
// Check for primitive.ObjectID
if v.Type() == reflect.TypeOf(primitive.ObjectID{}) {
zeroValue := reflect.Zero(v.Type()).Interface().(primitive.ObjectID)
currentValue := v.Interface().(primitive.ObjectID)
return compareObjectID(currentValue, zeroValue)
}
}
return false
}
func compareObjectID(a, b primitive.ObjectID) bool {
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}