-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
holder.go
135 lines (127 loc) · 3.15 KB
/
holder.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
package di
import (
"errors"
"reflect"
)
type ctor func(ctx *Context) (any, error)
type holder struct {
ctor ctor
created bool
instance any
providesType reflect.Type
}
func newHolder(ctor any, lazy bool) (*holder, *Error) {
ctype := reflect.TypeOf(ctor)
if ctype == nil {
return nil, newInvalidConstructorError("untyped constructor")
}
if lazy {
return createLazyHolder(ctor)
} else {
return createEagerHolder(ctor)
}
}
func createLazyHolder(ctor any) (*holder, *Error) {
ctype := reflect.TypeOf(ctor)
if ctype.Kind() != reflect.Func {
return nil, newInvalidConstructorError("expected constructor function")
}
cval := reflect.ValueOf(ctor)
numResults := ctype.NumOut()
if numResults < 1 || numResults > 2 {
return nil, newInvalidConstructorError("expected one result value with an optional error")
}
if ctype.IsVariadic() {
return nil, newInvalidConstructorError("variadic parameters not supported (use slice instead)")
}
if numResults == 2 {
outtype := reflect.New(ctype.Out(1))
if _, ok := outtype.Interface().(*error); !ok {
return nil, newInvalidConstructorError("expected second result value to be an error")
}
}
resultType := ctype.Out(0)
numArgs := ctype.NumIn()
params := make([]reflect.Type, numArgs)
for i := 0; i < numArgs; i++ {
params[i] = ctype.In(i)
}
prov := func(ctx *Context) (any, error) {
args := make([]reflect.Value, numArgs)
for i, ptype := range params {
if ptype.Kind() == reflect.Slice {
argslice, err := ctx.getAllByRType(ptype.Elem())
if err != nil {
return nil, err
}
rslice := reflect.MakeSlice(ptype, 0, len(argslice))
for _, item := range argslice {
rslice = reflect.Append(rslice, reflect.ValueOf(item))
}
args[i] = rslice
} else if ptype == genericTypeOf[*Context]() {
args[i] = reflect.ValueOf(ctx)
} else {
arg, err := ctx.getByRType(ptype)
if err != nil {
return nil, err
}
args[i] = reflect.ValueOf(arg)
}
}
result := cval.Call(args)
obj := result[0].Interface()
if len(result) == 2 {
if result[1].IsNil() {
return obj, nil
}
err, ok := result[1].Interface().(error)
if !ok {
return nil, newInvalidConstructorError("expected second result value to be an error xxxx")
}
return obj, err
}
return obj, nil
}
return &holder{
ctor: prov,
providesType: resultType,
}, nil
}
func createEagerHolder(value any) (*holder, *Error) {
return &holder{
created: true,
instance: value,
ctor: func(*Context) (any, error) { return value, nil },
providesType: reflect.TypeOf(value),
}, nil
}
func (h *holder) getOrCreate(ctx *Context) (any, error) {
obj := h.instance
if !h.created {
newobj, err := provide(ctx, h)
if err != nil {
return empty[any](), err
}
h.instance = newobj
h.created = true
obj = newobj
}
return obj, nil
}
func provide(ctx *Context, holder *holder) (result any, err error) {
defer func() {
if r := recover(); r != nil {
switch x := r.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.New("ctor panic")
}
result = empty[any]()
}
}()
return holder.ctor(ctx)
}