-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoke.go
More file actions
93 lines (79 loc) · 2.15 KB
/
invoke.go
File metadata and controls
93 lines (79 loc) · 2.15 KB
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
package di
import (
"reflect"
"github.com/pkg/errors"
)
func (c *container) Invoke(f interface{}) ([]reflect.Value, error) {
tp := reflect.TypeOf(f)
val := reflect.ValueOf(f)
if f == nil {
return nil, errors.New("f cannot be nil")
}
if tp.Kind() != reflect.Func {
return nil, errors.New("f must be a function")
}
var args []reflect.Value
if tp.NumIn() > 0 {
for i := 0; i < tp.NumIn(); i++ {
inType := tp.In(i)
if (inType.Kind() != reflect.Pointer || inType.Elem().Kind() != reflect.Struct) && inType.Kind() != reflect.Interface {
return nil, errors.New("parameter must be interface or *struct")
}
s, err := c.parseStruct(inType)
if err != nil {
return nil, err
}
dep, ok := c.singletonStore.Load(s.FullType())
if ok {
arg := reflect.ValueOf(dep)
if arg.Kind() == reflect.Func {
objs, err := c.Invoke(dep)
if err != nil {
return nil, err
}
arg = objs[0]
}
if inType.Kind() == reflect.Pointer && inType.Elem().Kind() == reflect.Struct {
if arg.Type().AssignableTo(inType) {
hasInjectFields := false
structType := inType.Elem()
for i := 0; i < structType.NumField(); i++ {
if _, ok := structType.Field(i).Tag.Lookup("inject"); ok {
hasInjectFields = true
break
}
}
if hasInjectFields {
ptrValue := reflect.New(inType.Elem()).Interface()
if err = c.Resolve(ptrValue); err != nil {
return nil, err
}
args = append(args, reflect.ValueOf(ptrValue))
} else {
args = append(args, arg)
}
} else {
ptrValue := reflect.New(inType.Elem()).Interface()
if err = c.Resolve(ptrValue); err != nil {
return nil, err
}
args = append(args, reflect.ValueOf(ptrValue))
}
continue
}
if inType.Kind() == reflect.Interface {
args = append(args, arg)
} else if inType.Kind() == reflect.Struct {
arg = arg.Elem()
args = append(args, arg)
} else {
args = append(args, arg)
}
continue
}
return nil, errors.New("dependency not found: " + s.FullType())
}
}
values := val.Call(args)
return values, nil
}