-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintrospect.go
74 lines (64 loc) · 2.02 KB
/
introspect.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
package injector
import (
"reflect"
"unsafe"
)
func injectFuncArgs(fn interface{}) ([]reflect.Value, error) {
depFactoryType := reflect.TypeOf(fn)
if depFactoryType.Kind() != reflect.Func {
return nil, ErrorDepFactoryNotAFunc
}
// Retrieve dependencies for all factory arguments
args := make([]reflect.Value, depFactoryType.NumIn())
for i := 0; i < depFactoryType.NumIn(); i++ {
inType := depFactoryType.In(i)
resolvedDep, err := InjectT(inType)
if err != nil {
// If the field was not resolvable, attempt to fill it as a struct
possibleStruct := reflect.New(inType)
fillStruct := possibleStruct
// If the field is a pointer, the fill struct must be initialized and de-refereced, else we have a double-pointer **Type
if inType.Kind() == reflect.Pointer {
possibleStruct.Elem().Set(reflect.New(inType.Elem()))
fillStruct = possibleStruct.Elem()
}
// Attempt to fill in the struct with dependencies
if err := injectStructFields(fillStruct.Interface()); err != nil {
return nil, err
}
args[i] = reflect.ValueOf(possibleStruct.Elem().Interface())
} else {
args[i] = reflect.ValueOf(resolvedDep)
}
}
return args, nil
}
func injectStructFields(strct interface{}) error {
depFactoryType := reflect.TypeOf(strct)
if depFactoryType.Kind() != reflect.Ptr {
return ErrorDepNotAPointer
}
if depFactoryType.Elem().Kind() != reflect.Struct {
return ErrorDepNotAStruct
}
// Set struct values to injected dependencies
depFactoryValue := reflect.ValueOf(strct).Elem()
for i := 0; i < depFactoryType.Elem().NumField(); i++ {
field := depFactoryType.Elem().Field(i)
fieldVal := depFactoryValue.Field(i)
if lookupType, exists := field.Tag.Lookup("injector"); exists {
name := DefaultForType
switch lookupType {
case "name":
name = field.Name
}
resolvedDep, err := InjectT(field.Type, name)
if err != nil {
return err
}
ptr := reflect.NewAt(fieldVal.Type(), unsafe.Pointer(fieldVal.UnsafeAddr())).Elem()
ptr.Set(reflect.ValueOf(resolvedDep))
}
}
return nil
}