-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject.go
More file actions
616 lines (522 loc) · 16.1 KB
/
Copy pathinject.go
File metadata and controls
616 lines (522 loc) · 16.1 KB
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package inject
import (
"cmp"
"context"
"maps"
"reflect"
"slices"
"strconv"
"strings"
"sync"
"unsafe"
"github.com/pkg/errors"
"golang.org/x/sync/singleflight"
)
var (
ErrTypeNotProvided = errors.New("type not provided")
ErrTypeAlreadyProvided = errors.New("type already provided")
ErrParentAlreadySet = errors.New("parent already set")
ErrTypeNotAllowed = errors.New("type not allowed")
ErrCircularDependency = errors.New("circular dependency detected")
ErrErrorTypeMustBeLast = errors.New("error type must be the last return value")
ErrInvalidProvider = errors.New("provide only accepts a function that returns at least one type except error")
ErrInvalidInvokeTarget = errors.New("invoke only accepts a function")
ErrInvalidApplyTarget = errors.New("apply only accepts a struct")
)
type ctxKeyDependencyPath struct{}
type dependencyPath []typeKey
func (dp dependencyPath) String() string {
if len(dp) == 0 {
return ""
}
var parts []string
for _, k := range dp {
s := k.rt.String()
if k.index > 0 {
s += "[" + strconv.Itoa(k.index) + "]"
}
parts = append(parts, s)
}
return strings.Join(parts, " -> ")
}
func getDependencyPath(ctx context.Context) dependencyPath {
path, _ := ctx.Value(ctxKeyDependencyPath{}).(dependencyPath)
return path
}
func appendDependencyToPath(ctx context.Context, key typeKey) context.Context {
path := getDependencyPath(ctx)
newPath := append(dependencyPath{}, path...)
newPath = append(newPath, key)
return context.WithValue(ctx, ctxKeyDependencyPath{}, newPath)
}
var (
typeError = reflect.TypeOf((*error)(nil)).Elem()
typeContext = reflect.TypeOf((*context.Context)(nil)).Elem()
typeElementVoid = reflect.TypeOf((*Element[*Void])(nil))
)
// IsTypeAllowed checks if a type is allowed as a function input or output parameter.
func IsTypeAllowed(typ reflect.Type) bool {
return typ != typeContext && typ != typeError
}
// IsInputTypeAllowed checks if a type is allowed as a function input parameter.
// Element[T] types are not allowed as input - use Slice[T] instead.
func IsInputTypeAllowed(typ reflect.Type) bool {
if !IsTypeAllowed(typ) {
return false
}
return !isElementType(typ)
}
// IsOutputTypeAllowed checks if a type is allowed as a function output.
// Slice[T] types are not allowed as output - use Element[T] instead.
func IsOutputTypeAllowed(typ reflect.Type) bool {
if !IsTypeAllowed(typ) {
return false
}
return !isSliceType(typ)
}
type provider struct {
seq uint64
ctor any // value func
}
// typeKey is used as a map key to support multiple providers for Element[T] types.
// For normal types, index is always 0.
// For Element[T] types, index increments for each provider.
type typeKey struct {
rt reflect.Type
index int
}
type Injector struct {
mu sync.RWMutex
values map[typeKey]reflect.Value
providers map[typeKey]*provider
parent *Injector
// elementCounts tracks the count of providers for each Element[T] type
elementCounts map[reflect.Type]int
maxProviderSeq uint64
sfg singleflight.Group
}
func New() *Injector {
inj := &Injector{
values: map[typeKey]reflect.Value{},
providers: map[typeKey]*provider{},
elementCounts: map[reflect.Type]int{},
maxProviderSeq: 0,
}
_ = inj.Provide(func() *Injector { return inj })
return inj
}
func (inj *Injector) SetParent(parent *Injector) error {
inj.mu.Lock()
defer inj.mu.Unlock()
if inj.parent != nil {
return errors.WithStack(ErrParentAlreadySet)
}
inj.parent = parent
return nil
}
func (inj *Injector) unsafeProvide(ctor any) error {
rv := reflect.ValueOf(ctor)
rt := rv.Type()
if rt.Kind() != reflect.Func {
return errors.Wrap(ErrInvalidProvider, "ctor is not a function")
}
// Get valid output types with error position validation
outputTypes, err := getValidOutputTypes(rt)
if err != nil {
return err
}
p := &provider{
seq: inj.maxProviderSeq,
ctor: ctor,
}
inj.maxProviderSeq++
// Collect all keys for cycle detection
var keysToCheck []typeKey
for _, outType := range outputTypes {
// Check if this is *Element[T] type - allow multiple registrations with incrementing index
if isElementType(outType) {
idx := inj.elementCounts[outType]
key := typeKey{rt: outType, index: idx}
inj.providers[key] = p
inj.elementCounts[outType] = idx + 1
keysToCheck = append(keysToCheck, key)
continue
}
// Normal types: index is always 0
key := typeKey{rt: outType, index: 0}
if _, ok := inj.values[key]; ok {
return errors.Wrap(ErrTypeAlreadyProvided, outType.String())
}
if _, ok := inj.providers[key]; ok {
return errors.Wrap(ErrTypeAlreadyProvided, outType.String())
}
inj.providers[key] = p
keysToCheck = append(keysToCheck, key)
}
// Cycle detection for all types (including Element types)
for _, key := range keysToCheck {
if err := inj.unsafeDFSDetectCycle(key); err != nil {
return err
}
}
return nil
}
func (inj *Injector) invoke(ctx context.Context, f any) ([]reflect.Value, error) {
rt := reflect.TypeOf(f)
if rt.Kind() != reflect.Func {
return nil, errors.Wrap(ErrInvalidInvokeTarget, "f is not a function")
}
numIn := rt.NumIn()
in := make([]reflect.Value, numIn)
for i := 0; i < numIn; i++ {
argType := rt.In(i)
if argType == typeContext {
in[i] = reflect.ValueOf(ctx)
continue
}
if !IsInputTypeAllowed(argType) {
return nil, errors.Wrapf(ErrTypeNotAllowed, "arg %d: %s", i, argType.String())
}
argValue, err := inj.resolve(ctx, typeKey{rt: argType, index: 0})
if err != nil {
return nil, err
}
in[i] = argValue
}
outs := reflect.ValueOf(f).Call(in)
// apply if possible
for _, out := range outs {
unwrapped := unwrapPtr(out)
if unwrapped.Kind() == reflect.Struct {
if err := inj.applyStruct(ctx, unwrapped); err != nil {
return nil, err
}
}
}
numOut := len(outs)
if numOut > 0 && rt.Out(numOut-1) == typeError {
rvErr := outs[numOut-1]
outs = outs[:numOut-1]
if !rvErr.IsNil() {
return outs, rvErr.Interface().(error)
}
}
// If no outputs (void function), return *Element[*Void]
if len(outs) == 0 {
outs = append(outs, reflect.ValueOf(NewVoidElement()))
}
return outs, nil
}
func (inj *Injector) resolve(ctx context.Context, key typeKey) (reflect.Value, error) {
originalCtx := ctx
ctx = appendDependencyToPath(ctx, key)
// Check if this is a Slice[T] type that should be resolved from Element[T] providers
if isSliceType(key.rt) {
return inj.resolveSlice(ctx, key.rt, getSliceInnerType(key.rt))
}
inj.mu.RLock()
rv := inj.values[key]
if rv.IsValid() {
inj.mu.RUnlock()
return rv, nil
}
p, ok := inj.providers[key]
parent := inj.parent
inj.mu.RUnlock()
if ok {
// ensure that the provider is only executed once same time
_, err, _ := inj.sfg.Do(strconv.FormatUint(p.seq, 10), func() (any, error) {
// must recheck the provider, because it may be deleted by prev inj.sfg.Do
inj.mu.RLock()
_, ok := inj.providers[key]
inj.mu.RUnlock()
if !ok {
return nil, nil
}
results, err := inj.invoke(ctx, p.ctor)
if err != nil {
return nil, err
}
// Find all keys that share this provider (for Element types that return multiple values)
inj.mu.Lock()
keysForProvider := make(map[reflect.Type][]typeKey)
for k, prov := range inj.providers {
if prov.seq == p.seq {
keysForProvider[k.rt] = append(keysForProvider[k.rt], k)
}
}
// Process results: assign each result to its corresponding key
typeCounts := make(map[reflect.Type]int)
for _, result := range results {
resultType := result.Type()
keys := keysForProvider[resultType]
idx := typeCounts[resultType]
if idx >= len(keys) {
inj.mu.Unlock()
return nil, errors.Errorf("provider seq %d returned more values of type %v than registered", p.seq, resultType)
}
inj.values[keys[idx]] = result
delete(inj.providers, keys[idx])
typeCounts[resultType]++
}
inj.mu.Unlock()
return nil, nil
})
if err != nil {
return rv, err
}
return inj.resolve(ctx, key)
}
if parent == nil {
return rv, errors.Wrapf(ErrTypeNotProvided, "dependency path: %s", getDependencyPath(ctx).String())
}
return parent.resolve(originalCtx, key)
}
// resolveSlice resolves a Slice[T] type by collecting all *Element[T] values
func (inj *Injector) resolveSlice(ctx context.Context, sliceType reflect.Type, innerType reflect.Type) (reflect.Value, error) {
// sliceType is Slice[T] which is []T
// Create the result slice
result := reflect.MakeSlice(sliceType, 0, 0)
// Collect from parent first (if any)
if inj.parent != nil {
parentResult, err := inj.parent.resolveSlice(ctx, sliceType, innerType)
if err != nil && !errors.Is(err, ErrTypeNotProvided) {
return reflect.Value{}, err
}
if err == nil && parentResult.Len() > 0 {
result = reflect.AppendSlice(result, parentResult)
}
}
// Find *Element[T] type (all Element types are normalized to pointer)
var elementType reflect.Type
inj.mu.RLock()
for et := range inj.elementCounts {
if inner := getElementInnerType(et); inner != nil && inner == innerType {
elementType = et
break
}
}
count := 0
if elementType != nil {
count = inj.elementCounts[elementType]
}
inj.mu.RUnlock()
if count == 0 && result.Len() == 0 {
return reflect.Value{}, errors.Wrapf(ErrTypeNotProvided, "dependency path: %s", getDependencyPath(ctx).String())
}
// Resolve each *Element[T] by calling resolve directly
for i := 0; i < count; i++ {
key := typeKey{rt: elementType, index: i}
rv, err := inj.resolve(ctx, key)
if err != nil {
return reflect.Value{}, errors.Wrapf(err, "failed to resolve element %d for %s", i, elementType.String())
}
if rv.IsValid() {
// rv is *Element[T], dereference to get Value
if rv.IsNil() {
continue
}
elem := rv.Elem()
valueField := elem.FieldByName("Value")
if valueField.IsValid() && valueField.Type() == innerType {
result = reflect.Append(result, valueField)
}
}
}
return result, nil
}
func unwrapPtr(rv reflect.Value) reflect.Value {
for rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
return rv
}
func (inj *Injector) Apply(val any) error {
return inj.ApplyContext(context.Background(), val)
}
func (inj *Injector) ApplyContext(ctx context.Context, val any) error {
rv := unwrapPtr(reflect.ValueOf(val))
if rv.Kind() != reflect.Struct {
return errors.Wrap(ErrInvalidApplyTarget, "val is not a struct")
}
return inj.applyStruct(ctx, rv)
}
var (
TagName = "inject"
TagValueOptional = "optional"
)
func (inj *Injector) applyStruct(ctx context.Context, rv reflect.Value) error {
rt := rv.Type()
for i := 0; i < rv.NumField(); i++ {
structField := rt.Field(i)
if tag, ok := structField.Tag.Lookup(TagName); ok {
if !IsInputTypeAllowed(structField.Type) {
return errors.Wrapf(ErrTypeNotAllowed, "field %d: %s", i, structField.Type.String())
}
dep, err := inj.resolve(ctx, typeKey{rt: structField.Type, index: 0})
if err != nil {
if errors.Is(err, ErrTypeNotProvided) {
if strings.TrimSpace(tag) == TagValueOptional {
continue
}
}
return err
}
field := rv.Field(i)
if !field.CanSet() {
// If the field is unexported, we need to create a new field that is settable.
field = reflect.NewAt(structField.Type, unsafe.Pointer(field.UnsafeAddr())).Elem()
}
field.Set(dep)
}
}
return nil
}
// flatten recursively flattens any arrays/slices found in the arguments.
// This allows grouping related constructors together for better organization.
func flatten(vs ...any) []any {
var result []any
for _, f := range vs {
rv := reflect.ValueOf(f)
// If it's a slice or array, recursively flatten it
if rv.Kind() == reflect.Slice || rv.Kind() == reflect.Array {
// Convert slice/array elements to []any
subItems := make([]any, rv.Len())
for i := 0; i < rv.Len(); i++ {
subItems[i] = rv.Index(i).Interface()
}
// Recursively flatten the sub-items
result = append(result, flatten(subItems...)...)
} else {
// Regular constructor function, add directly
result = append(result, f)
}
}
return result
}
func (inj *Injector) Provide(ctors ...any) (xerr error) {
ctors = flatten(ctors...)
inj.mu.Lock()
defer inj.mu.Unlock()
originalProviders := maps.Clone(inj.providers)
originalElementCounts := maps.Clone(inj.elementCounts)
originalMaxSeq := inj.maxProviderSeq
defer func() {
if xerr != nil {
inj.providers = originalProviders
inj.elementCounts = originalElementCounts
inj.maxProviderSeq = originalMaxSeq
}
}()
for _, ctor := range ctors {
if err := inj.unsafeProvide(ctor); err != nil {
return err
}
}
return nil
}
func (inj *Injector) Invoke(f any) ([]any, error) {
return inj.InvokeContext(context.Background(), f)
}
func (inj *Injector) InvokeContext(ctx context.Context, f any) ([]any, error) {
results, err := inj.invoke(ctx, f)
if err != nil {
return nil, err
}
out := make([]any, len(results))
for i, result := range results {
out[i] = result.Interface()
}
return out, nil
}
func (inj *Injector) Resolve(refs ...any) error {
return inj.ResolveContext(context.Background(), refs...)
}
func (inj *Injector) ResolveContext(ctx context.Context, refs ...any) error {
for _, ref := range refs {
refType := reflect.TypeOf(ref)
if refType == nil || refType.Kind() != reflect.Ptr {
return errors.Errorf("resolve requires pointer arguments, got %T", ref)
}
rv, err := inj.resolve(ctx, typeKey{rt: refType.Elem(), index: 0})
if err != nil {
return err
}
reflect.ValueOf(ref).Elem().Set(rv)
}
return nil
}
// Build automatically resolves all provided types using background context.
// This will trigger the creation of all registered constructors,
// ensuring that all dependencies are properly instantiated.
func (inj *Injector) Build(ctors ...any) error {
return inj.BuildContext(context.Background(), ctors...)
}
// BuildContext automatically resolves all provided types.
// This will trigger the creation of all registered constructors,
// ensuring that all dependencies are properly instantiated.
func (inj *Injector) BuildContext(ctx context.Context, ctors ...any) error {
if len(ctors) > 0 {
if err := inj.Provide(ctors...); err != nil {
return err
}
}
inj.mu.RLock()
var keysToResolve []typeKey
keyToSeq := make(map[typeKey]uint64)
for key, p := range inj.providers {
keysToResolve = append(keysToResolve, key)
keyToSeq[key] = p.seq
}
inj.mu.RUnlock()
// Sort keys by their provider sequence for stable order based on registration sequence
slices.SortFunc(keysToResolve, func(a, b typeKey) int {
return cmp.Compare(keyToSeq[a], keyToSeq[b])
})
for _, key := range keysToResolve {
if _, err := inj.resolve(ctx, key); err != nil {
return err
}
}
return nil
}
// getValidOutputTypes extracts all valid output types from a constructor function type,
// performing error type position validation and filtering out error types.
// For Element[T] types, duplicates are allowed (same type can appear multiple times).
// For normal types, duplicates are deduplicated.
// If the function has no return values (or only returns error), it returns *Element[*Void].
func getValidOutputTypes(rt reflect.Type) ([]reflect.Type, error) {
if rt.Kind() != reflect.Func {
return nil, nil
}
var outputTypes []reflect.Type
seen := make(map[reflect.Type]bool) // Use map for deduplication of normal types
numOut := rt.NumOut()
for i := 0; i < numOut; i++ {
outType := rt.Out(i)
// Validate error type position: error can only be the last return value
if outType == typeError {
if i != numOut-1 {
return nil, errors.Wrapf(ErrErrorTypeMustBeLast, "error type found at position %d, but must be at position %d", i, numOut-1)
}
// Skip error type if it is the last return value
continue
}
if !IsOutputTypeAllowed(outType) {
return nil, errors.Wrapf(ErrTypeNotAllowed, "out %d: %s", i, outType.String())
}
// Element types: allow duplicates (each gets its own index)
// Normal types: deduplicate
if isElementType(outType) {
outputTypes = append(outputTypes, outType)
} else if !seen[outType] {
outputTypes = append(outputTypes, outType)
seen[outType] = true
}
}
// If no valid output types, treat as returning *Element[*Void]
if len(outputTypes) == 0 {
outputTypes = append(outputTypes, typeElementVoid)
}
return outputTypes, nil
}