-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.go
98 lines (79 loc) · 2.68 KB
/
global.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
// Exports the methods of a global instance to simplify the use of the lib
package di
import (
"context"
"reflect"
)
var global = New(nil)
func Global() Container {
return global
}
// Initialize initialize all non-lazy singletons (startup)
func Initialize(ctx ...context.Context) error {
return global.Initialize(ctx...)
}
func Register(ctor any, opts ...FactoryConfig) {
global.Register(ctor, opts...)
}
// Injector simplifies component registration through reflection.
//
// Example:
//
// type myController struct {
// MyService Service `inject:""`
// }
//
// di.Injected[*myController]()
//
// In the example above, the MyService dependency will be injected automatically.
func Injected[T any](opts ...FactoryConfig) {
InjectedTo[T](global, opts...)
}
func ShouldRegister(ctor any, opts ...FactoryConfig) error {
return global.ShouldRegister(ctor, opts...)
}
// RegisterScope Register the given scope, backed by the given ScopeI implementation.
func RegisterScope(name string, scope ScopeI) error {
return global.RegisterScope(name, scope)
}
// Get return an instance, which may be shared or independent, of the specified component.
func Get[T any](ctx ...context.Context) (o T, e error) {
return GetFrom[T](global, ctx...)
}
// Contains check if this container contains a component with the given key.
// Does not consider any hierarchy this container may participate in.
func Contains(key reflect.Type) bool {
return global.Contains(key)
}
func ContainsRecursive(key reflect.Type) bool {
return global.ContainsRecursive(key)
}
func Filter(options ...FactoryConfig) *FilteredFactories {
return global.Filter(options...)
}
func GetObjectFactory(factory *Factory, managed bool, ctx ...context.Context) CreateObjectFunc {
return global.GetObjectFactory(factory, managed, ctx...)
}
func GetObjectFactoryFor(key reflect.Type, managed bool, ctx ...context.Context) CreateObjectFunc {
return global.GetObjectFactoryFor(key, managed, ctx...)
}
// ResolveArgs returns an ordered list of values which may be passed directly to the Factory Create method
func ResolveArgs(factory *Factory, ctx ...context.Context) ([]reflect.Value, error) {
return global.ResolveArgs(factory, ctx...)
}
// Destroy this container
func Destroy() error {
return global.Destroy()
}
// DestroyObject destroy the given instance
func DestroyObject(key reflect.Type, object any) error {
return global.DestroyObject(key, object)
}
// DestroySingletons destroy all singleton components in this container. To be called on shutdown of a factory.
func DestroySingletons() error {
return global.DestroySingletons()
}
// Mock test only, register a mock instance to the container
func Mock(mock any) (cleanup func()) {
return global.Mock(mock)
}