-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInjector.go
830 lines (625 loc) · 24.7 KB
/
Injector.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/* Package Goij provides a fully automatic recursive dependency injector for dependency initialisation and injection. */
package Goij
import (
"fmt"
"github.com/j7mbo/goij/src/Cache"
"github.com/j7mbo/goij/src/Logger"
"github.com/j7mbo/goij/src/TypeRegistry"
"reflect"
"strings"
)
/* Injector is the interface returned from calling NewInjector() and contains the methods for dependency initialisation. */
type Injector interface {
/*
Make initialises a struct type from a string as long as the string is a key in the TypeRegistry.
Make also recursively initialises and injects all dependencies for all public structs into a copy of the found type.
An interface name can also be provided and a concrete implementation will be attempted to be initialised.
*/
Make(name string) interface{}
/*
Share enables the sharing of a struct for any future injection usage.
Create the object once, share it, and it will be injected first when encountered in any recursive call.
*/
Share(object interface{})
/*
Bind binds an interface to a struct implementation for any future injection usage.
Any encounter of the interface in any future recursive call will have the implementation injected in it's place.
Bind must be used whenever multiple implementing types exist in the type registry for a single interface.
*/
Bind(interfaceName string, structName string)
/*
Delegate delegates the initialisation of a struct type to a lambda or first class function type.
Any encounter of the struct type in any future recursive calls will have the factory initialise the struct.
The factory does not cache the resulting object due to the possibility that it's contents is dynamic.
*/
Delegate(structName string, factoryMethod interface{})
/*
Define allows injection definitions for specific objects.
*/
Define(structName string, paramName string, value interface{})
/*
DefineGlobal allows the global definition of scalars such as strings, integers etc to be injected everywhere.
The matching is performed on property name.
*/
DefineGlobal(paramName string, value interface{})
/*
Invoke executes a function on the given object and returns all return values as an array.
*/
Invoke(object interface{}, methodName string, args ...interface{}) []interface{}
}
type injector struct {
/* Registry of all application types. */
tr *TypeRegistry.TypeRegistry
/* Contains any cached objects we want to draw from. */
objectCache Cache.ObjectCache
/* Initialisation delegates (factories). */
delegates Cache.DelegateCache
/* Optional if you want to know what wizardry is occurring. */
logger *Logger.Logger
/* Bindings from interface to concrete. */
bindings map[string]string
/* Scalar parameter definitions. */
definitions map[string]map[string]interface{}
/* Global scalar parameter definitions. */
globalDefinitions map[string]interface{}
}
func NewInjector(tr *TypeRegistry.TypeRegistry, logger *Logger.Logger) Injector {
return &injector{
tr: tr,
logger: logger,
objectCache: Cache.NewObjectCache(),
delegates: Cache.NewDelegateCache(),
definitions: make(map[string]map[string]interface{}),
globalDefinitions: make(map[string]interface{}),
}
}
/* Format: PackageName.StructName. */
func (ij *injector) Make(name string) interface{} {
ij.log(fmt.Sprintf("injector asked to provision: '%s' by user", name))
/* Let's check the struct and interface registries. */
obj := ij.getObjFromStructOrInterfaceTypeRegistry(name)
/* See if this object is already cached? */
foundObj := ij.objectCache.FindByValue(reflect.ValueOf(obj))
if foundObj != nil {
ij.log(fmt.Sprintf("Object of type: '%T' was already provisioned in registry - returning.", getValue(foundObj)))
return toStructPtr(getValue(foundObj))
}
delegateOrFactory := ij.findAndCallDelegateOrFactory(obj)
if delegateOrFactory != nil {
return delegateOrFactory
}
/* Provision all child fields of this top level object. */
builtObj := ij.buildFields(obj, obj)
/* Cache the object. */
ij.objectCache.Store(toStructPtr(getValue(builtObj)))
return builtObj
}
/* Define scalar parameters for injection. */
func (ij *injector) Define(objectName string, paramName string, value interface{}) {
if _, found := ij.definitions[objectName]; !found {
ij.definitions[objectName] = make(map[string]interface{})
}
ij.definitions[objectName][paramName] = value
}
/* Define global scalar parameters for injection. */
func (ij *injector) DefineGlobal(paramName string, value interface{}) {
ij.globalDefinitions[paramName] = value
}
/* Delegate the initialisation of an object to a factory method. */
func (ij *injector) Delegate(objectName string, factoryMethod interface{}) {
ij.delegates.Store(objectName, factoryMethod)
}
func (ij *injector) Bind(interfaceName string, structName string) {
if ij.bindings == nil {
ij.bindings = make(map[string]string)
}
var interfaceType interface{}
var structType interface{}
if interfaceType = ij.tr.FindInterfaceType(interfaceName); interfaceType == nil {
panic(fmt.Sprintf("Interface type: '%s' not found in struct registry, did you register it?", interfaceName))
}
if structType = ij.tr.FindStructType(structName); structType == nil {
panic(fmt.Sprintf("RegistryStruct type: '%s' not found in struct registry, did you register it?", structName))
}
ij.bindings[interfaceName] = structName
}
func (ij *injector) Invoke(object interface{}, methodName string, args ...interface{}) []interface{} {
inputs := make([]reflect.Value, len(args))
for i, arg := range args {
inputs[i] = reflect.ValueOf(arg)
}
results := reflect.ValueOf(object).MethodByName(methodName).Call(inputs)
outputs := make([]interface{}, len(results))
for i, result := range results {
outputs[i] = result.Interface()
}
return outputs
}
func (ij *injector) Share(obj interface{}) {
ij.objectCache.Store(obj)
}
/* Checks both the struct registry and the interface registry. */
func (ij *injector) getObjFromStructOrInterfaceTypeRegistry(name string) interface{} {
obj := ij.tr.FindStructType(name)
if obj != nil {
/* Object in registry is a struct - so create a ptr copy so when we pass obj in, it is updated recursively. */
return toStructPtr(obj)
}
/* Is it an interface though? */
interfaceType := ij.tr.FindInterfaceType(name)
if interfaceType == nil {
ij.panic(fmt.Sprintf("No type found in registry for name: '%s', did you forget to register it?", name))
}
/* Is the interface bound to a single concrete type via bind()? */
if structName, found := ij.bindings[name]; found {
return toStructPtr(ij.tr.FindStructType(structName))
}
/* Does the interface have a delegate (for when there are no exported structs for that interface)? */
delegateResults := ij.findAndCallDelegateOrFactory(interfaceType)
if delegateResults != nil {
return delegateResults
}
/* Interface type exists so search for a single implementing type. If more, user needs to bind one. */
structTypes := ij.tr.FindStructTypesByInterfaceType(name)
switch lenStructs := len(structTypes); {
case lenStructs == 0:
ij.panic("You can't Make() an interface unless there is exactly one implementing type in the registry.")
case lenStructs > 1:
ij.panic(
fmt.Sprintf(
"Multiple implementing types were found for interface: '%s', specify one with bind()", name,
),
)
default:
obj = structTypes[0]
ij.log(
fmt.Sprintf("Single object of type: '%T' implementing: '%s' was found and provisioned", obj, name),
)
}
return toStructPtr(obj)
}
func (ij *injector) buildFields(topLevelObj interface{}, parentObj interface{}) interface{} {
value, fieldCount := ij.getValueAndNumFields(parentObj)
if fieldCount == 0 {
return topLevelObj
}
for i := 0; i < fieldCount; i++ {
fieldName := reflect.TypeOf(getValue(parentObj)).Field(i).Name
fieldType := reflect.TypeOf(getValue(parentObj)).Field(i).Type
fieldIsPointer := reflect.TypeOf(getValue(parentObj)).Field(i).Type.Kind() == reflect.Ptr
valueIsPointer := value.Elem().Kind() == reflect.Ptr
var field interface{}
if valueIsPointer {
/* Ignore private fields */
if !value.Elem().Elem().Field(i).CanSet() {
ij.log(
fmt.Sprintf(
"Found private %s field: %s of type: %s on object: %T, ignoring...",
fieldType.Kind(), fieldName, fieldType, parentObj,
),
)
continue
}
/* Use Addr() to get the actually 'settable' field. */
field = value.Elem().Elem().Field(i).Addr().Interface()
} else {
/* Ignore private fields */
if !value.Elem().Field(i).CanInterface() {
ij.log(
fmt.Sprintf(
"Found private %s field: %s of type: %s on object: %T, ignoring...",
fieldType.Kind(), fieldName, fieldType, parentObj,
),
)
continue
}
field = value.Elem().Field(i).Addr().Interface()
}
ij.log(
fmt.Sprintf(
"Found %s field: %s of type: %s on object: %T", fieldType.Kind(), fieldName, fieldType, parentObj,
),
)
/* Interfaces */
if fieldType.Kind() == reflect.Interface {
obj := ij.provisionTypeFromInterface(fieldType, fieldName)
/* We found a single or bound type, great... but do we have this single or bound type already cached? */
dep := ij.objectCache.FindByType(reflect.TypeOf(obj))
if dep != nil {
ij.log(
fmt.Sprintf(
"Dependency of type: '%T' was already provisioned in registry - returning.", getValue(dep),
),
)
getElem(value.Interface()).Field(i).Set(reflect.ValueOf(toStructPtr(getValue(dep))))
/* Cache the dependency now - it wasn't created by a factory so it's okay to cache it. */
ij.objectCache.Store(dep)
continue
}
/* Any user-registered delegates or automatic factories available for it? */
delegateOrFactoryResult := ij.findAndCallDelegateOrFactory(obj)
if delegateOrFactoryResult != nil {
ij.log(fmt.Sprintf("Found delegate for type: %T. Delegate called and returned: %T", obj, delegateOrFactoryResult))
obj = delegateOrFactoryResult
}
/* Okay, are there any factories available for the INTERFACE instead? */
if delegateOrFactoryResult == nil {
// @todo changed this from fieldType to field, does it work?
delegateOrFactoryResult = ij.findAndCallDelegateOrFactory(field)
if delegateOrFactoryResult != nil {
ij.log(fmt.Sprintf("Found delegate for type: %T. Delegate called and returned: %T", obj, delegateOrFactoryResult))
obj = delegateOrFactoryResult
}
}
obj = toStructPtr(obj)
getElem(value.Interface()).Field(i).Set(reflect.ValueOf(toStructPtr(getValue(obj))))
ij.buildFields(topLevelObj, obj)
continue
}
/* Scalars */
if !fieldIsPointer && fieldType.Kind() != reflect.Struct || (fieldIsPointer && fieldType.Elem().Kind() != reflect.Struct) {
foundDefinition := ij.findDefinitionOrGlobalDefinition(value, fieldName)
if foundDefinition != nil {
getElem(value.Interface()).Field(i).Set(reflect.ValueOf(foundDefinition))
continue
}
/* We don't want to recurse with buildFields for user-provided definitions. */
continue
}
/* If the user has defined a specific injection definition, use this... comes first so overrides Share(). */
foundDefinition := ij.findDefinitionOrGlobalDefinition(value, fieldName)
if foundDefinition != nil {
ij.log(
fmt.Sprintf(
"Definition of type: '%T' was found for object: %T - injecting.", foundDefinition, value,
),
)
getElem(value.Interface()).Field(i).Set(reflect.ValueOf(foundDefinition))
/* We don't want to recurse with buildFields for user-provided definitions. */
continue
}
/* Has the object already been cached by the user? */
dep := ij.objectCache.FindByType(fieldType)
if dep != nil {
ij.log(
fmt.Sprintf(
"Dependency of type: '%T' was already provisioned in registry - returning.", getValue(dep),
),
)
if fieldIsPointer {
getElem(value.Interface()).Field(i).Set(reflect.ValueOf(toStructPtr(getValue(dep))))
} else {
getElem(value.Interface()).Field(i).Set(reflect.ValueOf(getValue(dep)))
}
/* Cache the dependency now - we don't want to cache factory results below as they may be dynamic. */
ij.objectCache.Store(dep)
continue
}
delegateOrFactory := ij.findAndCallDelegateOrFactory(fieldType)
if delegateOrFactory != nil {
dep = delegateOrFactory
} else {
/* Object has not been cached by the user nor is there a factory for it - initialise. */
dep = ij.tr.FindStructTypeByType(fieldType)
}
if dep == nil {
ij.panic(fmt.Sprintf("No type found in registry for name: '%s', did you forget to register it?", fieldName))
}
if fieldIsPointer {
getElem(value.Interface()).Field(i).Set(reflect.ValueOf(toStructPtr(getElem(dep).Interface())))
} else {
getElem(value.Interface()).Field(i).Set(getElem(dep))
}
/* If a factory has returned an object, we don't need to recurse on it as the user has decided to build it. */
if delegateOrFactory == nil {
ij.buildFields(topLevelObj, field)
}
}
return topLevelObj
}
/* On encountering a field asking for an interface, try and figure out which struct to inject. */
func (ij *injector) provisionTypeFromInterface(fieldType reflect.Type, fieldName string) interface{} {
interfaceType := ij.tr.FindInterfaceTypeByType(fieldType)
if interfaceType == nil {
ij.panic(
fmt.Sprintf(
"No interface found in registry for name: '%s', did you forget to register it?", fieldName,
),
)
}
/* We know it exists in the registry now. */
fullInterfaceName := fieldType.PkgPath() + "." + fieldType.Name()
/* Interface type exists so search for a single implementing type. If more exist, user needs to bind one. */
structTypes := ij.tr.FindStructTypesByInterfaceType(fullInterfaceName)
var obj interface{}
/* Is the interface bound to a single concrete type via bind()? */
if structName, found := ij.bindings[fullInterfaceName]; found {
return ij.tr.FindStructType(structName)
}
/* What about a short name for the interface? */
if structName, found := ij.bindings[fieldType.Name()]; found {
return ij.tr.FindStructType(structName)
}
/* Does the interface have a delegate (for when there are no exported structs for that interface)? */
delegateOrFactoryResult := ij.findAndCallDelegateOrFactory(interfaceType)
if delegateOrFactoryResult != nil {
return delegateOrFactoryResult
}
switch lenStructs := len(structTypes); {
case lenStructs == 0:
ij.panic(
"Could not initialise interface dependency unless there is exactly one implementing type in " +
"the registry or it has been bound to a single type with bind().",
)
case lenStructs > 1:
ij.panic(
fmt.Sprintf(
"Multiple implementing types were found for interface: '%s', specify one with bind()",
fullInterfaceName,
),
)
default:
obj = toStructPtr(structTypes[0])
ij.log(
fmt.Sprintf(
"Found single mapping of: '%T' implementing: '%s', provisioning", obj, fullInterfaceName,
),
)
}
return obj
}
func (ij *injector) findDefinitionOrGlobalDefinition(value reflect.Value, fieldName string) interface{} {
/* Is there a short name available (without the package path, so "testObject"). ? */
shortName := value.Type().Elem().Name()
if definition, found := ij.definitions[shortName]; found {
if definitionVal, found := definition[fieldName]; found {
return definitionVal
}
}
/* How about the long name? */
var valueName string
valueIsPointer := value.Elem().Kind() == reflect.Ptr
if valueIsPointer {
valueName = fmt.Sprintf("%s.%s", value.Type().PkgPath(), value.Type().Name())
} else {
valueName = fmt.Sprintf("%s.%s", value.Type().Elem().PkgPath(), value.Type().Elem().Name())
}
if definition, found := ij.definitions[valueName]; found {
if definitionVal, found := definition[fieldName]; found {
return definitionVal
}
}
/* Is there a globally available injection definition? */
if definitionVal, found := ij.globalDefinitions[fieldName]; found {
return definitionVal
}
return nil
}
func (ij *injector) findAndCallDelegateOrFactory(objType interface{}) interface{} {
/* Any user-registered delegates for it? */
userProvidedDelegate := ij.delegates.FindByType(reflect.TypeOf(objType))
if userProvidedDelegate != nil {
return ij.callDelegate(userProvidedDelegate)
}
/* Automatic factory usage possible? */
typeName := fmt.Sprintf("%s.%s", getElem(objType).Type().PkgPath(), getElem(objType).Type().Name())
/* Did we pass in a reflect.Type? Easier than checking the type of the type of the type etc.. */
if typeName == "reflect.rtype" {
/* We can have a **reflect.rtype, don't ask me why. I lost that a long time ago in this craziness. */
objType = getElem(objType).Addr().Interface()
assertedType := objType.(reflect.Type)
typeName = fmt.Sprintf("%s.%s", assertedType.PkgPath(), assertedType.Name())
/*
If this is an interface, but the type is private, Name() will be lowercased so won't be found:
ie: Injector interface, but here assertedType.Name() will be "injector"..
*/
userProvidedDelegate = ij.delegates.FindByName(typeName)
if userProvidedDelegate != nil {
return ij.callDelegate(userProvidedDelegate)
}
/* What about user-provided short-names? */
assertedTypeName := assertedType.Name()
userProvidedDelegate = ij.delegates.FindByName(assertedTypeName)
if userProvidedDelegate != nil {
return ij.callDelegate(userProvidedDelegate)
}
}
/* What about user-provided short-names? */
userProvidedDelegate = ij.delegates.FindByName(getElem(objType).Type().Name())
if userProvidedDelegate != nil {
return ij.callDelegate(userProvidedDelegate)
}
factoryDelegate := ij.getFactoryFromFactoryRegistry(typeName)
if factoryDelegate != nil {
ij.log(fmt.Sprintf("Found single factory delegate automatically in registry: %T", factoryDelegate))
args := ij.resolveInvocationArgs(factoryDelegate)
if len(args) > 0 {
ij.log(fmt.Sprintf("Ready to inject args: %v into factory delegate: %T", args, factoryDelegate))
}
factoryReturns := reflect.ValueOf(factoryDelegate).Call(args)
return factoryReturns[0].Interface()
}
return nil
}
func (ij *injector) callDelegate(delegate interface{}) interface{} {
factoryReturns := reflect.ValueOf(delegate).Elem().Call(
ij.resolveInvocationArgs(delegate),
)
return factoryReturns[0].Interface()
}
/* Resolves the invocation args for a provided function type. */
func (ij *injector) resolveInvocationArgs(object interface{}) (results []reflect.Value) {
var objectType reflect.Type
if reflect.TypeOf(object).Kind() == reflect.Ptr {
objectType = reflect.TypeOf(object).Elem()
} else {
objectType = reflect.TypeOf(object)
}
if objectType.Kind() != reflect.Func {
return nil
}
numArguments := objectType.NumIn()
if numArguments == 0 {
ij.log(fmt.Sprintf("No invocation args required for delegate: %T", object))
return nil
}
ij.log(fmt.Sprintf("Resolving invocation args for delegate: %T", object))
for i := 0; i < numArguments; i++ {
arg := objectType.In(i)
/* Argument names cannot be retrieved with reflection for functions, so they must be the zero value instead. */
if (arg.Kind() != reflect.Interface && arg.Kind() != reflect.Struct && arg.Kind() != reflect.Ptr) ||
(arg.Kind() == reflect.Ptr && arg.Elem().Kind() != reflect.Interface && arg.Elem().Kind() != reflect.Struct) {
ij.log(
fmt.Sprintf(
"Encountered scalar delegate argument: %T for delegate: %T, injecting zero value", arg, object,
),
)
/* In the case it's a pointer to a scalar... like *int64... */
if arg.Kind() == reflect.Ptr && arg.Elem().Kind() != reflect.Struct {
results = append(results, reflect.New(arg.Elem()))
} else {
results = append(results, reflect.New(arg).Elem())
}
continue
}
/* If interface - resolve interface to struct first.. */
if arg.Kind() == reflect.Interface {
argFQName := fmt.Sprintf("%s.%s", arg.PkgPath(), arg.Name())
ij.log(fmt.Sprintf("Encountered interface delegate argument: %s for delegate: %T", argFQName, object))
/* Check if there is a delegate specifically for this interface first... */
if delegateOrFactoryResult := ij.findAndCallDelegateOrFactory(arg); delegateOrFactoryResult != nil {
ij.log(
fmt.Sprintf(
"Retrieved delegate or factory result: %T, for delegate interface argument: %v, for delegate: %T",
delegateOrFactoryResult, arg, object,
),
)
// @todo - Depending on pointer or not??
results = append(results, reflect.ValueOf(delegateOrFactoryResult))
continue
}
if resolvedStruct := ij.provisionTypeFromInterface(arg, argFQName); resolvedStruct != nil {
/* Found struct type from type registry - replace interface in arg var and continue. */
if reflect.TypeOf(resolvedStruct).Kind() == reflect.Ptr && arg.Kind() != reflect.Ptr {
arg = reflect.TypeOf(resolvedStruct).Elem()
} else {
arg = reflect.TypeOf(resolvedStruct)
}
/*
If the argument is the same as the return type from a delegate, it'll be infinitely recursive so avoid..
Naively assumes factories only return one object of the type we want...
*/
returnValue := objectType.Out(0)
if strings.ToLower(arg.String()) == strings.ToLower(returnValue.String()) {
results = append(results, reflect.ValueOf(resolvedStruct))
continue
}
}
}
/* Use cached arg if one exists.. */
if obj := ij.objectCache.FindByType(arg); obj != nil {
ij.log(fmt.Sprintf("Encountered cached delegate argument: %T for delegate: %T", obj, object))
if arg.Kind() == reflect.Ptr && reflect.TypeOf(obj).Elem().Kind() != reflect.Ptr {
results = append(results, reflect.ValueOf(obj))
continue
}
/* Cached things look like **elem, and the delegate arg is not a pointer. */
if arg.Kind() == reflect.Struct && reflect.TypeOf(obj).Elem().Kind() == reflect.Ptr {
results = append(results, getElem(obj))
continue
}
results = append(results, reflect.ValueOf(obj).Elem())
continue
}
/* User delegate or factory? This is effectively a recursive call... */
if delegateOrFactoryResult := ij.findAndCallDelegateOrFactory(arg); delegateOrFactoryResult != nil {
/* In the case that the argument is an interface but we have a struct... */
if arg.Kind() == reflect.Interface && reflect.TypeOf(delegateOrFactoryResult).Kind() == reflect.Struct {
delegateOrFactoryResult = reflect.ValueOf(reflect.PtrTo(reflect.TypeOf(delegateOrFactoryResult))).Interface()
} else if objectType.In(i).Kind() == reflect.Struct && reflect.TypeOf(delegateOrFactoryResult).Kind() == reflect.Ptr {
delegateOrFactoryResult = reflect.ValueOf(delegateOrFactoryResult).Elem().Interface()
}
ij.log(
fmt.Sprintf(
"Retrieved delegate or factory result: %T, for delegate argument: %v, for delegate: %T",
delegateOrFactoryResult, arg, object,
),
)
results = append(results, reflect.ValueOf(delegateOrFactoryResult))
continue
}
var newArg interface{}
if arg.Kind() == reflect.Ptr {
newArg = reflect.New(arg.Elem()).Interface()
} else {
newArg = reflect.New(arg).Interface()
}
if objectType.In(i).Kind() == reflect.Struct && reflect.TypeOf(newArg).Kind() == reflect.Ptr {
newArg = reflect.ValueOf(newArg).Elem().Interface()
}
ij.log(fmt.Sprintf("Provisioning new argument: %T (as none cached) for delegate: %T", newArg, object))
results = append(results, reflect.ValueOf(ij.buildFields(newArg, newArg)))
}
return
}
/*
The only reason we would be calling this method is if there was not a factory delegated already, so this is for auto
factory usage only.
*/
func (ij *injector) getFactoryFromFactoryRegistry(name string) interface{} {
factoryTypes := ij.tr.FindFactoryTypes(name)
numFactories := len(factoryTypes)
if numFactories == 1 {
return factoryTypes[0]
}
if numFactories > 1 {
ij.panic(
fmt.Sprintf(
"More than one factory exists in registry for object: '%s', you must Delegate() one first", name,
),
)
}
return nil
}
func (ij *injector) getValueAndNumFields(obj interface{}) (reflect.Value, int) {
val := reflect.ValueOf(&obj)
num := getElem(obj).NumField()
for val.Kind() == reflect.Ptr {
val = val.Elem()
}
ij.log(fmt.Sprintf("Object: '%s' has %d field(s)", reflect.TypeOf(val.Interface()).String(), num))
return reflect.ValueOf(toStructPtr(val.Interface())).Elem(), num
}
/* Given a value, loop through until we get a concrete element out of it. */
func getValue(obj interface{}) interface{} {
return getElem(obj).Interface()
}
func getElem(obj interface{}) reflect.Value {
val := reflect.ValueOf(obj)
for val.Kind() == reflect.Ptr {
val = val.Elem()
}
return val
}
func toStructPtr(obj interface{}) interface{} {
vp := reflect.New(reflect.TypeOf(obj))
vp.Elem().Set(reflect.ValueOf(obj))
return vp.Interface()
}
/* Log normal 'debug-level' stuff. */
func (ij *injector) log(msg string) {
if ij.logger != nil {
ij.logger.Debug(msg)
}
}
/* Log error stuff. */
func (ij *injector) elog(msg string) {
if ij.logger != nil {
ij.logger.Error(msg)
}
}
/* Log imminent death. And then die. */
func (ij *injector) panic(msg string) {
ij.elog(msg)
panic(msg)
}