/
container.go
482 lines (375 loc) · 11.6 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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package container
import (
"context"
"errors"
"fmt"
"github.com/procyon-projects/logy"
"github.com/procyon-projects/reflector"
"reflect"
"strings"
"sync"
)
var (
log = logy.Get()
)
type Container interface {
Start() error
DefinitionRegistry() DefinitionRegistry
SharedInstances() SharedInstances
Hooks() Hooks
Get(ctx context.Context, name string) (any, error)
GetByNameAndType(ctx context.Context, name string, typ reflector.Type) (any, error)
GetByNameAndArgs(ctx context.Context, name string, args ...any) (any, error)
GetByType(ctx context.Context, typ reflector.Type) (any, error)
GetInstancesByType(ctx context.Context, requiredType reflector.Type) ([]any, error)
Contains(name string) bool
IsShared(name string) bool
IsPrototype(name string) bool
RegisterResolvable(typ reflector.Type, object any) error
ScopeNames() []string
GetScope(scopeName string) (Scope, error)
}
type container struct {
definitionRegistry DefinitionRegistry
sharedInstances SharedInstances
hooks Hooks
scopes map[string]Scope
muScopes *sync.RWMutex
resolvableDependencies map[reflect.Type]any
muResolvableDependencies *sync.RWMutex
}
func New() Container {
return WithDefinitions(nil)
}
func WithDefinitions(definitions []*Definition) Container {
return &container{
definitionRegistry: NewDefinitionRegistry(definitions),
sharedInstances: NewSharedInstances(),
hooks: NewHooks(),
scopes: map[string]Scope{},
muScopes: &sync.RWMutex{},
resolvableDependencies: map[reflect.Type]any{},
muResolvableDependencies: &sync.RWMutex{},
}
}
func (c *container) Start() error {
ctx := contextWithHolder(context.Background())
for _, name := range c.definitionRegistry.DefinitionNames() {
definition, ok := c.definitionRegistry.Find(name)
if ok && definition.IsShared() {
_, err := c.Get(ctx, definition.Name())
if err != nil {
return err
}
}
}
return nil
}
func (c *container) DefinitionRegistry() DefinitionRegistry {
return c.definitionRegistry
}
func (c *container) SharedInstances() SharedInstances {
return c.sharedInstances
}
func (c *container) Hooks() Hooks {
return c.hooks
}
func (c *container) Get(ctx context.Context, name string) (any, error) {
return c.getInstance(ctx, name, nil)
}
func (c *container) GetByNameAndType(ctx context.Context, name string, typ reflector.Type) (any, error) {
return c.getInstance(ctx, name, typ)
}
func (c *container) GetByNameAndArgs(ctx context.Context, name string, args ...any) (any, error) {
return c.getInstance(ctx, name, nil, args...)
}
func (c *container) GetByType(ctx context.Context, typ reflector.Type) (any, error) {
return c.getInstance(ctx, "", typ)
}
func (c *container) GetInstancesByType(ctx context.Context, requiredType reflector.Type) ([]any, error) {
if requiredType == nil {
return nil, errors.New("container: requiredType cannot be nil")
}
instances, err := c.getInstances(ctx, reflector.ToSlice(reflector.TypeOf[[]any]()), requiredType)
if err != nil {
return nil, err
}
return instances.([]any), nil
}
func (c *container) Contains(name string) bool {
return c.sharedInstances.Contains(name)
}
func (c *container) IsShared(name string) bool {
def, ok := c.definitionRegistry.Find(name)
return ok && def.IsShared()
}
func (c *container) IsPrototype(name string) bool {
def, ok := c.definitionRegistry.Find(name)
return ok && def.IsPrototype()
}
func (c *container) RegisterResolvable(typ reflector.Type, object any) error {
if typ == nil {
return errors.New("container: type should be passed")
}
if object == nil {
return errors.New("container: object should be passed")
}
defer c.muResolvableDependencies.Unlock()
c.muResolvableDependencies.Lock()
c.resolvableDependencies[typ.ReflectType()] = object
return nil
}
func (c *container) RegisterScope(scopeName string, scope Scope) error {
if strings.TrimSpace(scopeName) == "" {
panic("container: scopeName cannot be empty or blank")
}
if scope == nil {
panic("container: scope cannot be nil")
}
if SharedScope != scopeName && PrototypeScope != scopeName {
defer c.muScopes.Unlock()
c.muScopes.Lock()
c.scopes[scopeName] = scope
return nil
}
return errors.New("container: cannot replace existing scopes 'shared' and 'prototype'")
}
func (c *container) ScopeNames() []string {
defer c.muScopes.Unlock()
c.muScopes.Lock()
scopeNames := make([]string, 0)
for scopeName, _ := range c.scopes {
scopeNames = append(scopeNames, scopeName)
}
return scopeNames
}
func (c *container) GetScope(scopeName string) (Scope, error) {
defer c.muScopes.Unlock()
c.muScopes.Lock()
if scope, ok := c.scopes[scopeName]; ok {
return scope, nil
}
return nil, fmt.Errorf("container: no scope registered for scope name %s", scopeName)
}
func (c *container) getInstance(ctx context.Context, name string, requiredType reflector.Type, args ...any) (any, error) {
if ctx == nil {
return nil, errors.New("container: context should be passed")
}
ctx = contextWithHolder(ctx)
if name == "" && requiredType == nil {
return nil, errors.New("container: either name or requiredType should be given")
}
if name == "" {
candidate, err := c.sharedInstances.FindByType(requiredType)
if err == nil {
return candidate, nil
}
names := c.definitionRegistry.DefinitionNamesByType(requiredType)
if len(names) == 0 {
return nil, ¬FoundError{
ErrorString: fmt.Sprintf("container: not found instance or definition with required type %s", requiredType.Name()),
}
} else if len(names) > 1 {
return nil, fmt.Errorf("container: there is more than one definition for the required type %s, it cannot be distinguished", requiredType.Name())
}
name = names[0]
}
def, ok := c.definitionRegistry.Find(name)
if !ok {
return nil, ¬FoundError{
ErrorString: fmt.Sprintf("container: not found definition with name %s", name),
}
}
if requiredType != nil && !c.match(def.Type(), requiredType) {
return nil, fmt.Errorf("container: definition type with name %s does not match the required type", name)
}
if def.IsShared() {
instance, err := c.sharedInstances.OrElseGet(name, func() (any, error) {
if log.IsDebugEnabled() {
log.D(ctx, "Creating shared instance of '{}' under package '{}'", def.Type().Name(), def.Type().PackagePath())
}
return c.createInstance(ctx, def, args)
})
return instance, err
} else if def.IsPrototype() {
prototypeHolder := holderFromContext(ctx)
err := prototypeHolder.beforeCreation(name)
if err != nil {
return nil, err
}
defer prototypeHolder.afterCreation(name)
return c.createInstance(ctx, def, args)
}
if strings.TrimSpace(def.Scope()) == "" {
return nil, fmt.Errorf("container: no scope name for required type %s", requiredType.Name())
}
scope, err := c.GetScope(def.Scope())
if err != nil {
return nil, err
}
return scope.GetObject(ctx, name, func(ctx context.Context) (any, error) {
scopeHolder := holderFromContext(ctx)
err = scopeHolder.beforeCreation(name)
if err != nil {
return nil, err
}
defer scopeHolder.afterCreation(name)
return c.createInstance(ctx, def, args)
})
}
func (c *container) match(instanceType reflector.Type, requiredType reflector.Type) bool {
if instanceType.CanConvert(requiredType) {
return true
} else if reflector.IsPointer(instanceType) && !reflector.IsPointer(requiredType) && !reflector.IsInterface(requiredType) {
ptrType := reflector.ToPointer(instanceType)
if ptrType.Elem().CanConvert(requiredType) {
return true
}
}
return false
}
func (c *container) createInstance(ctx context.Context, definition *Definition, args []any) (instance any, err error) {
newFunc := definition.constructorFunc
parameterCount := len(definition.Inputs())
if parameterCount != 0 && len(args) == 0 {
var resolvedArguments []any
resolvedArguments, err = c.resolveInputs(ctx, definition.Inputs())
if err != nil {
return nil, err
}
var results []any
results, err = newFunc.Invoke(resolvedArguments...)
if err != nil {
return nil, err
}
instance = results[0]
} else if (parameterCount == 0 && len(args) == 0) || (len(args) != 0 && parameterCount == len(args)) {
var results []any
results, err = newFunc.Invoke(args...)
if err != nil {
return nil, err
}
instance = results[0]
} else {
return nil, fmt.Errorf("container: the number of provided arguments is wrong for definition %s", definition.Name())
}
return c.initializeInstance(definition.name, instance)
}
func (c *container) getInstances(ctx context.Context, sliceType reflector.Slice, itemType reflector.Type) (any, error) {
val, err := sliceType.Instantiate()
if err != nil {
return nil, err
}
var (
instance any
items any
)
instances := c.sharedInstances.FindAllByType(itemType)
sliceType = reflector.ToSlice(reflector.ToPointer(reflector.TypeOfAny(val.Val())).Elem())
items, err = sliceType.Append(instances...)
if err != nil {
return nil, err
}
definitionNames := c.definitionRegistry.DefinitionNamesByType(itemType)
for _, definitionName := range definitionNames {
if c.sharedInstances.Contains(definitionName) {
continue
}
instance, err = c.Get(ctx, definitionName)
if err != nil {
return nil, err
}
items, err = sliceType.Append(instance)
}
return items, nil
}
func (c *container) getResolvableInstance(typ reflector.Type) (any, bool) {
defer c.muResolvableDependencies.Unlock()
c.muResolvableDependencies.Lock()
for resolvableType, instance := range c.resolvableDependencies {
if resolvableType.ConvertibleTo(typ.ReflectType()) {
return instance, true
}
}
return nil, false
}
func (c *container) resolveInputs(ctx context.Context, inputs []*Input) ([]any, error) {
arguments := make([]any, 0)
for _, input := range inputs {
if reflector.IsSlice(input.Type()) {
sliceType := reflector.ToSlice(input.Type())
instances, err := c.getInstances(ctx, sliceType, sliceType.Elem())
if err != nil {
return nil, err
}
arguments = append(arguments, instances)
continue
}
var (
instance any
err error
)
resolvableInstance, exists := c.getResolvableInstance(input.Type())
if exists {
arguments = append(arguments, resolvableInstance)
continue
}
if input.Name() != "" {
instance, err = c.Get(ctx, input.Name())
if log.IsDebugEnabled() {
log.D(ctx, "Autowiring by name")
}
} else {
instance, err = c.GetByType(ctx, input.Type())
}
if err != nil {
if notFoundErr := (*notFoundError)(nil); errors.As(err, ¬FoundErr) && !reflector.IsPointer(input.Type()) && input.Type().IsInstantiable() {
var val reflector.Value
val, err = input.Type().Instantiate()
if err != nil {
return nil, err
}
instance = val.Elem()
arguments = append(arguments, instance)
continue
}
if !input.IsOptional() && err != nil {
return nil, err
} else if input.IsOptional() && err != nil {
arguments = append(arguments, nil)
}
} else {
arguments = append(arguments, instance)
}
}
return arguments, nil
}
func (c *container) initializeInstance(name string, instance any) (any, error) {
var (
err error
)
hooks := c.Hooks().ToSlice()
for _, hook := range hooks {
if hook.OnPreInitialization != nil {
instance, err = hook.OnPreInitialization(name, instance)
}
if err != nil {
return nil, err
}
}
if postConstructor, implements := instance.(PostConstructor); implements {
err = postConstructor.PostConstruct()
if err != nil {
return nil, err
}
}
for _, hook := range hooks {
if hook.OnPostInitialization != nil {
instance, err = hook.OnPostInitialization(name, instance)
if err != nil {
return nil, err
}
}
}
return instance, nil
}