-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
206 lines (175 loc) · 5.09 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
package di
import (
"fmt"
"reflect"
"github.com/fluffy-bunny/fluffy-dozm-di/reflectx"
"github.com/fluffy-bunny/fluffy-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]
realizedLookupKeyServices *syncx.Map[string, ServiceAccessor]
disposed bool
callSiteValidator *CallSiteValidator
}
func (c *container) GetDescriptors() []*Descriptor {
return c.CallSiteFactory.Descriptors()
}
func (c *container) Get(serviceType reflect.Type) (any, error) {
return c.GetWithScope(serviceType, c.Root)
}
func (c *container) GetByLookupKey(serviceType reflect.Type, key string) (any, error) {
return c.GetWithScopeWithLookupKey(serviceType, key, 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) GetWithScopeWithLookupKey(serviceType reflect.Type, key string, 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)
}
}
}()
hashKey := hashTypeAndString(serviceType, key)
accessor, ok := c.realizedLookupKeyServices.Load(hashKey)
if !ok {
accessor, err = c.createServiceLookupKeyAccessor(hashKey)
if err != nil {
return
} else {
accessor, _ = c.realizedLookupKeyServices.LoadOrStore(hashKey, 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) createServiceLookupKeyAccessor(key string) (ServiceAccessor, error) {
descriptor := c.CallSiteFactory.descriptorKeyLookup[key]
itemDescriptor := descriptor.item
if len(descriptor.items) > 0 {
// get the last one
itemDescriptor = descriptor.items[len(descriptor.items)-1]
}
callSite, err := c.CallSiteFactory.GetCallSiteByDescriptor(itemDescriptor, 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) 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)
}