-
Notifications
You must be signed in to change notification settings - Fork 2
/
params.go
484 lines (431 loc) · 13.6 KB
/
params.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package evendeep
import (
"reflect"
"unsafe"
"github.com/hedzr/evendeep/dbglog"
"github.com/hedzr/evendeep/flags"
"github.com/hedzr/evendeep/flags/cms"
"github.com/hedzr/evendeep/internal/cl"
"github.com/hedzr/evendeep/ref"
logz "github.com/hedzr/logg/slog"
)
// Params is params package.
type Params struct {
srcOwner *reflect.Value // srcOwner of source slice or struct, or any others
dstOwner *reflect.Value // dstOwner of destination slice or struct, or any others
srcDecoded *reflect.Value //
dstDecoded *reflect.Value //
srcType reflect.Type // = field(i+parent.srcOffset).type, or srcOwner.type for non-struct
dstType reflect.Type // = field(i+parent.dstOffset).type, or dstOwner.type for non-struct
// index int // struct field or slice index,
// srcOffset int // -1, or an offset of the embedded struct fields
// dstOffset int // -1, or an offset of the embedded struct fields
// srcFieldType *reflect.StructField //
// dstFieldType *reflect.StructField //
// srcAnonymous bool //
// dstAnonymous bool //
// mergingMode bool // base state
visited map[visit]visiteddestination
visiting visit
resultForNewSlice *reflect.Value
targetIterator structIterable //
accessor accessor //
// srcIndex int //
// field *reflect.StructField // source field type
// fieldTags *fieldTags // tag of source field
flags flags.Flags
children map[string]*Params // children of struct fields
childrenAnonymous []*Params // or children without name (non-struct)
owner *Params //
controller *cpController //
}
type visit struct {
addr1, addr2 unsafe.Pointer
typ reflect.Type
}
type visiteddestination struct {
dst reflect.Value
}
type paramsOpt func(p *Params)
func newParams(opts ...paramsOpt) *Params {
p := &Params{}
for _, opt := range opts {
opt(p)
}
return p
}
func withFlags(flagsList ...cms.CopyMergeStrategy) paramsOpt { //nolint:unused //future code
return func(p *Params) {
p.flags = flags.New(flagsList...)
}
}
func withOwnersSimple(c *cpController, ownerParams *Params) paramsOpt { //nolint:revive,unparam //future code
return func(p *Params) {
p.controller = c
if ownerParams != nil {
ownerParams.addChildParams(p)
}
}
}
func withOwners(c *cpController, ownerParams *Params, ownerSource, ownerTarget, osDecoded, otDecoded *reflect.Value) paramsOpt { //nolint:revive,lll,gocognit //future
return func(p *Params) {
p.srcOwner = ownerSource
p.dstOwner = ownerTarget
p.srcDecoded = osDecoded
p.dstDecoded = otDecoded
var st, tt reflect.Type
if p.srcDecoded == nil && p.srcOwner != nil {
d, _ := ref.Rdecode(*p.srcOwner)
p.srcDecoded = &d
if p.srcOwner.IsValid() {
p.srcType = p.srcOwner.Type()
}
}
if p.srcDecoded != nil && p.srcDecoded.IsValid() {
st = p.srcDecoded.Type()
p.parseSourceStruct(ownerParams, st)
p.dstType = p.dstOwner.Type()
} else if p.srcOwner != nil {
st = p.srcOwner.Type()
st = ref.RindirectType(st)
p.parseSourceStruct(ownerParams, st)
p.dstType = st
}
if p.dstDecoded == nil && p.dstOwner != nil {
d, _ := ref.Rdecode(*p.dstOwner)
p.dstDecoded = &d
}
if p.dstDecoded != nil && p.dstDecoded.IsValid() {
tt = p.dstDecoded.Type()
p.parseTargetStruct(ownerParams, tt)
} else if p.dstOwner != nil {
tt = p.dstOwner.Type()
tt = ref.RindirectType(tt)
p.parseTargetStruct(ownerParams, tt)
}
//
// p.mergingMode = c.flags.isAnyFlagsOK(SliceMerge, MapMerge) || ownerParams.isAnyFlagsOK(SliceMerge, MapMerge)
if p.dstDecoded != nil {
t := *p.dstDecoded
p.targetIterator = newStructIterator(t,
withStructPtrAutoExpand(c.autoExpandStruct),
withStructFieldPtrAutoNew(c.autoNewStruct),
withStructSource(p.srcDecoded, c.autoExpandStruct),
)
}
//
p.controller = c
ownerParams.addChildParams(p)
}
}
// func (params *Params) withIteratorIndex(srcIndex int) (sourcefield tableRecT) {
// params.srcIndex = srcIndex
//
// //if i < params.srcType.NumField() {
// // t := params.srcType.Field(i)
// // params.fieldType = &t
// // params.fieldTags = parseFieldTags(t.Tag)
// //}
//
// if srcIndex < len(params.sourcefields.tableRecordsT) {
// sourcefield = params.sourcefields.tableRecordsT[srcIndex]
// params.field = sourcefield.StructField()
// params.fieldTags = parseFieldTags(params.field.Tag)
// }
// return
// }
func (params *Params) sourceFieldShouldBeIgnored() (yes bool) {
if params.targetIterator != nil {
yes = params.targetIterator.SourceFieldShouldBeIgnored(params.controller.ignoreNames)
}
return
}
func (params *Params) shouldBeIgnored(name string) (yes bool) {
if name == "" {
return true
}
if params.targetIterator != nil {
yes = params.targetIterator.ShouldBeIgnored(name, params.controller.ignoreNames)
}
return
}
func (params *Params) nextTargetField() (sourceField *tableRecT, ok bool) {
if params.targetIterator != nil {
byName := params.isGroupedFlagOKDeeply(cms.ByName)
params.accessor, ok = params.targetIterator.Next(params, byName)
if ok {
sourceField = params.accessor.SourceField()
_, isIgnored := params.parseFieldTags(sourceField.structField.Tag)
ok = !isIgnored
}
}
return
}
func (params *Params) nextTargetFieldLite() (ok bool) {
if params.targetIterator != nil {
byName := params.isGroupedFlagOKDeeply(cms.ByName)
params.accessor, ok = params.targetIterator.Next(params, byName)
}
return
}
func (params *Params) inMergeMode() bool {
return params.controller.flags.IsAnyFlagsOK(cms.SliceMerge, cms.MapMerge) ||
params.owner.isAnyFlagsOK(cms.SliceMerge, cms.MapMerge) ||
params.flags.IsAnyFlagsOK(cms.SliceMerge, cms.MapMerge)
}
func (params *Params) dstFieldIsExportedR() (copyUnexportedFields, isExported bool) {
k := reflect.Invalid
if params != nil && params.dstDecoded != nil {
k = params.dstDecoded.Kind()
}
if k != reflect.Struct || params.accessor == nil {
return false, true // non-struct-field target, treat it as exported
}
isExported, copyUnexportedFields = ref.IsExported(params.accessor.StructField()), params.controller.copyUnexportedFields
if isExported && params.owner != nil {
copyUnexportedFields, isExported = params.owner.dstFieldIsExportedR()
}
return
}
// processUnexportedField try to set newval into target if it's an unexported field.
func (params *Params) processUnexportedField(target, newval reflect.Value) (processed bool) {
if params == nil || params.controller == nil || params.accessor == nil {
return
}
if !params.controller.copyUnexportedFields {
return
}
if fld := params.accessor.StructField(); fld != nil {
// in a struct
if !ref.IsExported(fld) {
dbglog.Log(" unexported field %q (typ: %v): old(%v) -> new(%v)",
fld.Name, ref.Typfmt(fld.Type), ref.Valfmt(&target), ref.Valfmt(&newval))
cl.SetUnexportedField(target, newval)
processed = true
}
}
return
}
// func (params *Params) parseSourceFieldTag(i int) {
// params.index = i
// }
func (params *Params) parseSourceStruct(ownerParams *Params, st reflect.Type) {
params.srcType = st
_ = ownerParams
// if kind := st.Kind(); kind == reflect.Struct {
// // idx := index
// // if ownerParams != nil {
// // idx += ownerParams.srcOffset
// // }
// // params.withIteratorIndex(idx)
// // //if idx < st.NumField() {
// // // t := st.Field(idx)
// // // p.srcFieldType = &t
// // // p.fieldTags = parseFieldTags(t.Tag)
// // // p.srcType = t.Type
// // //}
// // //if ownerParams != nil {
// // // if oft := ownerParams.srcFieldType; oft != nil && oft.Anonymous && oft.Type.Kind() == reflect.Struct {
// // // p.srcAnonymous = true
// // // p.srcOffset = p.index
// // // }
// // //}
// }
}
func (params *Params) parseTargetStruct(ownerParams *Params, tt reflect.Type) {
params.dstType = tt
_ = ownerParams
// if kind := tt.Kind(); kind == reflect.Struct {
// // idx := index
// // if ownerParams != nil {
// // idx += ownerParams.dstOffset
// // }
// // //if idx < tt.NumField() {
// // // t := tt.Field(idx)
// // // p.dstFieldType = &t
// // // p.dstType = t.Type
// // //}
// // //if ownerParams != nil {
// // // if oft := ownerParams.dstFieldType; oft != nil && oft.Anonymous && oft.Type.Kind() == reflect.Struct {
// // // p.dstAnonymous = true
// // // p.dstOffset = p.index
// // // }
// // //}
// // //} else if ownerParams != nil && ownerParams.dstFieldType != nil {
// }
}
// addChildParams does link this params into parent params.
func (params *Params) addChildParams(ppChild *Params) {
if params == nil || ppChild == nil {
return
}
// if struct
if ppChild.accessor != nil && ppChild.accessor.StructField() != nil {
fieldName := ppChild.accessor.StructFieldName()
if ppChild.children == nil {
ppChild.children = make(map[string]*Params)
}
if params.children == nil {
params.children = make(map[string]*Params)
}
if _, ok := ppChild.children[fieldName]; ok {
logz.Panic("field exists, cannot iterate another field on the same name", "field", fieldName)
}
// if ppChild == nil {
// logz.Panic("setting nil Params for field, r u kidding me?", "field", fieldName)
// }
params.children[fieldName] = ppChild
} else {
params.childrenAnonymous = append(params.childrenAnonymous, ppChild)
}
ppChild.owner = params
}
// revoke does revoke itself from parent params if necessary.
func (params *Params) revoke() {
if pp := params.owner; pp != nil {
if pp.accessor != nil && pp.accessor.StructField() != nil {
fieldName := pp.accessor.StructFieldName()
delete(pp.children, fieldName)
} else {
for i := 0; i < len(pp.childrenAnonymous); i++ {
if child := pp.childrenAnonymous[i]; child == params {
pp.childrenAnonymous = append(pp.childrenAnonymous[0:i], pp.childrenAnonymous[i+1:]...)
break
}
}
}
}
}
// // ValueOfSource _
// func (params *Params) ValueOfSource() reflect.Value {
// if params.srcFieldType != nil {
// return params.srcDecoded.Field(params.index + params.srcOffset)
// }
// return *params.srcOwner
// }
//
// // ValueOfDestination _
// func (params *Params) ValueOfDestination() reflect.Value {
// if params.dstFieldType != nil {
// return params.dstDecoded.Field(params.index + params.dstOffset)
// }
// return *params.dstOwner
// }
func (params *Params) isStruct() bool { //nolint:unused //future
if params == nil || params.accessor == nil || params.dstOwner == nil {
return false
}
return params.accessor.IsStruct()
}
func (params *Params) parseFieldTags(tag reflect.StructTag) (flagsInTag *fieldTags, isIgnored bool) {
var tagName string
if params.controller != nil {
tagName = params.controller.tagKeyName
}
flagsInTag = parseFieldTags(tag, tagName)
isIgnored = flagsInTag.isFlagIgnored()
return
}
func (params *Params) isFlagExists(ftf cms.CopyMergeStrategy) (ret bool) {
if params == nil {
return
}
if params.controller != nil {
ret = params.controller.flags.IsFlagOK(ftf)
}
if !ret && params.flags != nil {
ret = params.flags.IsFlagOK(ftf)
}
if !ret && params.accessor != nil {
ret = params.accessor.IsFlagOK(ftf)
}
return
}
// isGroupedFlagOK tests if the given flag is exists or valid.
//
// Different with isGroupedFlagOKDeeply is, isGroupedFlagOK will return
// false simply while Params.fieldTags is empty or unset.
//
// When Params.fieldTags is valid, the actual testing will be forwarded
// to Params.fieldTags.flags.isGroupedFlagOK().
func (params *Params) isGroupedFlagOK(ftf ...cms.CopyMergeStrategy) (ret bool) { //nolint:unused //future
if params == nil {
return flags.New().IsGroupedFlagOK(ftf...)
}
if params.controller != nil {
ret = params.controller.flags.IsGroupedFlagOK(ftf...)
}
if !ret && params.flags != nil {
ret = params.flags.IsGroupedFlagOK(ftf...)
}
if !ret && params.accessor != nil {
ret = params.accessor.IsGroupedFlagOK(ftf...)
}
return
}
// isGroupedFlagOKDeeply tests if the given flag is exists or valid.
//
// Different with isGroupedFlagOK is, isGroupedFlagOKDeeply will check
// whether the given flag is a leader (i.e. default choice) in a group
// or not, even if Params.fieldTags is empty or unset. And too, we run
// the same logical on these sources:
//
// 1. params.controller.flags
// 2. params.flags
// 3. params.accessor.fieldTags.flags if present
//
// When Params.fieldTags is valid, the actual testing will be forwarded
// to Params.fieldTags.flags.isGroupedFlagOK().
func (params *Params) isGroupedFlagOKDeeply(ftf ...cms.CopyMergeStrategy) (ret bool) {
if params == nil {
return flags.New().IsGroupedFlagOK(ftf...)
}
if params.controller != nil {
ret = params.controller.flags.IsGroupedFlagOK(ftf...)
}
if !ret && params.flags != nil {
ret = params.flags.IsGroupedFlagOK(ftf...)
}
if !ret && params.accessor != nil {
ret = params.accessor.IsGroupedFlagOK(ftf...)
}
return
}
func (params *Params) isAnyFlagsOK(ftf ...cms.CopyMergeStrategy) (ret bool) {
if params == nil {
return
}
if params.controller != nil {
ret = params.controller.flags.IsAnyFlagsOK(ftf...)
}
if !ret && params.flags != nil {
ret = params.flags.IsAnyFlagsOK(ftf...)
}
if !ret && params.accessor != nil {
ret = params.accessor.IsAnyFlagsOK(ftf...)
}
return
}
func (params *Params) isAllFlagsOK(ftf ...cms.CopyMergeStrategy) (ret bool) { //nolint:unused //future
if params == nil {
return
}
if params.controller != nil {
ret = params.controller.flags.IsAllFlagsOK(ftf...)
}
if !ret && params.flags != nil {
ret = params.flags.IsAllFlagsOK(ftf...)
}
if !ret && params.accessor != nil {
ret = params.accessor.IsAllFlagsOK(ftf...)
}
return
}
func (params *Params) depth() (depth int) {
p := params
for p != nil {
depth++
p = p.owner
}
return
}