-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom.go
More file actions
75 lines (64 loc) · 1.39 KB
/
custom.go
File metadata and controls
75 lines (64 loc) · 1.39 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
package provide
import (
"errors"
"reflect"
)
func customProvide(provideFn interface{}) ([]initializer, error) {
v := reflect.ValueOf(provideFn)
t := v.Type()
if t.Kind() != reflect.Func {
return nil, errors.New("providers must be functions")
}
ins := make([]reflect.Type, t.NumIn())
deps := make([]task, len(ins))
for i := range ins {
ins[i] = t.In(i)
deps[i] = task{ins[i], true}
}
type output struct {
Type reflect.Type
IsErr bool
}
outs := make([]output, t.NumOut())
for i := range outs {
out := t.Out(i)
outs[i] = output{
Type: out,
IsErr: isErrorType(out),
}
}
alreadyDone := false
doFn := func(values map[reflect.Type]reflect.Value) error {
if alreadyDone {
return nil
}
alreadyDone = true
inputs := make([]reflect.Value, len(ins))
for i := range ins {
inputs[i] = values[ins[i]]
}
outputs := v.Call(inputs)
for i := range outputs {
if outs[i].IsErr {
if !outputs[i].IsNil() {
return outputs[i].Interface().(error)
}
} else {
values[outs[i].Type] = outputs[i]
}
}
return nil
}
initializers := make([]initializer, 0, len(outs))
for i := range outs {
if outs[i].IsErr {
continue
}
initializers = append(initializers, initializer{
Type: outs[i].Type,
Partial: state{DependsOn: deps, Do: doFn},
Complete: state{DependsOn: []task{{outs[i].Type, false}}},
})
}
return initializers, nil
}