-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
246 lines (195 loc) · 4.98 KB
/
container.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
// Copyright 2022 Sergey Novichkov. All rights reserved.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
package di
import (
"errors"
"fmt"
"reflect"
"sync"
"github.com/gozix/di/internal/compiler"
"github.com/gozix/di/internal/cycle"
"github.com/gozix/di/internal/runtime"
)
type (
// container cache
cache map[int]reflect.Value
// container implements the Container interface.
container struct {
mux sync.Mutex
defs definitions
cache map[int]reflect.Value
closers []compiler.Closer
}
)
var (
// container implements the Container interface.
_ Container = (*container)(nil)
// reflectErrorType is error reflect type cache.
reflectErrorType = reflect.TypeOf((*error)(nil)).Elem()
// reflectContainerType is Container reflect type cache.
reflectContainerType = reflect.TypeOf((*Container)(nil)).Elem()
)
func (c *container) Call(fn Function, options ...ConstraintOption) (err error) {
var rv = reflect.ValueOf(fn)
if rv.Kind() != reflect.Func {
return fmt.Errorf("%s : fn %w", runtime.Caller(0), ErrorMustBeFunction)
}
var (
rt = rv.Type()
in = make([]reflect.Value, 0, rt.NumIn())
cs = make(constraints, len(options))
)
for _, o := range options {
o.applyConstraintOption(cs)
}
var (
vt = rt.IsVariadic()
ic = rt.NumIn() - 1
)
for i := 0; i <= ic; i++ {
var (
at = rt.In(i)
dep = &compiler.Dependency{
Name: at.Name(),
Index: i,
Type: at,
Value: reflect.New(at),
}
)
c.mux.Lock()
err = c.resolveDependency(cycle.New(), dep, cs)
c.mux.Unlock()
if err != nil {
return fmt.Errorf("%s : %w", runtime.Caller(0), err)
}
if vt && i == ic {
for i = 0; i < dep.Value.Elem().Len(); i++ {
in = append(in, dep.Value.Elem().Index(i))
}
continue
}
in = append(in, dep.Value.Elem())
}
var out = rv.Call(in)
if len(out) > 0 && out[len(out)-1].Type().Implements(reflectErrorType) && !out[rt.NumOut()-1].IsNil() {
return fmt.Errorf("%s : %w", runtime.Caller(0), out[rt.NumOut()-1].Interface().(error))
}
return nil
}
func (c *container) Close() (err error) {
c.mux.Lock()
var closers = append(([]compiler.Closer)(nil), c.closers...)
c.defs = make(definitions, 0)
c.closers = c.closers[:0]
c.mux.Unlock()
for i := len(closers) - 1; i >= 0; i-- {
if err = closers[i](); err != nil {
return fmt.Errorf("unable close container : %w", err)
}
}
return nil
}
func (c *container) Has(value Type, modifiers ...Modifier) bool {
var (
rt = reflect.TypeOf(value)
defs = c.defs.find(rt, modifiers)
)
return len(defs) > 0
}
func (c *container) Resolve(target Value, modifiers ...Modifier) (err error) {
var rv = reflect.ValueOf(target)
c.mux.Lock()
err = c.resolve(&rv, cycle.New(), modifiers)
c.mux.Unlock()
if err != nil {
return fmt.Errorf("%s : %w", runtime.Caller(0), err)
}
return nil
}
func (c *container) resolve(tv *reflect.Value, cycle *cycle.Cycle, modifiers []Modifier) (err error) {
if tv.Kind() != reflect.Pointer && tv.Kind() != reflect.Slice {
if tv.IsValid() {
return NewTypeError(tv.Type(), ErrMustBeSliceOrPointer)
}
return ErrMustBeSliceOrPointer
}
var ft = tv.Type().Elem()
if reflectContainerType.AssignableTo(ft) {
c.set(tv, reflect.ValueOf(c))
return
}
var defs = c.defs.find(ft, modifiers)
if len(defs) == 0 {
if ft.Kind() == reflect.Slice {
defs = c.defs.find(ft.Elem(), modifiers)
}
if len(defs) == 0 {
return NewTypeError(tv.Type(), ErrDoesNotExist)
}
}
if ft.Kind() != reflect.Slice && len(defs) > 1 {
return NewTypeError(tv.Type(), ErrMultipleDefinitions)
}
for _, def := range defs {
if err = cycle.Add(def.id); err != nil {
return NewTypeError(tv.Type(), err)
}
if !def.unshared {
var rv = c.cache[def.id]
if rv.IsValid() {
c.set(tv, rv)
cycle.Del(def.id)
continue
}
}
var deps = def.compiler.Dependencies()
for _, dep := range deps {
if err = c.resolveDependency(cycle, dep, def.constraints); err != nil {
cycle.Del(def.id)
return err
}
}
var sv, closer, err = def.compiler.Create(deps...)
if err != nil {
cycle.Del(def.id)
return NewTypeError(def.compiler.Type(), err)
}
if !def.unshared {
c.cache[def.id] = sv
}
if closer != nil {
c.closers = append(c.closers, closer)
}
c.set(tv, sv)
cycle.Del(def.id)
}
return nil
}
func (c *container) resolveDependency(cycle *cycle.Cycle, dep *compiler.Dependency, cs constraints) error {
var v = &dep.Value
if v.CanAddr() {
v = &[]reflect.Value{v.Addr()}[0]
}
var (
constr = cs.choose(dep.Index, dep.Name, dep.Type)
err = c.resolve(v, cycle, constr.modifiers)
)
if errors.Is(err, ErrDoesNotExist) && constr.optional {
if e, ok := err.(*TypeError); ok && v.Type() != e.Type {
return err
}
return nil
}
return err
}
func (c *container) set(tv *reflect.Value, sv reflect.Value) {
switch tv.Elem().Kind() {
case reflect.Slice:
tv.Elem().Set(
reflect.Append(tv.Elem(), sv),
)
default:
tv.Elem().Set(sv)
}
}