-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_copier.go
377 lines (343 loc) · 11.9 KB
/
struct_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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package deepcopy
import (
"fmt"
"reflect"
"strings"
"unsafe"
)
var (
errType = reflect.TypeOf((*error)(nil)).Elem()
)
// structCopier data structure of copier that copies from a `struct`
type structCopier struct {
ctx *Context
fieldCopiers []copier
}
// Copy implementation of Copy function for struct copier
func (c *structCopier) Copy(dst, src reflect.Value) error {
for _, cp := range c.fieldCopiers {
if err := cp.Copy(dst, src); err != nil {
return err
}
}
return nil
}
//nolint:gocognit,gocyclo
func (c *structCopier) init(dstType, srcType reflect.Type) (err error) {
cacheKey := c.ctx.createCacheKey(dstType, srcType)
c.ctx.mu.RLock()
cp, ok := c.ctx.copierCacheMap[*cacheKey]
c.ctx.mu.RUnlock()
if ok {
c.fieldCopiers = cp.(*structCopier).fieldCopiers //nolint:forcetypeassert
return nil
}
var dstCopyingMethods map[string]*reflect.Method
if c.ctx.CopyBetweenStructFieldAndMethod {
dstCopyingMethods = c.parseCopyingMethods(dstType)
}
dstDirectFields, mapDstDirectFields, dstInheritedFields, mapDstInheritedFields := c.parseAllFields(dstType)
srcDirectFields, mapSrcDirectFields, srcInheritedFields, mapSrcInheritedFields := c.parseAllFields(srcType)
c.fieldCopiers = make([]copier, 0, len(dstDirectFields)+len(dstInheritedFields))
for _, key := range append(srcDirectFields, srcInheritedFields...) {
// Find field details from `src` having the key
sfDetail := mapSrcDirectFields[key]
if sfDetail == nil {
sfDetail = mapSrcInheritedFields[key]
}
if sfDetail == nil || sfDetail.ignored || sfDetail.done {
continue
}
// Copying methods have higher priority, so if a method defined in the dst struct, use it
if dstCopyingMethods != nil {
methodName := "Copy" + strings.ToUpper(key[:1]) + key[1:]
dstCpMethod, exists := dstCopyingMethods[methodName]
if exists && !dstCpMethod.Type.In(1).AssignableTo(sfDetail.field.Type) {
return fmt.Errorf("%w: struct method '%v.%s' does not accept argument type '%v' from '%v[%s]'",
ErrMethodInvalid, dstType, dstCpMethod.Name, sfDetail.field.Type, srcType, sfDetail.field.Name)
}
if exists {
c.fieldCopiers = append(c.fieldCopiers, c.createField2MethodCopier(dstCpMethod, sfDetail))
sfDetail.markDone()
continue
}
}
// Find field details from `dst` having the key
dfDetail := mapDstDirectFields[key]
if dfDetail == nil {
dfDetail = mapDstInheritedFields[key]
}
if dfDetail == nil || dfDetail.ignored || dfDetail.done {
// Found no corresponding dest field to copy to, raise an error in case this is required
if sfDetail.required {
return fmt.Errorf("%w: struct field '%v[%s]' requires copying",
ErrFieldRequireCopying, srcType, sfDetail.field.Name)
}
continue
}
copier, err := c.buildCopier(dstType, srcType, dfDetail, sfDetail)
if err != nil {
return err
}
c.fieldCopiers = append(c.fieldCopiers, copier)
dfDetail.markDone()
sfDetail.markDone()
}
// Remaining dst fields can't be copied
for _, dfDetail := range mapDstDirectFields {
if !dfDetail.done && dfDetail.required {
return fmt.Errorf("%w: struct field '%v[%s]' requires copying",
ErrFieldRequireCopying, dstType, dfDetail.field.Name)
}
}
for _, dfDetail := range mapDstInheritedFields {
if !dfDetail.done && dfDetail.required {
return fmt.Errorf("%w: struct field '%v[%s]' requires copying",
ErrFieldRequireCopying, dstType, dfDetail.field.Name)
}
}
c.ctx.mu.Lock()
c.ctx.copierCacheMap[*cacheKey] = c
c.ctx.mu.Unlock()
return nil
}
// parseCopyingMethods collects all copying methods from the given struct type
func (c *structCopier) parseCopyingMethods(structType reflect.Type) map[string]*reflect.Method {
ptrType := reflect.PointerTo(structType)
numMethods := ptrType.NumMethod()
result := make(map[string]*reflect.Method, numMethods)
for i := 0; i < numMethods; i++ {
method := ptrType.Method(i)
// Method name must be something like `Copy<something>`
if !strings.HasPrefix(method.Name, "Copy") {
continue
}
// Method must accept an arg and return error type (1st arg is the struct itself)
if method.Type.NumIn() != 2 || method.Type.NumOut() != 1 {
continue
}
if method.Type.Out(0) != errType {
continue
}
result[method.Name] = &method
}
return result
}
// parseAllFields parses all fields of a struct including direct fields and fields inherited from embedded structs
func (c *structCopier) parseAllFields(typ reflect.Type) (
directFieldKeys []string,
mapDirectFields map[string]*fieldDetail,
inheritedFieldKeys []string,
mapInheritedFields map[string]*fieldDetail,
) {
numFields := typ.NumField()
directFieldKeys = make([]string, 0, numFields)
mapDirectFields = make(map[string]*fieldDetail, numFields)
inheritedFieldKeys = make([]string, 0, numFields)
mapInheritedFields = make(map[string]*fieldDetail, numFields)
for i := 0; i < numFields; i++ {
sf := typ.Field(i)
fDetail := &fieldDetail{field: &sf, index: []int{i}}
parseTag(fDetail)
if fDetail.ignored {
continue
}
directFieldKeys = append(directFieldKeys, fDetail.key)
mapDirectFields[fDetail.key] = fDetail
// Parse embedded struct to get its fields
if sf.Anonymous {
for key, detail := range c.parseAllNestedFields(sf.Type, fDetail.index) {
inheritedFieldKeys = append(inheritedFieldKeys, key)
mapInheritedFields[key] = detail
fDetail.nestedFields = append(fDetail.nestedFields, detail)
}
}
}
return directFieldKeys, mapDirectFields, inheritedFieldKeys, mapInheritedFields
}
// parseAllNestedFields parses all fields with initial index of starting field
func (c *structCopier) parseAllNestedFields(typ reflect.Type, index []int) map[string]*fieldDetail {
if typ.Kind() == reflect.Pointer {
typ = typ.Elem()
}
if typ.Kind() != reflect.Struct {
return nil
}
numFields := typ.NumField()
result := make(map[string]*fieldDetail, numFields)
for i := 0; i < numFields; i++ {
sf := typ.Field(i)
fDetail := &fieldDetail{field: &sf, index: append(index, i)}
parseTag(fDetail)
if fDetail.ignored {
continue
}
result[fDetail.key] = fDetail
// Parse embedded struct recursively to get its fields
if sf.Anonymous {
for key, detail := range c.parseAllNestedFields(sf.Type, fDetail.index) {
result[key] = detail
fDetail.nestedFields = append(fDetail.nestedFields, detail)
}
}
}
return result
}
func (c *structCopier) buildCopier(dstType, srcType reflect.Type, dstDetail, srcDetail *fieldDetail) (copier, error) {
df, sf := dstDetail.field, srcDetail.field
// OPTIMIZATION: buildCopier() can handle this nicely, but it will add another wrapping layer
if simpleKindMask&(1<<sf.Type.Kind()) > 0 {
if sf.Type == df.Type {
// NOTE: pass nil to unset custom copier and trigger direct copying.
// We can pass `&directCopier{}` for the same result (but it's a bit slower).
return c.createField2FieldCopier(dstDetail, srcDetail, nil), nil
}
if sf.Type.ConvertibleTo(df.Type) {
return c.createField2FieldCopier(dstDetail, srcDetail, &convCopier{}), nil
}
}
cp, err := buildCopier(c.ctx, df.Type, sf.Type)
if err != nil {
return nil, err
}
if c.ctx.IgnoreNonCopyableTypes && (srcDetail.required || dstDetail.required) {
_, isNopCopier := cp.(*nopCopier)
if isNopCopier && dstDetail.required {
return nil, fmt.Errorf("%w: struct field '%v[%s]' requires copying",
ErrFieldRequireCopying, dstType, dstDetail.field.Name)
}
if isNopCopier && srcDetail.required {
return nil, fmt.Errorf("%w: struct field '%v[%s]' requires copying",
ErrFieldRequireCopying, srcType, srcDetail.field.Name)
}
}
return c.createField2FieldCopier(dstDetail, srcDetail, cp), nil
}
func (c *structCopier) createField2MethodCopier(dM *reflect.Method, sfDetail *fieldDetail) copier {
return &structField2MethodCopier{
dstMethod: dM.Index,
dstMethodUnexported: !dM.IsExported(),
srcFieldIndex: sfDetail.index,
srcFieldUnexported: !sfDetail.field.IsExported(),
}
}
func (c *structCopier) createField2FieldCopier(df, sf *fieldDetail, cp copier) copier {
return &structField2FieldCopier{
copier: cp,
dstFieldIndex: df.index,
dstFieldUnexported: !df.field.IsExported(),
srcFieldIndex: sf.index,
srcFieldUnexported: !sf.field.IsExported(),
}
}
// structFieldDirectCopier data structure of copier that copies from
// a src field to a dst field directly
type structField2FieldCopier struct {
copier copier
dstFieldIndex []int
dstFieldUnexported bool
srcFieldIndex []int
srcFieldUnexported bool
}
// Copy implementation of Copy function for struct field copier direct
func (c *structField2FieldCopier) Copy(dst, src reflect.Value) (err error) {
if len(c.srcFieldIndex) == 1 {
src = src.Field(c.srcFieldIndex[0])
} else {
// NOTE: When a struct pointer is embedded (e.g. type StructX struct { *BaseStruct }),
// this retrieval can fail if the embedded struct pointer is nil. Just skip copying when fails.
src, err = src.FieldByIndexErr(c.srcFieldIndex)
if err != nil {
// There's no src field to copy from, reset the dst field to zero
c.setFieldZero(dst, c.dstFieldIndex)
return nil //nolint:nilerr
}
}
if c.srcFieldUnexported {
if !src.CanAddr() {
return fmt.Errorf("%w: accessing unexported field requires it to be addressable",
ErrValueUnaddressable)
}
src = reflect.NewAt(src.Type(), unsafe.Pointer(src.UnsafeAddr())).Elem() //nolint:gosec
}
if len(c.dstFieldIndex) == 1 {
dst = dst.Field(c.dstFieldIndex[0])
} else {
// Get dst field with making sure it's settable
dst = c.getFieldWithInit(dst, c.dstFieldIndex)
}
if c.dstFieldUnexported {
if !dst.CanAddr() {
return fmt.Errorf("%w: accessing unexported field requires it to be addressable",
ErrValueUnaddressable)
}
dst = reflect.NewAt(dst.Type(), unsafe.Pointer(dst.UnsafeAddr())).Elem() //nolint:gosec
}
// Use custom copier if set
if c.copier != nil {
return c.copier.Copy(dst, src)
}
// Otherwise, just perform simple direct copying
dst.Set(src)
return nil
}
// getFieldWithInit gets deep nested field with init value for pointer ones
func (c *structField2FieldCopier) getFieldWithInit(field reflect.Value, index []int) reflect.Value {
for _, idx := range index {
if field.Kind() == reflect.Pointer {
if field.IsNil() {
field.Set(reflect.New(field.Type().Elem()))
}
field = field.Elem()
}
field = field.Field(idx)
}
return field
}
// setFieldZero sets zero to a deep nested field
func (c *structField2FieldCopier) setFieldZero(field reflect.Value, index []int) {
field, err := field.FieldByIndexErr(index)
if err == nil && field.IsValid() {
field.Set(reflect.Zero(field.Type())) // NOTE: Go1.18 has no SetZero
}
}
// structField2MethodCopier data structure of copier that copies between `fields` and `methods`
type structField2MethodCopier struct {
dstMethod int
dstMethodUnexported bool
srcFieldIndex []int
srcFieldUnexported bool
}
// Copy implementation of Copy function for struct field copier between `fields` and `methods`
func (c *structField2MethodCopier) Copy(dst, src reflect.Value) (err error) {
if len(c.srcFieldIndex) == 1 {
src = src.Field(c.srcFieldIndex[0])
} else {
// NOTE: When a struct pointer is embedded (e.g. type StructX struct { *BaseStruct }),
// this retrieval can fail if the embedded struct pointer is nil. Just skip copying when fails.
src, err = src.FieldByIndexErr(c.srcFieldIndex)
if err != nil {
return nil //nolint:nilerr
}
}
if c.srcFieldUnexported {
if !src.CanAddr() {
return fmt.Errorf("%w: accessing unexported field requires it to be addressable",
ErrValueUnaddressable)
}
src = reflect.NewAt(src.Type(), unsafe.Pointer(src.UnsafeAddr())).Elem() //nolint:gosec
}
dst = dst.Addr().Method(c.dstMethod)
if c.dstMethodUnexported {
dst = reflect.NewAt(dst.Type(), unsafe.Pointer(dst.UnsafeAddr())).Elem() //nolint:gosec
}
errVal := dst.Call([]reflect.Value{src})[0]
if errVal.IsNil() {
return nil
}
err, ok := errVal.Interface().(error)
if !ok {
return fmt.Errorf("%w: struct method returned non-error value", ErrTypeInvalid)
}
return err
}