-
Notifications
You must be signed in to change notification settings - Fork 1
/
copier.go
342 lines (301 loc) · 7.74 KB
/
copier.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package xgo
import (
"errors"
"fmt"
"reflect"
"time"
)
// tagCopier is tag for deep copy target
const tagCopier = "copier"
type SetCustomField func(src, dst reflect.Value) (bool, error)
// DeepCopy
func DeepCopy(srcModel interface{}, dstModel interface{}) error {
f := func(src, dst reflect.Value) (bool, error) { return false, nil }
return DeepCopyWithCustomSetter(srcModel, dstModel, f)
}
// DeepCopy with custom setter
func DeepCopyWithCustomSetter(
srcModel interface{},
dstModel interface{},
customSetter SetCustomField,
) error {
src := reflect.Indirect(reflect.ValueOf(srcModel))
dst := reflect.Indirect(reflect.ValueOf(dstModel))
if !dst.CanAddr() {
return errors.New("copy to value is unaddressable")
}
if src.Kind() == reflect.Slice {
if err := copySlice(src, dst); err != nil {
return fmt.Errorf("%v", err)
}
return nil
}
// What to do if the deepcopy destination model has a tag
var srcToDstTagMap = map[string]string{}
for i := 0; i < dst.NumField(); i++ {
dstF := dst.Type().Field(i)
if tag, ok := dstF.Tag.Lookup(tagCopier); ok {
srcToDstTagMap[tag] = dstF.Name
}
}
for i := 0; i < src.NumField(); i++ {
field := src.Type().Field(i)
srcFieldValue := src.FieldByName(field.Name)
dstFieldName := field.Name
if tag, ok := field.Tag.Lookup(tagCopier); ok {
dstFieldName = tag
}
if tag, ok := srcToDstTagMap[field.Name]; ok {
dstFieldName = tag
}
_, ok := dst.Type().FieldByName(dstFieldName)
dstFieldValue := dst.FieldByName(dstFieldName)
if !ok {
continue
}
// Ignores private field
if !IsFirstUpper(dstFieldName) {
continue
}
isSet, err := convert(srcFieldValue, dstFieldValue)
if err != nil {
return fmt.Errorf("%s: %v", field.Name, err)
}
if isSet {
continue
}
isSet, err = customSetter(srcFieldValue, dstFieldValue)
if err != nil {
return fmt.Errorf("%s: %v", field.Name, err)
}
if isSet {
continue
}
// set the time.Time field
isSet, err = setTimeField(srcFieldValue, dstFieldValue)
if err != nil {
return fmt.Errorf("%s: %v", field.Name, err)
}
if isSet {
continue
}
// struct, pointer, slice
switch srcFieldValue.Kind() {
case reflect.Struct:
if !field.Anonymous {
dv, vFunc := instantiate(dstFieldValue)
if err := DeepCopy(srcFieldValue.Interface(), dv.Interface()); err != nil {
return fmt.Errorf("%s: %v", field.Name, err)
}
dstFieldValue.Set(vFunc())
continue
}
dstFieldValue.SetInt(srcFieldValue.Int())
case reflect.Ptr:
if srcFieldValue.IsNil() {
continue
}
// copy to indirect
indirect := reflect.Indirect(srcFieldValue)
if indirect.Type().AssignableTo(dstFieldValue.Type()) && dstFieldValue.Type().Kind() != reflect.Ptr {
dstFieldValue.Set(indirect)
continue
}
dv, vFunc := instantiate(dstFieldValue)
if err := DeepCopy(srcFieldValue.Interface(), dv.Interface()); err != nil {
return fmt.Errorf("%s: %v", field.Name, err)
}
dstFieldValue.Set(vFunc())
continue
case reflect.Slice:
if err := copySlice(srcFieldValue, dstFieldValue); err != nil {
return fmt.Errorf("%s: %v", field.Name, err)
}
continue
}
}
return nil
}
// Instantiates a value that can handle copying in both directions - from a pointer to a struct and from a struct to a pointer.
func instantiate(v reflect.Value) (reflect.Value, func() reflect.Value) {
// ptr
if v.Type().Kind() == reflect.Ptr {
rv := reflect.New(v.Type().Elem())
vFunc := func() reflect.Value { return rv }
return rv, vFunc
}
// struct
rv := reflect.New(v.Type())
vFunc := func() reflect.Value { return reflect.Indirect(rv) }
return rv, vFunc
}
func convert(src, dst reflect.Value) (bool, error) {
// the source and destination types are the same
if src.Type().ConvertibleTo(dst.Type()) {
dst.Set(src.Convert(dst.Type()))
return true, nil
}
// from pointer type to non pointer type
if src.Type().Kind() == reflect.Ptr {
if src.IsNil() {
return true, nil
}
if src.Type().Elem().ConvertibleTo(dst.Type()) {
dst.Set(src.Elem().Convert(dst.Type()))
return true, nil
}
if dst.Type().Kind() == reflect.Ptr {
if src.Type().Elem().ConvertibleTo(dst.Type().Elem()) {
rv := reflect.New(dst.Type().Elem())
rv.Elem().Set(src.Elem().Convert(dst.Type().Elem()))
dst.Set(rv)
return true, nil
}
}
}
// from non pointer type to pointer type
if dst.Type().Kind() == reflect.Ptr {
if src.Type().ConvertibleTo(dst.Type().Elem()) {
rv := reflect.New(dst.Type().Elem())
rv.Elem().Set(src.Convert(dst.Type().Elem()))
dst.Set(rv)
return true, nil
}
}
return false, nil
}
func copySlice(src, dst reflect.Value) error {
if src.IsNil() {
return nil
}
slice := reflect.MakeSlice(reflect.SliceOf(dst.Type().Elem()), src.Len(), src.Cap())
dst.Set(slice)
for i := 0; i < src.Len(); i++ {
d := dst.Index(i)
isSet, err := convert(src.Index(i), d)
if err != nil {
return err
}
if isSet {
continue
}
// pointer or struct
dv, vFunc := instantiate(d)
if err := DeepCopy(src.Index(i).Interface(), dv.Interface()); err != nil {
return err
}
d.Set(vFunc())
}
return nil
}
func setTimeField(src, dst reflect.Value) (bool, error) {
const format = time.RFC3339Nano
switch t := src.Interface().(type) {
case time.Time:
// time.Time -> int64
switch dst.Interface().(type) {
case int64:
dst.Set(reflect.ValueOf(t.UnixNano()))
return true, nil
case *int64:
rv := reflect.New(dst.Type().Elem())
rv.Elem().Set(reflect.ValueOf(t.UnixNano()))
dst.Set(rv)
return true, nil
case string:
dst.Set(reflect.ValueOf(t.Format(format)))
return true, nil
case *string:
rv := reflect.New(dst.Type().Elem())
rv.Elem().Set(reflect.ValueOf(t.Format(format)))
dst.Set(rv)
return true, nil
}
case *time.Time:
if t == nil {
return true, nil
}
// *time.Time -> int64 or *int64
switch dst.Interface().(type) {
case int64:
dst.Set(reflect.ValueOf(t.UnixNano()))
return true, nil
case *int64:
rv := reflect.New(dst.Type().Elem())
rv.Elem().Set(reflect.ValueOf(t.UnixNano()))
dst.Set(rv)
return true, nil
case string:
dst.Set(reflect.ValueOf(t.Format(format)))
return true, nil
case *string:
rv := reflect.New(dst.Type().Elem())
rv.Elem().Set(reflect.ValueOf(t.Format(format)))
dst.Set(rv)
return true, nil
}
case int64:
// int64 -> time.Time or *time.Time
switch dst.Interface().(type) {
case time.Time:
dst.Set(reflect.ValueOf(time.Unix(0, t)))
return true, nil
case *time.Time:
rv := reflect.New(dst.Type().Elem())
rv.Elem().Set(reflect.ValueOf(time.Unix(0, t)))
dst.Set(rv)
return true, nil
}
case *int64:
if t == nil {
return true, nil
}
// *int64 -> time.Time or *time.Time
switch dst.Interface().(type) {
case time.Time:
reflect.Indirect(dst).Set(reflect.ValueOf(time.Unix(0, *t)))
return true, nil
case *time.Time:
rv := reflect.New(dst.Type().Elem())
rv.Elem().Set(reflect.ValueOf(time.Unix(0, *t)))
dst.Set(rv)
return true, nil
}
case string:
// string -> time.Time or *time.Time
v, err := time.Parse(format, t)
if err != nil {
return true, nil
}
switch dst.Interface().(type) {
case time.Time:
dst.Set(reflect.ValueOf(v))
return true, nil
case *time.Time:
rv := reflect.New(dst.Type().Elem())
rv.Elem().Set(reflect.ValueOf(v))
dst.Set(rv)
return true, nil
}
case *string:
if t == nil {
return true, nil
}
// *string -> time.Time or *time.Time
v, err := time.Parse(format, *t)
if err != nil {
return true, nil
}
switch dst.Interface().(type) {
case time.Time:
reflect.Indirect(dst).Set(reflect.ValueOf(v))
return true, nil
case *time.Time:
rv := reflect.New(dst.Type().Elem())
rv.Elem().Set(reflect.ValueOf(v))
dst.Set(rv)
return true, nil
}
}
return false, nil
}