-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.go
399 lines (379 loc) · 11 KB
/
generate.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
package nject
import (
"fmt"
"reflect"
"sync/atomic"
)
type valueCollection []reflect.Value
// This maps a parameter list (either inputs or outputs) into the up or down
// valueCollection.
type parameterMap struct {
vcIndex []int // maps param index to value collection index
types []reflect.Type // maps param index to param type
len int // length of the above since it's used
}
func (v valueCollection) Copy() valueCollection {
dup := make(valueCollection, len(v))
copy(dup, v)
return dup
}
// generageFlowMap generates a mapping between the parameter list indexes and positions within
// a valueCollection.
func generateParameterMap(
fm *provider,
param flowType, // which parameter list to map to/from
start int, // offset into the parameter list to start mapping
rmap map[typeCode]typeCode, // type overrides for parameters vs valueCollection
vmap map[typeCode]int, // map of types in valueCollection to their positions
purpose string, // debug string
) (parameterMap, error) {
flow := fm.flows[param]
m := make([]int, len(flow))
types := make([]reflect.Type, len(flow))
for i, p := range flow {
if i < start && p != noTypeCode {
return parameterMap{}, fm.errorf("internal error #13: expecting notype for skipped %s, %d %s", param, i, purpose)
}
if p == noTypeCode && i >= start {
return parameterMap{}, fm.errorf("internal error #14: expecting skip for notype %s, %d %s", param, i, purpose)
}
if p != noTypeCode {
var useP typeCode
if rmap == nil {
useP = p
} else {
var found bool
useP, found = rmap[p]
if !found {
return parameterMap{}, fm.errorf("internal error #7: rmap incomplete %s %d %s missing %s", param, i, purpose, p)
}
}
vci, found := vmap[useP]
if !found {
return parameterMap{}, fm.errorf("internal error #8: vmap incomplete %s %d %s missing %s was %s", param, i, purpose, useP, p)
}
m[i] = vci
types[i] = useP.Type()
}
}
return parameterMap{
vcIndex: m,
types: types,
len: len(flow),
}, nil
}
// generateInputMapper returns a function that copies values from valueCollection to an array of reflect.Value
func generateInputMapper(fm *provider, start int, param flowType, rmap map[typeCode]typeCode, vmap map[typeCode]int, purpose string) (func(valueCollection) []reflect.Value, error) {
pMap, err := generateParameterMap(fm, param, start, rmap, vmap, purpose+" valueCollection->[]")
if err != nil {
return nil, err
}
return func(v valueCollection) []reflect.Value {
if debugEnabled() {
debugf("%s: %s [%s] numIn:%d, m:%v", fm, param, formatFlow(fm.flows[param]), pMap.len, pMap.vcIndex)
dumpValueArray(v, "", vmap)
}
in := make([]reflect.Value, pMap.len)
for i := start; i < pMap.len; i++ {
if pMap.vcIndex[i] != -1 {
in[i] = v[pMap.vcIndex[i]]
if !in[i].IsValid() {
in[i] = reflect.Zero(pMap.types[i])
}
}
}
return in
}, nil
}
// generateOutputMapper returns a function that copies values from an array of reflect.Value to a valueCollection
// nolint:unparam
func generateOutputMapper(fm *provider, start int, param flowType, vmap map[typeCode]int, purpose string) (func(valueCollection, []reflect.Value), error) {
pMap, err := generateParameterMap(fm, param, start, nil, vmap, purpose+" []->valueCollection")
if err != nil {
return nil, err
}
return func(v valueCollection, out []reflect.Value) {
for i := start; i < pMap.len; i++ {
if pMap.vcIndex[i] != -1 {
v[pMap.vcIndex[i]] = out[i]
if !v[pMap.vcIndex[i]].IsValid() {
v[pMap.vcIndex[i]] = reflect.Zero(pMap.types[i])
}
}
}
}, nil
}
func makeZero(fm *provider, vMap map[typeCode]int, mustZero []typeCode, context string) (func(v valueCollection), error) {
zeroMap := make(map[int]reflect.Type)
newMap := make(map[int]reflect.Type)
done := make(map[typeCode]bool)
for _, p := range mustZero {
if done[p] {
continue
}
done[p] = true
i, found := vMap[p]
if !found {
return nil, fm.errorf("internal error #9: no type mapping for %s that must be zeroed", p)
}
if reflect.Zero(p.Type()).CanInterface() {
zeroMap[i] = p.Type()
} else if reflect.New(p.Type()).CanAddr() && reflect.New(p.Type()).Elem().CanInterface() {
newMap[i] = p.Type()
} else if !fm.callsInner {
return nil, fm.errorf("cannot create useful zero value for %s (%s)", p, context)
}
}
return func(v valueCollection) {
for i, typ := range zeroMap {
v[i] = reflect.Zero(typ)
}
for i, typ := range newMap {
v[i] = reflect.New(typ).Elem()
}
}, nil
}
func terminalErrorIndex(fn reflectType) (int, error) {
for i, t := range typesOut(fn) {
if t == terminalErrorType {
return i, nil
}
}
return -1, fmt.Errorf("internal error #10: Could not find TerminalError in output")
}
func generateWrappers(
fm *provider,
downVmap map[typeCode]int, // value collection map for variables passed down
upVmap map[typeCode]int, // value collection map for return values coming up
) error {
fv := getCanCall(fm.fn)
switch fm.class {
case finalFunc:
inMap, err := generateInputMapper(fm, 0, inputParams, fm.downRmap, downVmap, "in(final)")
if err != nil {
return err
}
upMap, err := generateOutputMapper(fm, 0, returnParams, upVmap, "up(final)")
if err != nil {
return err
}
fm.wrapEndpoint = func(v valueCollection) {
in := inMap(v)
upMap(v, fv.Call(in))
}
case wrapperFunc:
inMap, err := generateInputMapper(fm, 1, inputParams, fm.downRmap, downVmap, "in(w)") // parameters to the middleware handler
if err != nil {
return err
}
outMap, err := generateOutputMapper(fm, 0, outputParams, downVmap, "out(w)") // parameters to inner()
if err != nil {
return err
}
upMap, err := generateOutputMapper(fm, 0, returnParams, upVmap, "up(w)") // return values from handler
if err != nil {
return err
}
retMap, err := generateInputMapper(fm, 0, receivedParams, fm.upRmap, upVmap, "ret(w)") // return values from inner()
if err != nil {
return err
}
zero, err := makeZero(fm, upVmap, fm.mustZeroIfInnerNotCalled, "upMap(w)")
if err != nil {
return err
}
in0Type, reflective := getInZero(fv)
fm.wrapWrapper = func(v valueCollection, next func(valueCollection)) {
vCopy := v.Copy()
var callCount int32
rTypes := make([]reflect.Type, len(fm.flows[receivedParams]))
for i, tc := range fm.flows[receivedParams] {
rTypes[i] = tc.Type()
}
// for thread safety, this is not built outside WrapWrapper
inner := func(i []reflect.Value) []reflect.Value {
common := func(v valueCollection) []reflect.Value {
outMap(v, i)
next(v)
r := retMap(v)
for i, retV := range r {
if rTypes[i].Kind() == reflect.Interface {
r[i] = retV.Convert(rTypes[i])
}
}
return r
}
if !fm.parallel {
callCount++
if callCount > 1 {
v = vCopy.Copy()
}
return common(v)
}
atomic.AddInt32(&callCount, 1)
return common(vCopy.Copy())
}
in := inMap(v)
if reflective {
in[0] = reflect.ValueOf(inner)
} else {
in[0] = reflect.MakeFunc(in0Type, inner)
}
defer func() {
if r := recover(); r != nil {
zero(v)
panic(r)
}
}()
out := fv.Call(in)
if callCount == 0 {
zero(v)
}
upMap(v, out)
}
case fallibleInjectorFunc:
inMap, err := generateInputMapper(fm, 0, inputParams, fm.downRmap, downVmap, "in(fallible)")
if err != nil {
return err
}
outMap, err := generateOutputMapper(fm, 0, outputParams, downVmap, "out(fallible)")
if err != nil {
return err
}
zero, err := makeZero(fm, upVmap, fm.mustZeroIfInnerNotCalled, "up(fallible)")
if err != nil {
return err
}
errorIndex, err := terminalErrorIndex(getReflectType(fm.fn))
if err != nil {
return err
}
upVerrorIndex := upVmap[getTypeCode(errorType)]
var memoized cacherFunc
if fm.memoized {
memoized = generateCache(fm.id, fv, len(fm.flows[inputParams]), fm.mapKeyCheck)
}
fm.wrapFallibleInjector = func(v valueCollection) bool {
in := inMap(v)
var out []reflect.Value
if fm.memoized {
out = memoized(in)
} else {
out = fv.Call(in)
}
if out[errorIndex].Interface() != nil {
zero(v)
v[upVerrorIndex] = out[errorIndex].Convert(errorType)
if debugEnabled() {
debugln("ABOUT TO RETURN ERROR")
dumpValueArray(v, "error return", upVmap)
}
return true
}
outMap(v, append(out[:errorIndex], out[errorIndex+1:]...))
debugln("ABOUT TO RETURN NIL")
return false
}
case injectorFunc:
inMap, err := generateInputMapper(fm, 0, inputParams, fm.downRmap, downVmap, "in")
if err != nil {
return err
}
outMap, err := generateOutputMapper(fm, 0, outputParams, downVmap, "out")
if err != nil {
return err
}
if fm.memoized {
memoized := generateCache(fm.id, fv, len(fm.flows[inputParams]), fm.mapKeyCheck)
fm.wrapFallibleInjector = func(v valueCollection) bool {
in := inMap(v)
outMap(v, memoized(in))
return false
}
} else {
fm.wrapFallibleInjector = func(v valueCollection) bool {
in := inMap(v)
outMap(v, fv.Call(in))
return false
}
}
case staticInjectorFunc:
inMap, err := generateInputMapper(fm, 0, inputParams, fm.downRmap, downVmap, "in")
if err != nil {
return err
}
outMap, err := generateOutputMapper(fm, 0, outputParams, downVmap, "out")
if err != nil {
return err
}
lookup := generateLookup(fm, fv, len(fm.flows[inputParams]))
fm.wrapStaticInjector = func(v valueCollection) error {
in := inMap(v)
var out []reflect.Value
if lookup != nil {
out = lookup(in)
} else {
out = fv.Call(in)
}
outMap(v, out)
return nil
}
case fallibleStaticInjectorFunc:
inMap, err := generateInputMapper(fm, 0, inputParams, fm.downRmap, downVmap, "in")
if err != nil {
return err
}
outMap, err := generateOutputMapper(fm, 0, outputParams, downVmap, "out")
if err != nil {
return err
}
errorIndex, err := terminalErrorIndex(getReflectType(fm.fn))
if err != nil {
return err
}
zero, err := makeZero(fm, downVmap, fm.mustZeroIfRemainderSkipped, "need to fill in values set in skipped functions")
if err != nil {
return err
}
lookup := generateLookup(fm, fv, len(fm.flows[inputParams]))
fm.wrapStaticInjector = func(v valueCollection) error {
debugf("RUNNING %s", fm)
in := inMap(v)
var out []reflect.Value
if lookup != nil {
out = lookup(in)
} else {
out = fv.Call(in)
}
err := out[errorIndex].Interface() // this is a TerminalError
out[errorIndex] = out[errorIndex].Convert(errorType)
outMap(v, out)
if err != nil {
if debugEnabled() {
debugf("Zeroing for %s", fm)
dumpValueArray(v, "BEFORE", downVmap)
}
zero(v)
if debugEnabled() {
dumpValueArray(v, "AFTER", downVmap)
debugf("RETURNING %v", err)
}
//nolint:errcheck // we know that err can convert to error
return err.(error)
}
if debugEnabled() {
debugf("NOT zeroing for %s", fm)
debugf("RETURNING nil")
}
return nil
}
case invokeFunc, initFunc, literalValue:
// handled elsewhere
return nil
case unsetClassType:
fallthrough
default:
return fmt.Errorf("internal error #11: unexpected class")
}
return nil
}