-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfiller.go
561 lines (530 loc) · 14.5 KB
/
filler.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package nject
import (
"fmt"
"reflect"
"strings"
"unicode"
"unicode/utf8"
)
const debugFiller = false
// filler tracks how to fill a struct, it is an example implementation
// of Reflective
type filler struct {
pointer bool
copy bool
typ reflect.Type
inputs []inputDisposition
}
// inputDisposition is how one input to Call() gets stored
// into a struct
type inputDisposition struct {
mapping []int
typ reflect.Type
}
var _ Reflective = &filler{}
// canCall is either a reflect.Value, a filler, or a Reflective
type canCall interface {
Call([]reflect.Value) []reflect.Value
}
func getCanCall(f interface{}) canCall {
if r, ok := f.(Reflective); ok {
return r
}
return reflect.ValueOf(f)
}
type canRealType interface {
Type() reflect.Type
}
type canFakeType interface {
In(int) reflect.Type
}
type canInner interface {
Inner() ReflectiveArgs
}
func getReflectType(i interface{}) reflectType {
if r, ok := i.(Reflective); ok {
w := wrappedReflective{r}
return w
}
return reflect.TypeOf(i)
}
// getInZero is used to get the first input type for a function
// that is in a canCall. This depends upon knowing that canCall
// is either a reflect.Type, a Reflective, or a ReflectiveWrapper
//
// If it's reflective, the bool will be true and the type will
// be nil
func getInZero(cc canCall) (reflect.Type, bool) {
if t, ok := cc.(canRealType); ok {
return t.Type().In(0), false
}
if _, ok := cc.(canInner); ok {
return nil, true
}
if t, ok := cc.(canFakeType); ok {
return t.In(0), false
}
return nil, false
}
type fillerOptions struct {
tag string
postMethodName []string
postActionByTag map[string]postActionOption
postActionByName map[string]postActionOption
postActionByType []postActionOption
create bool
}
var reservedTags = map[string]struct{}{
"whole": {},
"blob": {},
"fields": {},
"field": {},
"-": {},
"skip": {},
"nofill": {},
"fill": {},
}
// MakeStructBuilder generates a Provider that wants to receive as
// arguments all of the fields of the struct and returns the struct
// as what it provides.
//
// The input model must be a struct: if not MakeStructFiller
// will panic. Model may be a pointer to a struct or a struct.
// Unexported fields are always ignored.
// Passing something other than a struct or pointer to a struct to
// MakeStructBuilder results is an error. Unknown tag values is an error.
//
// Struct tags can be used to control the
// behavior: the argument controls the name of the struct tag used.
//
// The following struct tags are pre-defined. User-created struct tags
// (created with PostActionByTag) may not uses these names:
//
// "whole" & "blob": indicate that an embedded struct should be filled as
// a blob rather than field-by-field.
//
// "field" & "fields": indicates that an embedded struct should be filled
// field-by-field. This is the default and the tag exists for clarity.
//
// "-" & "skip": the field should not be filled and it should should ignore a
// PostActionByType and PostActionByName matches.
// PostActionByTag would still apply.
//
// "nofill": the field should not be filled, but all PostActions still
// apply. "nofill" overrides other behviors including defaults set with
// post-actions.
//
// "fill": normally if there is a PostActionByTag match, then the field
// will not be filled from the provider chain. "fill" overrides that
// behavior. "fill" overrides other behaviors including defaults set with
// post-actions.
//
// If you just want to provide a value variable, use FillVars() instead.
func MakeStructBuilder(model interface{}, optArgs ...FillerFuncArg) (Provider, error) {
// Options handling
options := fillerOptions{
tag: "nject",
create: true,
postActionByTag: make(map[string]postActionOption),
postActionByName: make(map[string]postActionOption),
}
for _, f := range optArgs {
f(&options)
}
for tag := range options.postActionByTag {
if _, ok := reservedTags[tag]; ok {
return nil, fmt.Errorf("Tag value '%s' is reserved and cannot be used by PostAction", tag)
}
}
byType := make(map[typeCode][]postActionOption)
for _, fun := range options.postActionByType {
v := reflect.ValueOf(fun.function)
if !v.IsValid() || v.Type().Kind() != reflect.Func || v.Type().NumIn() == 0 {
return nil, fmt.Errorf("PostActionByType for %T called with an invalid value: %T", model, fun)
}
in0 := getTypeCode(v.Type().In(0))
byType[in0] = append(byType[in0], fun)
}
// Type verification
t := reflect.TypeOf(model)
if debugFiller {
fmt.Println("filler type", t.String())
}
originalType := t
f := filler{}
if t.Kind() == reflect.Ptr {
f.pointer = true
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return nil, fmt.Errorf("MakeStructFiller must be called with a struct or pointer to struct, not %T", model)
}
f.typ = t
f.inputs = make([]inputDisposition, 0, t.NumField()+1)
// model creation
var addIgnore bool
if !options.create {
f.inputs = append(f.inputs, inputDisposition{
typ: originalType,
})
f.copy = true
if !f.pointer {
return nil, fmt.Errorf("Cannot fill an existing struct that is not a pointer. Called with %T", model)
}
} else if t.NumField() > 0 && t.Field(0).Type.Kind() == reflect.Func {
// uh, oh, we don't want to take a function
f.inputs = append(f.inputs, inputDisposition{
typ: ignoreType,
})
addIgnore = true
}
// Field handling. A closure so that it can be invoked recursively
// since that's how you have to traverse nested structures.
var additionalReflectives []interface{}
var mapStruct func(t reflect.Type, path []int) error
mapStruct = func(t reflect.Type, path []int) error {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
np := copyIntSlice(path)
np = append(np, field.Index...)
if debugFiller {
fmt.Printf("Field %d of %s: %s %v\n", i, t, field.Name, np)
}
r, _ := utf8.DecodeRuneInString(field.Name)
if !unicode.IsUpper(r) {
continue
}
var skip bool
var noSkip bool
var whole bool
var hardSkip bool
handleFieldFiller := func(fun postActionOption, description string) error {
if hardSkip {
return nil
}
ap, filledWithPtr, err := addFieldFiller(np, field, originalType, fun, description)
if err != nil {
return err
}
if fun.fillSet {
if !noSkip {
skip = !fun.fill
}
} else if filledWithPtr {
if !noSkip {
skip = true
}
}
additionalReflectives = append(additionalReflectives, ap)
return nil
}
if options.tag != "" {
ps := field.Tag.Get(options.tag)
if ps != "" {
for _, tv := range strings.Split(ps, ",") {
switch tv {
case "nofill":
skip = true
case "fill":
skip = false
noSkip = true
case "-", "skip":
skip = true
hardSkip = true
case "whole", "blob":
if field.Type.Kind() == reflect.Struct {
whole = true
} else {
return fmt.Errorf("Cannot use tag %s on type %s (%s) for building struct filler",
tv, field.Name, field.Type)
}
case "fields":
if field.Type.Kind() == reflect.Struct {
whole = false
} else {
return fmt.Errorf("Cannot use tag %s on type %s (%s) for building struct filler",
tv, field.Name, field.Type)
}
default:
// PostActionByTag
if fun, ok := options.postActionByTag[tv]; ok {
err := handleFieldFiller(fun, fmt.Sprintf(
"PostActionByTag(%s, func) for %s", tv, originalType))
if err != nil {
return err
}
} else {
return fmt.Errorf("Invalid struct tag '%s' on %s for building struct filler", tv, field.Name)
}
}
}
}
}
if fun, ok := options.postActionByName[field.Name]; ok {
err := handleFieldFiller(fun, fmt.Sprintf(
"PostActionByName(%s, func) for %s", field.Name, originalType))
if err != nil {
return err
}
}
// PostActionByType
fieldTypes := []reflect.Type{field.Type}
if f.pointer {
fieldTypes = []reflect.Type{reflect.PointerTo(field.Type), field.Type}
}
for _, typ := range fieldTypes {
for _, fun := range byType[getTypeCode(typ)] {
err := handleFieldFiller(fun,
fmt.Sprintf("PostActionByType(%s) for %s", fun.function, originalType))
if err != nil {
return err
}
}
}
if skip {
continue
}
if field.Type.Kind() == reflect.Struct && !whole {
err := mapStruct(field.Type, np)
if err != nil {
return err
}
continue
}
if debugFiller {
fmt.Printf(" map input %d (%s) to %s %v\n", len(f.inputs), field.Type.String(), field.Name, np)
}
f.inputs = append(f.inputs, inputDisposition{
typ: field.Type,
mapping: np,
})
}
return nil
}
err := mapStruct(t, []int{})
if err != nil {
return nil, err
}
// Build the Provider/Cluster
p := Provide(fmt.Sprintf("builder for %T", model), &f)
var chain []interface{}
if addIgnore {
chain = append(chain, ignore{})
}
chain = append(chain, p)
chain = append(chain, additionalReflectives...)
for _, name := range options.postMethodName {
m, err := generatePostMethod(originalType, name)
if err != nil {
return nil, err
}
chain = append(chain, m)
}
if len(chain) == 1 {
return p, nil
}
return Cluster(fmt.Sprintf("builder seq for %T", model), chain...), nil
}
func (f *filler) In(i int) reflect.Type {
return f.inputs[i].typ
}
func (f *filler) NumIn() int {
return len(f.inputs)
}
func (f *filler) Out(i int) reflect.Type {
if f.pointer {
return reflect.PointerTo(f.typ)
}
return f.typ
}
func (f *filler) NumOut() int {
return 1
}
func (f *filler) Call(inputs []reflect.Value) []reflect.Value {
var v reflect.Value
var r reflect.Value
if f.copy {
if debugFiller {
fmt.Println("filling ", f.typ.String())
}
r = inputs[0]
if f.pointer {
if r.IsNil() {
fmt.Println(" IS NIL")
}
v = r.Elem()
} else {
fmt.Println(" not pointer")
v = r
}
} else {
if debugFiller {
fmt.Println("creating ", f.typ.String())
}
r = reflect.New(f.typ)
v = r.Elem()
if !f.pointer {
r = v
}
}
for i, input := range inputs {
disposition := f.inputs[i]
if disposition.mapping == nil {
continue
}
fv := v.FieldByIndex(disposition.mapping)
if debugFiller {
fmt.Printf(" input %d: about to set %v %s (%T -> %T)\n",
i, disposition.mapping, v.Type().FieldByIndex(disposition.mapping).Name,
input.Type().String(), fv.Type().String())
}
fv.Set(input)
}
return []reflect.Value{r}
}
func copyIntSlice(in []int) []int {
c := make([]int, len(in), len(in)+1)
copy(c, in)
return c
}
// addFieldFiller creates a Reflective that wraps a closure that
// unpacks the recently filled struct and extracts the relevant field
// from that struct and substitutes that into the inputs to
// the provided function to be called on a field of the struct
// post-filling.
func addFieldFiller(
path []int,
field reflect.StructField,
outerStruct reflect.Type,
option postActionOption,
context string,
) (Provider, bool, error) {
funcAsValue := reflect.ValueOf(option.function)
if !funcAsValue.IsValid() {
return nil, false, fmt.Errorf("%s was called with something other than a function", context)
}
t := funcAsValue.Type()
if t.Kind() != reflect.Func {
return nil, false, fmt.Errorf("%s was called with a %s instead of with a function", context, t)
}
inputs := typesIn(t)
const bad = 1000000 // a number larger than the number of Methods that an interface might have
const convert = 750000
const assign = 500000
var score int = bad // negative is best
var inputIndex int
var addressOf bool
var needConvert bool
var countEmptyInterfaces int
for i, in := range inputs {
i, in := i, in
check := func(t reflect.Type, aOf bool) {
var s int
var c bool
switch {
case in == t:
s = -1
case t.AssignableTo(in):
if in.Kind() != reflect.Interface {
s = assign
} else {
s = in.NumMethod()
}
case t.ConvertibleTo(in):
s = convert
c = true
default:
return
}
if s >= score {
return
}
score = s
inputIndex = i
addressOf = aOf
needConvert = c
}
if option.matchToInterface {
if in == emptyInterfaceType {
countEmptyInterfaces++
inputIndex = i
addressOf = true
}
continue
}
check(field.Type, false)
check(reflect.PointerTo(field.Type), true)
}
if option.matchToInterface && countEmptyInterfaces != 1 {
return nil, false, fmt.Errorf("%s need exactly one interface{} parameters in function", context)
}
if score == bad {
return nil, false, fmt.Errorf("%s no match found between field type %s and function inputs",
context, field.Type)
}
targetType := inputs[inputIndex]
inputs[inputIndex] = outerStruct
structIsPtr := outerStruct.Kind() == reflect.Ptr
if needConvert && addressOf {
return nil, false, fmt.Errorf(" %s, matched %s to input %s (converting) but that cannot be combined with conversion to a pointer",
context, field.Type, targetType)
}
return Provide(fmt.Sprintf("fill-%s-of-%s", field.Name, outerStruct),
thinReflective{
thinReflectiveArgs: thinReflectiveArgs{
inputs: inputs,
outputs: typesOut(t),
},
fun: func(in []reflect.Value) []reflect.Value {
strct := in[inputIndex]
if structIsPtr {
strct = strct.Elem()
}
v := strct.FieldByIndex(path)
if addressOf {
v = v.Addr()
}
if needConvert {
v = v.Convert(targetType)
}
in[inputIndex] = v
return funcAsValue.Call(in)
},
}), addressOf, nil
}
func generatePostMethod(modelType reflect.Type, methodName string) (Provider, error) {
method, ok := modelType.MethodByName(methodName)
if !ok {
return nil, fmt.Errorf("WIthPostMethod(%s) on %s: no such method exists", methodName, modelType)
}
desc := fmt.Sprintf("%s.%s()", modelType, methodName)
mt := method.Func.Type()
switch {
case mt.In(0) == modelType:
return Provide(desc, thinReflective{
thinReflectiveArgs: thinReflectiveArgs{
inputs: typesIn(mt),
outputs: typesOut(mt),
},
fun: func(in []reflect.Value) []reflect.Value {
return method.Func.Call(in)
},
}), nil
case modelType.Kind() == reflect.Ptr && mt.In(0) == modelType.Elem():
inputs := typesIn(mt)
inputs[0] = modelType
return Provide(desc, thinReflective{
thinReflectiveArgs: thinReflectiveArgs{
inputs: inputs,
outputs: typesOut(mt),
},
fun: func(in []reflect.Value) []reflect.Value {
in[0] = in[0].Elem()
return method.Func.Call(in)
},
}), nil
default:
return nil, fmt.Errorf("internal error #36: no match between model %s and method input %s",
modelType, method.Type.In(0))
}
}