-
Notifications
You must be signed in to change notification settings - Fork 2
/
flows.go
186 lines (176 loc) · 4.96 KB
/
flows.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
package nject
import (
"reflect"
)
func (fm provider) DownFlows() ([]reflect.Type, []reflect.Type) {
switch fm.class {
case unsetClassType:
// continue
default:
return fm.flows[inputParams].Types(), fm.flows[outputParams].Types()
}
switch r := fm.fn.(type) {
case Reflective:
return reflectiveEffectiveOutputs(r)
case generatedFromInjectionChain:
return nil, nil
}
v := reflect.ValueOf(fm.fn)
if !v.IsValid() {
return nil, nil
}
t := v.Type()
if t.Kind() == reflect.Func {
switch fm.group {
case finalGroup:
return typesIn(t), nil
default:
return effectiveOutputs(t)
}
}
if fm.group == invokeGroup && t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Func {
return nil, typesIn(t.Elem())
}
return nil, []reflect.Type{t}
}
func reflectiveEffectiveOutputs(r Reflective) ([]reflect.Type, []reflect.Type) {
fn := wrappedReflective{r}
if w, ok := r.(ReflectiveWrapper); ok {
in := typesIn(fn)
// discard the first type because it's the inner()
return in[1:], typesIn(wrappedReflective{w.Inner()})
}
return effectiveOutputs(fn)
}
// The inputs to inner() are additional types that are provided
// downstream.
func effectiveOutputs(fn reflectType) ([]reflect.Type, []reflect.Type) {
inputs := typesIn(fn)
outputs := typesOut(fn)
if len(inputs) == 0 || inputs[0].Kind() != reflect.Func {
for i := len(outputs) - 1; i >= 0; i-- {
out := outputs[i]
if out == terminalErrorType {
outputs = append(outputs[:i], outputs[i+1:]...)
}
}
return inputs, outputs
}
i0 := inputs[0]
inputs = inputs[1:]
return inputs, typesIn(i0)
}
func (c Collection) netFlows(f func(fm *provider) ([]reflect.Type, []reflect.Type)) ([]reflect.Type, []reflect.Type) {
available := make(interfaceMap)
seenIn := make(map[reflect.Type]struct{})
uniqueIn := make([]reflect.Type, 0, len(c.contents)*4)
seenOut := make(map[reflect.Type]struct{})
uniqueOut := make([]reflect.Type, 0, len(c.contents)*4)
for i, fm := range c.contents {
inputs, outputs := f(fm)
inputsByType := make(map[reflect.Type]struct{})
for _, input := range inputs {
t, _, err := available.bestMatch(getTypeCode(input), "input")
if err == nil {
input = t.Type()
}
inputsByType[input] = struct{}{}
if _, ok := seenOut[input]; ok {
continue
}
if _, ok := seenIn[input]; ok {
continue
}
seenIn[input] = struct{}{}
uniqueIn = append(uniqueIn, input)
}
for _, output := range outputs {
available.Add(getTypeCode(output), i, fm)
if _, ok := inputsByType[output]; ok {
continue
}
if _, ok := seenIn[output]; ok {
continue
}
if _, ok := seenOut[output]; ok {
continue
}
seenOut[output] = struct{}{}
uniqueOut = append(uniqueOut, output)
}
}
return uniqueIn, uniqueOut
}
// DownFlows provides the net unresolved flows down the injection chain.
// If a type is used both as input and as output for the same provider,
// then that type counts as an input only.
func (c Collection) DownFlows() ([]reflect.Type, []reflect.Type) {
return c.netFlows(func(fm *provider) ([]reflect.Type, []reflect.Type) {
return fm.DownFlows()
})
}
func (fm provider) UpFlows() ([]reflect.Type, []reflect.Type) {
switch fm.class {
case unsetClassType:
// continue
default:
return fm.flows[receivedParams].Types(), fm.flows[returnParams].Types()
}
switch r := fm.fn.(type) {
case Reflective:
return reflectiveEffectiveReturns(r)
case generatedFromInjectionChain:
return nil, nil
}
v := reflect.ValueOf(fm.fn)
if !v.IsValid() {
return nil, nil
}
t := v.Type()
if t.Kind() == reflect.Func {
switch fm.group {
case finalGroup:
return nil, typesOut(t)
default:
return effectiveReturns(t)
}
}
if fm.group == invokeGroup && t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Func {
return typesOut(t.Elem()), nil
}
return nil, nil
}
func reflectiveEffectiveReturns(r Reflective) ([]reflect.Type, []reflect.Type) {
fn := wrappedReflective{r}
if w, ok := r.(ReflectiveWrapper); ok {
return typesOut(wrappedReflective{w.Inner()}), typesOut(fn)
}
return effectiveReturns(fn)
}
// Only wrapper functions consume return values and only
// wrapper functions provide return values
func effectiveReturns(fn reflectType) ([]reflect.Type, []reflect.Type) {
inputs := typesIn(fn)
if len(inputs) == 0 || inputs[0].Kind() != reflect.Func {
for _, out := range typesOut(fn) {
if out == terminalErrorType {
return nil, []reflect.Type{errorType}
}
}
return nil, nil
}
i0 := inputs[0]
return typesOut(i0), typesOut(fn)
}
// UpFlows provides the net unresolved flows up the injection chain.
// If a type is used both as value it consumes as a return value and also
// as a value that it in turn returns, then the up flow for that provider will
// be counted only by what it consumes.
//
// Providers that return TerminalError are a special case and count as
// producing error.
func (c Collection) UpFlows() ([]reflect.Type, []reflect.Type) {
return c.netFlows(func(fm *provider) ([]reflect.Type, []reflect.Type) {
return fm.UpFlows()
})
}