This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
mfunc.go
179 lines (145 loc) · 5.33 KB
/
mfunc.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
package conjungo
import (
"fmt"
"reflect"
"github.com/sirupsen/logrus"
)
// A MergeFunc defines how two items are merged together. It should accept a reflect.Value
// representation of a target and source, and return the final merged product.
// The value returned from the function will be written directly to the parent value,
// as long as there is no error.
// Options are also passed in, and it is the responsibility of the function to honor
// these options and handle any variations in behavior that should occur.
type MergeFunc func(target, source reflect.Value, o *Options) (reflect.Value, error)
type funcSelector struct {
typeFuncs map[reflect.Type]MergeFunc
kindFuncs map[reflect.Kind]MergeFunc
defaultFunc MergeFunc
}
func newFuncSelector() *funcSelector {
return &funcSelector{
typeFuncs: map[reflect.Type]MergeFunc{},
kindFuncs: map[reflect.Kind]MergeFunc{
reflect.Map: mergeMap,
reflect.Slice: mergeSlice,
reflect.Struct: mergeStruct,
},
defaultFunc: defaultMergeFunc,
}
}
func (f *funcSelector) setTypeMergeFunc(t reflect.Type, mf MergeFunc) {
if nil == f.typeFuncs {
f.typeFuncs = map[reflect.Type]MergeFunc{}
}
f.typeFuncs[t] = mf
}
func (f *funcSelector) setKindMergeFunc(k reflect.Kind, mf MergeFunc) {
if nil == f.kindFuncs {
f.kindFuncs = map[reflect.Kind]MergeFunc{}
}
f.kindFuncs[k] = mf
}
func (f *funcSelector) setDefaultMergeFunc(mf MergeFunc) {
f.defaultFunc = mf
}
// Get func must always return a function.
// First looks for a merge func defined for its type. Type is the most specific way to categorize something,
// for example, struct type foo of package bar or map[string]string. Next it looks for a merge func defined for its
// kind, for example, struct or map. At this point, if nothing matches, it will fall back to the default merge definition.
func (f *funcSelector) getFunc(v reflect.Value) MergeFunc {
// prioritize a specific 'type' definition
ti := v.Type()
if fx, ok := f.typeFuncs[ti]; ok {
return fx
}
// then look for a more general 'kind'.
if fx, ok := f.kindFuncs[ti.Kind()]; ok {
return fx
}
if f.defaultFunc != nil {
return f.defaultFunc
}
return defaultMergeFunc
}
// The most basic merge function to be used as default behavior.
// In overwrite mode, it returns the source. Otherwise, it returns the target.
func defaultMergeFunc(t, s reflect.Value, o *Options) (reflect.Value, error) {
if o.Overwrite {
return s, nil
}
return t, nil
}
func mergeMap(t, s reflect.Value, o *Options) (v reflect.Value, err error) {
if t.Kind() != reflect.Map || s.Kind() != reflect.Map {
return reflect.Value{}, fmt.Errorf("got non-map type (tagret: %v; source: %v)", t.Kind(), s.Kind())
}
keys := s.MapKeys()
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("failed to merge map: %v", r)
}
}()
for _, k := range keys {
logrus.Debugf("MERGE T<>S '%s' :: %v <> %v", k, t.MapIndex(k), s.MapIndex(k))
val, err := merge(t.MapIndex(k), s.MapIndex(k), o)
if err != nil {
return reflect.Value{}, fmt.Errorf("key '%s': %v", k, err)
}
t.SetMapIndex(k, val)
}
v = t
return
}
// Merges two slices of the same type by appending source to target.
func mergeSlice(t, s reflect.Value, o *Options) (reflect.Value, error) {
if t.Type() != s.Type() {
return reflect.Value{}, fmt.Errorf("slices must have same type: T: %v S: %v", t.Type(), s.Type())
}
return reflect.AppendSlice(t, s), nil
}
// This func is designed to be called by merge().
// It should not be used on its own because it will panic.
func mergeStruct(t, s reflect.Value, o *Options) (reflect.Value, error) {
// accept pointer values, but dereference them
valT := reflect.Indirect(t)
valS := reflect.Indirect(s)
kindT := valT.Kind()
kindS := valS.Kind()
newT := reflect.New(valT.Type()).Elem() //a new instance of the struct type that can be set
okT := kindT == reflect.Struct
okS := kindS == reflect.Struct
if !okT || !okS {
return reflect.Value{}, fmt.Errorf("got non-struct kind (tagret: %v; source: %v)", kindT, kindS)
}
for i := 0; i < valS.NumField(); i++ {
fieldT := newT.Field(i)
logrus.Debugf("merging struct field %s", fieldT)
// field is addressable because it's created above. So this means it is unexported.
if !fieldT.CanSet() {
if o.ErrorOnUnexported {
return reflect.Value{}, fmt.Errorf("struct of type %v has unexported field: %s",
t.Type().Name(), newT.Type().Field(i).Name)
}
// revert to using the default func instead to treat the struct as single entity
return defaultMergeFunc(t, s, o)
}
//fieldT should always be valid because it's created above
merged, err := merge(valT.Field(i), valS.Field(i), o)
if err != nil {
return reflect.Value{}, fmt.Errorf("failed to merge field `%s.%s`: %v",
newT.Type().Name(), newT.Type().Field(i).Name, err)
}
if !merged.IsValid() {
logrus.Warnf("merged value is invalid for field %s. Falling back to default merge: %v <> %v",
newT.Type().Field(i).Name, valT.Field(i), valS.Field(i))
// if merge returned an invalid value, fallback to a default merge for the field
// defaultMergeFun() does not error
merged, _ = defaultMergeFunc(valT.Field(i), valS.Field(i), o)
}
if fieldT.Kind() != reflect.Interface && fieldT.Type() != merged.Type() {
return reflect.Value{}, fmt.Errorf("types dont match %v <> %v", fieldT.Type(), merged.Type())
}
fieldT.Set(merged)
}
return newT, nil
}