-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathafter.go
43 lines (36 loc) · 841 Bytes
/
after.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
package httpwrap
import "reflect"
type afterFn struct {
val reflect.Value
inTypes []reflect.Type
outTypes []reflect.Type
}
func newAfter(fn any) (afterFn, error) {
val := reflect.ValueOf(fn)
fnType := val.Type()
inTypes, outTypes := []reflect.Type{}, []reflect.Type{}
for i := 0; i < fnType.NumIn(); i++ {
inTypes = append(inTypes, fnType.In(i))
}
for i := 0; i < fnType.NumOut(); i++ {
outTypes = append(outTypes, fnType.Out(i))
}
if err := validateAfter(inTypes, outTypes); err != nil {
return afterFn{}, err
}
return afterFn{
val: val,
inTypes: inTypes,
outTypes: outTypes,
}, nil
}
func (fn afterFn) run(ctx *runctx) {
inputs, err := ctx.generate(fn.inTypes)
if err != nil {
return
}
outs := fn.val.Call(inputs)
for i := 0; i < len(outs); i++ {
ctx.provide(outs[i].Interface())
}
}