-
Notifications
You must be signed in to change notification settings - Fork 3
/
providers.go
134 lines (116 loc) · 3.28 KB
/
providers.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
package inject
import (
"fmt"
"reflect"
)
type providerKey struct {
valueType reflect.Type
annotationType reflect.Type
}
type providerData struct {
provider reflect.Value
arguments []providerKey
hasError bool
cached bool
}
type providersData struct {
// A map of provider keys to provider functions.
providers map[providerKey]providerData
}
func buildProviders(module Module) (*providersData, error) {
providers := &providersData{
providers: map[providerKey]providerData{},
}
for _, module := range flattenModule(module) {
dynamicProviders, err := Providers(module)
if err != nil {
return nil, err
}
for _, dynamicProvider := range dynamicProviders {
if err := buildProvidersFromDynamicProvider(dynamicProvider, providers); err != nil {
return nil, err
}
}
}
return providers, nil
}
func buildProvidersFromDynamicProvider(dynamicProvider Provider, providers *providersData) error {
if !dynamicProvider.IsValid() {
return fmt.Errorf("%#v is an invalid provider.", dynamicProvider)
}
function := dynamicProvider.Function()
functionType := function.Type()
arguments := make([]providerKey, 0, functionType.NumIn()/2)
for inputIndex := 0; inputIndex < functionType.NumIn(); inputIndex += 2 {
valueInput := functionType.In(inputIndex)
annotationInput := functionType.In(inputIndex + 1)
arguments = append(arguments, providerKey{
valueType: valueInput,
annotationType: annotationInput,
})
}
provider := providerData{
provider: function,
arguments: arguments,
cached: dynamicProvider.cached,
hasError: functionType.NumOut() == 3,
}
key := providerKey{
valueType: functionType.Out(0),
annotationType: functionType.Out(1),
}
if existingProvider, ok := providers.providers[key]; ok {
if !reflect.DeepEqual(existingProvider.provider, provider.provider) {
return fmt.Errorf(
"Duplicate providers for key {%v, %v}",
key.valueType, key.annotationType)
}
}
providers.providers[key] = provider
return nil
}
var globalAnnotationType = reflect.TypeOf((*Annotation)(nil)).Elem()
var globalErrorType = reflect.TypeOf((*error)(nil)).Elem()
func isProvider(methodType reflect.Type) bool {
if methodType.Kind() != reflect.Func {
return false
}
if methodType.NumOut() != 2 {
return false
}
return hasAnnotationOutput(methodType) && hasInputsWithAnnotations(methodType)
}
func isProviderWithError(methodType reflect.Type) bool {
if methodType.Kind() != reflect.Func {
return false
}
if methodType.NumOut() != 3 {
return false
}
if !isError(methodType.Out(2)) {
return false
}
return hasAnnotationOutput(methodType) && hasInputsWithAnnotations(methodType)
}
func hasAnnotationOutput(methodType reflect.Type) bool {
return isAnnotation(methodType.Out(1))
}
func hasInputsWithAnnotations(methodType reflect.Type) bool {
numberOfInputs := methodType.NumIn()
if numberOfInputs%2 != 0 {
return false
}
for inputIndex := 1; inputIndex < numberOfInputs; inputIndex += 2 {
annotationInput := methodType.In(inputIndex)
if !isAnnotation(annotationInput) {
return false
}
}
return true
}
func isAnnotation(valueType reflect.Type) bool {
return valueType.Implements(globalAnnotationType)
}
func isError(valueType reflect.Type) bool {
return valueType == globalErrorType || valueType.Implements(globalErrorType)
}