-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
di.go
144 lines (106 loc) · 3.12 KB
/
di.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
package do
import (
"fmt"
)
func Provide[T any](i *Injector, provider Provider[T]) {
name := generateServiceName[T]()
ProvideNamed[T](i, name, provider)
}
func ProvideNamed[T any](i *Injector, name string, provider Provider[T]) {
_i := getInjectorOrDefault(i)
if _i.exists(name) {
panic(fmt.Errorf("DI: service `%s` has already been declared", name))
}
service := newServiceLazy(name, provider)
_i.set(name, service)
_i.logf("service %s injected", name)
}
func ProvideValue[T any](i *Injector, value T) {
name := generateServiceName[T]()
ProvideNamedValue[T](i, name, value)
}
func ProvideNamedValue[T any](i *Injector, name string, value T) {
_i := getInjectorOrDefault(i)
if _i.exists(name) {
panic(fmt.Errorf("DI: service `%s` has already been declared", name))
}
service := newServiceEager(name, value)
_i.set(name, service)
_i.logf("service %s injected", name)
}
func Override[T any](i *Injector, provider Provider[T]) {
name := generateServiceName[T]()
OverrideNamed[T](i, name, provider)
}
func OverrideNamed[T any](i *Injector, name string, provider Provider[T]) {
_i := getInjectorOrDefault(i)
service := newServiceLazy(name, provider)
_i.set(name, service)
_i.logf("service %s overridden", name)
}
func OverrideValue[T any](i *Injector, value T) {
name := generateServiceName[T]()
OverrideNamedValue[T](i, name, value)
}
func OverrideNamedValue[T any](i *Injector, name string, value T) {
_i := getInjectorOrDefault(i)
service := newServiceEager(name, value)
_i.set(name, service)
_i.logf("service %s overridden", name)
}
func Invoke[T any](i *Injector) (T, error) {
name := generateServiceName[T]()
return InvokeNamed[T](i, name)
}
func MustInvoke[T any](i *Injector) T {
s, err := Invoke[T](i)
must(err)
return s
}
func InvokeNamed[T any](i *Injector, name string) (T, error) {
return invokeImplem[T](i, name)
}
func MustInvokeNamed[T any](i *Injector, name string) T {
s, err := InvokeNamed[T](i, name)
must(err)
return s
}
func invokeImplem[T any](i *Injector, name string) (T, error) {
_i := getInjectorOrDefault(i)
serviceAny, ok := _i.get(name)
if !ok {
return empty[T](), _i.serviceNotFound(name)
}
service, ok := serviceAny.(Service[T])
if !ok {
return empty[T](), _i.serviceNotFound(name)
}
instance, err := service.getInstance(_i)
if err != nil {
return empty[T](), err
}
_i.onServiceInvoke(name)
_i.logf("service %s invoked", name)
return instance, nil
}
func HealthCheck[T any](i *Injector) error {
name := generateServiceName[T]()
return getInjectorOrDefault(i).healthcheckImplem(name)
}
func HealthCheckNamed(i *Injector, name string) error {
return getInjectorOrDefault(i).healthcheckImplem(name)
}
func Shutdown[T any](i *Injector) error {
name := generateServiceName[T]()
return getInjectorOrDefault(i).shutdownImplem(name)
}
func MustShutdown[T any](i *Injector) {
name := generateServiceName[T]()
must(getInjectorOrDefault(i).shutdownImplem(name))
}
func ShutdownNamed(i *Injector, name string) error {
return getInjectorOrDefault(i).shutdownImplem(name)
}
func MustShutdownNamed(i *Injector, name string) {
must(getInjectorOrDefault(i).shutdownImplem(name))
}