-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcallsite.go
516 lines (414 loc) · 11.7 KB
/
callsite.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package di
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/dozm/di/errorx"
"github.com/dozm/di/syncx"
"github.com/dozm/di/util"
)
type CallSiteKind byte
const (
CallSiteKind_Factory CallSiteKind = iota
CallSiteKind_Constructor
CallSiteKind_Constant
CallSiteKind_Slice
CallSiteKind_Container
CallSiteKind_Scope
CallSiteKind_Transient
CallSiteKind_Singleton
)
type CallSite interface {
ServiceType() reflect.Type
Kind() CallSiteKind
Value() any
SetValue(any)
Cache() ResultCache
}
//
type ConstantCallSite struct {
serviceType reflect.Type
value any
}
func (cs *ConstantCallSite) Value() any {
return cs.value
}
func (cs *ConstantCallSite) SetValue(v any) {
cs.value = v
}
func (cs *ConstantCallSite) DefaultValue() any {
return cs.value
}
func (cs *ConstantCallSite) ServiceType() reflect.Type {
return cs.serviceType
}
func (cs *ConstantCallSite) Kind() CallSiteKind {
return CallSiteKind_Constant
}
func (cs *ConstantCallSite) Cache() ResultCache {
return NoneResultCache
}
func newConstantCallSite(serviceType reflect.Type, defaultValue any) *ConstantCallSite {
return &ConstantCallSite{
serviceType: serviceType,
value: defaultValue,
}
}
//Factory call site
type FactoryCallSite struct {
serviceType reflect.Type
value any
cache ResultCache
Factory Factory
}
func (cs *FactoryCallSite) Value() any {
return cs.value
}
func (cs *FactoryCallSite) SetValue(v any) {
cs.value = v
}
func (cs *FactoryCallSite) ServiceType() reflect.Type {
return cs.serviceType
}
func (cs *FactoryCallSite) Kind() CallSiteKind {
return CallSiteKind_Factory
}
func (cs *FactoryCallSite) Cache() ResultCache {
return cs.cache
}
func newFactoryCallSite(cache ResultCache, serviceType reflect.Type, factory Factory) *FactoryCallSite {
return &FactoryCallSite{
serviceType: serviceType,
cache: cache,
Factory: factory,
}
}
//
type ConstructorCallSite struct {
serviceType reflect.Type
value any
Ctor *ConstructorInfo
Parameters []CallSite
cache ResultCache
}
func (cs *ConstructorCallSite) Value() any {
return cs.value
}
func (cs *ConstructorCallSite) SetValue(v any) {
cs.value = v
}
func (cs *ConstructorCallSite) ServiceType() reflect.Type {
return cs.serviceType
}
func (cs *ConstructorCallSite) Kind() CallSiteKind {
return CallSiteKind_Constructor
}
func (cs *ConstructorCallSite) Cache() ResultCache {
return cs.cache
}
func newConstructorCallSite(cache ResultCache, serviceType reflect.Type, ctor *ConstructorInfo, parameters []CallSite) *ConstructorCallSite {
return &ConstructorCallSite{
cache: cache,
serviceType: serviceType,
Ctor: ctor,
Parameters: parameters,
}
}
//
type ContainerCallSite struct {
value any
}
func (cs *ContainerCallSite) Value() any {
return cs.value
}
func (cs *ContainerCallSite) SetValue(v any) {
cs.value = v
}
func (cs *ContainerCallSite) ServiceType() reflect.Type {
return ContainerType
}
func (cs *ContainerCallSite) Kind() CallSiteKind {
return CallSiteKind_Container
}
func (cs *ContainerCallSite) Cache() ResultCache {
return NoneResultCache
}
//
type SliceCallSite struct {
serviceType reflect.Type
Elem reflect.Type
CallSites []CallSite
cache ResultCache
value any
}
func (cs *SliceCallSite) Value() any {
return cs.value
}
func (cs *SliceCallSite) SetValue(v any) {
cs.value = v
}
func (cs *SliceCallSite) Cache() ResultCache {
return cs.cache
}
func (cs *SliceCallSite) ServiceType() reflect.Type {
return cs.serviceType
}
func (cs *SliceCallSite) Kind() CallSiteKind {
return CallSiteKind_Slice
}
func newSliceCallSite(cache ResultCache, elem reflect.Type, callSites []CallSite) *SliceCallSite {
return &SliceCallSite{
cache: cache,
Elem: elem,
CallSites: callSites,
serviceType: reflect.SliceOf(elem),
}
}
//
type chainItem struct {
Order int
Ctor *ConstructorInfo
}
type callSiteChain struct {
items map[reflect.Type]chainItem
}
func (c *callSiteChain) CheckCircularDependency(serviceType reflect.Type) error {
for k := range c.items {
if k == serviceType {
return c.createCircularDependencyError(serviceType)
}
}
return nil
}
func (c *callSiteChain) Remove(serviceType reflect.Type) {
delete(c.items, serviceType)
}
// the ctor can be nil when the serviceType is a slice
func (c *callSiteChain) Add(serviceType reflect.Type, ctor *ConstructorInfo) {
c.items[serviceType] = chainItem{
Order: len(c.items),
Ctor: ctor,
}
}
func (c *callSiteChain) createCircularDependencyError(t reflect.Type) error {
var sb strings.Builder
sb.WriteString("a circular dependency was detected for the service of type '")
sb.WriteString(t.String())
sb.WriteString("'.")
// TODO: add resolution path
return &errorx.CircularDependencyError{Message: sb.String()}
}
func newCallSiteChain() *callSiteChain {
return &callSiteChain{
items: make(map[reflect.Type]chainItem),
}
}
//
const DefaultSlot int = 0
type CallSiteFactory struct {
descriptors []*Descriptor
callSiteCache *syncx.Map[ServiceCacheKey, CallSite]
descriptorLookup map[reflect.Type]descriptorCacheItem
callSiteLockers *syncx.LockMap
}
func (f *CallSiteFactory) Descriptors() []*Descriptor {
return f.descriptors
}
func (f *CallSiteFactory) populate() {
for _, descriptor := range f.descriptors {
serviceType := descriptor.ServiceType
cacheItem := f.descriptorLookup[serviceType]
f.descriptorLookup[serviceType] = cacheItem.Add(descriptor)
}
}
func (f *CallSiteFactory) GetCallSite(serviceType reflect.Type, chain *callSiteChain) (CallSite, error) {
if site, ok := f.callSiteCache.Load(ServiceCacheKey{ServiceType: serviceType, Slot: DefaultSlot}); ok {
return site, nil
}
return f.createCallSite(serviceType, chain)
}
func (f *CallSiteFactory) GetCallSiteByDescriptor(descriptor *Descriptor, chain *callSiteChain) (CallSite, error) {
if descriptorCache, ok := f.descriptorLookup[descriptor.ServiceType]; ok {
return f.tryCreateExact(
descriptor,
chain,
descriptorCache.GetSlot(descriptor))
}
return nil, errors.New("descriptorLookup didn't contain requested descriptor")
}
func (f *CallSiteFactory) createCallSite(serviceType reflect.Type, chain *callSiteChain) (CallSite, error) {
if err := chain.CheckCircularDependency(serviceType); err != nil {
return nil, err
}
callSiteLocker := f.callSiteLockers.LoadOrCreate(serviceType)
callSiteLocker.Lock()
defer callSiteLocker.Unlock()
if descriptor, ok := f.descriptorLookup[serviceType]; ok {
return f.tryCreateExact(descriptor.Last(), chain, DefaultSlot)
}
if serviceType.Kind() == reflect.Slice {
return f.createSlice(serviceType, chain)
}
return nil, &errorx.ServiceNotFound{ServiceType: serviceType}
}
func (f *CallSiteFactory) tryCreateExact(descriptor *Descriptor, chain *callSiteChain, slot int) (CallSite, error) {
callSiteKey := ServiceCacheKey{descriptor.ServiceType, slot}
callSite, ok := f.callSiteCache.Load(callSiteKey)
if ok {
return callSite, nil
}
cache := newResultCacheWithLifetime(descriptor.Lifetime, descriptor.ServiceType, slot)
var err error
if descriptor.Instance != nil {
callSite = newConstantCallSite(descriptor.ServiceType, descriptor.Instance)
} else if descriptor.Factory != nil {
callSite = newFactoryCallSite(cache, descriptor.ServiceType, descriptor.Factory)
} else if descriptor.Ctor != nil {
callSite, err = f.createConstructorCallSite(cache, descriptor.ServiceType, descriptor.Ctor, chain)
if err != nil {
return nil, err
}
} else {
return nil, &errorx.InvalidDescriptor{ServiceType: descriptor.ServiceType}
}
f.callSiteCache.Store(callSiteKey, callSite)
return callSite, nil
}
func (f *CallSiteFactory) createConstructorCallSite(cache ResultCache, serviceType reflect.Type, ctor *ConstructorInfo, chain *callSiteChain) (*ConstructorCallSite, error) {
chain.Add(serviceType, ctor)
defer chain.Remove(serviceType)
if len(ctor.In) == 0 {
return newConstructorCallSite(cache, serviceType, ctor, nil), nil
}
parameterCallSites, err := f.createArgumentCallSites(chain, ctor)
if err != nil {
return nil, err
}
return newConstructorCallSite(cache, serviceType, ctor, parameterCallSites), nil
}
func (f *CallSiteFactory) createArgumentCallSites(chain *callSiteChain, ctor *ConstructorInfo) ([]CallSite, error) {
callSites := make([]CallSite, len(ctor.In))
for i, t := range ctor.In {
cs, err := f.GetCallSite(t, chain)
if err != nil {
return nil, err
}
callSites[i] = cs
}
return callSites, nil
}
func (f *CallSiteFactory) createSlice(serviceType reflect.Type, chain *callSiteChain) (CallSite, error) {
if serviceType.Kind() != reflect.Slice {
return nil, fmt.Errorf("service type '%v' is not slice", serviceType)
}
key := ServiceCacheKey{serviceType, DefaultSlot}
if callSite, ok := f.callSiteCache.Load(key); ok {
return callSite, nil
}
chain.Add(serviceType, nil)
defer chain.Remove(serviceType)
elementType := serviceType.Elem()
cacheLocation := CacheLocation_Root
callSites := make([]CallSite, 0)
if descriptorCache, ok := f.descriptorLookup[elementType]; ok {
num := descriptorCache.Num()
for i := 0; i < num; i++ {
cs, err := f.tryCreateExact(descriptorCache.Get(i), chain, num-i-1)
if err != nil {
return nil, err
}
cacheLocation = f.getCommonCacheLocation(cacheLocation, cs.Cache().Location)
callSites = append(callSites, cs)
}
}
resultCache := NoneResultCache
if cacheLocation == CacheLocation_Scope || cacheLocation == CacheLocation_Root {
resultCache = newResultCache(cacheLocation, key)
}
return newSliceCallSite(resultCache, elementType, util.ClipSlice(callSites)), nil
}
func (f *CallSiteFactory) Add(serviceType reflect.Type, callSite CallSite) {
f.callSiteCache.Store(ServiceCacheKey{ServiceType: serviceType, Slot: DefaultSlot}, callSite)
}
// Determines if the specified service type is available from the ServiceProvider.
func (f *CallSiteFactory) IsService(serviceType reflect.Type) bool {
if serviceType == nil {
return false
}
if _, ok := f.descriptorLookup[serviceType]; ok {
return true
}
if serviceType.Kind() == reflect.Slice {
return true
}
return serviceType == ContainerType ||
serviceType == ScopeFactoryType ||
serviceType == IsServiceType
}
func (f *CallSiteFactory) getCommonCacheLocation(locationA CacheLocation, locationB CacheLocation) CacheLocation {
if locationA > locationB {
return locationA
}
return locationB
}
func newCallSiteFactory(descriptors []*Descriptor) *CallSiteFactory {
d := make([]*Descriptor, len(descriptors))
copy(d, descriptors)
f := &CallSiteFactory{
descriptors: d,
callSiteCache: syncx.NewMap[ServiceCacheKey, CallSite](),
descriptorLookup: make(map[reflect.Type]descriptorCacheItem),
callSiteLockers: &syncx.LockMap{},
}
f.populate()
return f
}
type descriptorCacheItem struct {
item *Descriptor
items []*Descriptor
}
func (dci descriptorCacheItem) Last() *Descriptor {
if l := len(dci.items); l > 0 {
return dci.items[l-1]
}
return dci.item
}
func (dci descriptorCacheItem) Num() int {
if dci.item == nil {
return 0
}
return 1 + len(dci.items)
}
func (dci descriptorCacheItem) Get(index int) *Descriptor {
if index >= dci.Num() {
panic("index out of range")
}
if index == 0 {
return dci.item
}
return dci.items[index-1]
}
func (dci descriptorCacheItem) GetSlot(descriptor *Descriptor) int {
if descriptor == dci.item {
return dci.Num() - 1
}
if l := len(dci.items); l > 0 {
for i := range dci.items {
if descriptor == dci.items[i] {
return l - (i + 1)
}
}
}
panic(errors.New("descriptor not exist"))
}
func (dci descriptorCacheItem) Add(descriptor *Descriptor) descriptorCacheItem {
var newCacheItem descriptorCacheItem
if dci.item == nil {
newCacheItem.item = descriptor
} else {
newCacheItem.item = dci.item
newCacheItem.items = append(dci.items, descriptor)
}
return newCacheItem
}