-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontainer.go
135 lines (111 loc) · 3.04 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
package di
import (
"fmt"
"reflect"
"github.com/dozm/di/reflectx"
"github.com/dozm/di/syncx"
)
var ContainerType = reflectx.TypeOf[Container]()
var ContainerImplType = reflectx.TypeOf[container]()
var ScopeFactoryType = reflectx.TypeOf[ScopeFactory]()
var IsServiceType = reflectx.TypeOf[IsService]()
// Container options.
type Options struct {
ValidateScopes bool
ValidateOnBuild bool
}
// Get default container options.
func DefaultOptions() Options {
return Options{}
}
// Container implementation
type container struct {
Root *ContainerEngineScope
CallSiteFactory *CallSiteFactory
engine ContainerEngine
realizedServices *syncx.Map[reflect.Type, ServiceAccessor]
disposed bool
callSiteValidator *CallSiteValidator
}
func (c *container) Get(serviceType reflect.Type) (any, error) {
return c.GetWithScope(serviceType, c.Root)
}
func (c *container) CreateScope() Scope {
if c.disposed {
panic(fmt.Errorf("%v disposed", reflect.TypeOf(c).Elem()))
}
return newEngineScope(c, false)
}
func (c *container) GetWithScope(serviceType reflect.Type, scope *ContainerEngineScope) (result any, err error) {
if c.disposed {
err = fmt.Errorf("%v disposed", reflect.TypeOf(c).Elem())
return
}
defer func() {
if p := recover(); p != nil {
if e, ok := p.(error); ok {
err = e
} else {
err = fmt.Errorf("%v", p)
}
}
}()
accessor, ok := c.realizedServices.Load(serviceType)
if !ok {
accessor, err = c.createServiceAccessor(serviceType)
if err != nil {
return
} else {
accessor, _ = c.realizedServices.LoadOrStore(serviceType, accessor)
}
}
if c.callSiteValidator != nil {
err := c.callSiteValidator.ValidateResolution(serviceType, scope, c.Root)
if err != nil {
return nil, err
}
}
return accessor(scope)
}
func (c *container) validateService(d *Descriptor) error {
callSite, err := c.CallSiteFactory.GetCallSiteByDescriptor(d, newCallSiteChain())
if err != nil {
return err
}
if c.callSiteValidator != nil {
return c.callSiteValidator.ValidateCallSite(callSite)
}
return nil
}
func (c *container) Dispose() {
c.disposed = true
c.Root.Dispose()
}
func (c *container) IsDisposed() bool {
return c.disposed
}
func (c *container) createEngine() ContainerEngine {
return newContainerEngine(c)
}
func (c *container) createServiceAccessor(serviceType reflect.Type) (ServiceAccessor, error) {
callSite, err := c.CallSiteFactory.GetCallSite(serviceType, newCallSiteChain())
if err != nil {
return nil, err
}
if c.callSiteValidator != nil {
if err := c.callSiteValidator.ValidateCallSite(callSite); err != nil {
return nil, err
}
}
if callSite.Cache().Location == CacheLocation_Root {
value, err := CallSiteResolverInstance.Resolve(callSite, c.Root)
if err != nil {
return nil, err
}
return func(scope *ContainerEngineScope) (any, error) { return value, nil }, nil
}
return c.engine.RealizeService(callSite)
}
func (c *container) ReplaceServiceAccessor(callSite CallSite, accessor ServiceAccessor) {
c.realizedServices.Store(callSite.ServiceType(), accessor)
}