forked from samber/do
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdi.go
181 lines (137 loc) · 4.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
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
package di
import (
"fmt"
)
func Provide[T any](i *Container, provider Provider[T]) {
name := generateServiceName[T]()
ProvideNamed[T](i, name, provider)
}
func ProvideNamed[T any](i *Container, name string, provider Provider[T]) {
_i := getContainerOrDefault(i)
if _i.exists(name) {
panic(fmt.Errorf("DI: service `%s` has already been declared", name))
}
providerFn := toProviderFn[T](provider)
service := newServiceLazy(name, providerFn)
_i.set(name, service)
_i.logf("service %s injected", name)
}
func ProvideValue[T any](i *Container, value T) {
name := generateServiceName[T]()
ProvideNamedValue[T](i, name, value)
}
func ProvideNamedValue[T any](i *Container, name string, value T) {
_i := getContainerOrDefault(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 *Container, provider Provider[T]) {
name := generateServiceName[T]()
OverrideNamed[T](i, name, provider)
}
func OverrideNamed[T any](i *Container, name string, provider Provider[T]) {
_i := getContainerOrDefault(i)
providerFn := toProviderFn[T](provider)
service := newServiceLazy(name, providerFn)
_i.set(name, service)
_i.logf("service %s overridden", name)
}
func OverrideValue[T any](i *Container, value T) {
name := generateServiceName[T]()
OverrideNamedValue[T](i, name, value)
}
func OverrideNamedValue[T any](i *Container, name string, value T) {
_i := getContainerOrDefault(i)
service := newServiceEager(name, value)
_i.set(name, service)
_i.logf("service %s overridden", name)
}
func Invoke[T any](i *Container) (T, error) {
name := generateServiceName[T]()
return InvokeNamed[T](i, name)
}
func MustInvoke[T any](i *Container) T {
s, err := Invoke[T](i)
must(err)
return s
}
func InvokeNamed[T any](i *Container, name string) (T, error) {
return invokeImplem[T](i, name, "")
}
func MustInvokeNamed[T any](i *Container, name string) T {
s, err := InvokeNamed[T](i, name)
must(err)
return s
}
func invokeByName(serviceName string, i *Container, dependencyName string, fallbackName string) (interface{}, error) {
d, err := invokeImplem[any](i, dependencyName, fallbackName)
if err != nil {
return nil, fmt.Errorf("Failed to inject dependencies to service %s: %w", serviceName, err)
}
return d, nil
}
func invokeImplem[T any](i *Container, name string, fallbackName string) (T, error) {
_i := getContainerOrDefault(i)
var serviceAny any
var ok bool
serviceAny, ok = _i.get(name)
if !ok {
fallbackNames := []string{
// if name is not found, try to find by pointer name
fmt.Sprintf("*%s", name),
}
if fallbackName != "" {
fallbackNames = append(fallbackNames, fallbackName)
}
for _, fallbackName := range fallbackNames {
serviceAny, ok = _i.get(fallbackName)
if ok {
break
}
}
if !ok {
return empty[T](), _i.serviceNotFound(
fmt.Sprintf("name: %s, fallbackName:%s", name, fallbackName),
)
}
}
service, ok := serviceAny.(Service)
if !ok {
return empty[T](), _i.serviceNotFound(name)
}
instanceAny, err := service.getInstance(_i)
if err != nil {
return empty[T](), err
}
_i.onServiceInvoke(name)
if instance, ok := instanceAny.(T); ok {
_i.logf("service %s invoked", name)
return instance, nil
}
panic(fmt.Errorf("DI: service `%s` is not of type `%T`", name, empty[T]()))
}
func HealthCheck[T any](i *Container) error {
name := generateServiceName[T]()
return getContainerOrDefault(i).healthcheckImplem(name)
}
func HealthCheckNamed(i *Container, name string) error {
return getContainerOrDefault(i).healthcheckImplem(name)
}
func Shutdown[T any](i *Container) error {
name := generateServiceName[T]()
return getContainerOrDefault(i).shutdownImplem(name)
}
func MustShutdown[T any](i *Container) {
name := generateServiceName[T]()
must(getContainerOrDefault(i).shutdownImplem(name))
}
func ShutdownNamed(i *Container, name string) error {
return getContainerOrDefault(i).shutdownImplem(name)
}
func MustShutdownNamed(i *Container, name string) {
must(getContainerOrDefault(i).shutdownImplem(name))
}