-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
container.go
259 lines (211 loc) · 4.91 KB
/
container.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
package wire
import (
"reflect"
"runtime"
"strconv"
"strings"
)
const tag = "wire"
type component struct {
id string
value reflect.Value
dependencies []dependency
declaredAt string
}
type dependency struct {
id string
name string
index int
typ reflect.Type
impl string
}
type group []component
func (gr group) find(id string) (component, bool) {
for _, c := range gr {
if c.id == id {
return c, true
}
}
return component{}, false
}
func (gr group) get(id string) component {
if c, ok := gr.find(id); ok {
return c
}
panic(idNotFoundError{id: id, component: gr[0]})
}
// Container provides an isolated container for DI.
type Container struct {
components map[reflect.Type]group
callerSkip int
}
// New create new isolated DI container.
func New() Container {
return Container{
components: make(map[reflect.Type]group),
}
}
// Connect a component, optionally identified by id.
func (container Container) Connect(val interface{}, id ...string) {
ptr := false
rv := reflect.ValueOf(val)
rt := rv.Type()
nam := ""
_, file, no, _ := runtime.Caller(container.callerSkip + 1)
if len(id) > 0 {
nam = id[0]
}
comp := component{
id: nam,
value: rv,
declaredAt: file + ":" + strconv.Itoa(no),
}
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
rv = rv.Elem()
comp.value = rv
ptr = true
}
if gr, ok := container.components[rt]; ok {
if comp, ok := gr.find(nam); ok {
panic(duplicateError{previous: comp})
}
}
if rt.Kind() != reflect.Struct {
container.components[rt] = append(container.components[rt], comp)
return
}
for i := 0; i < rt.NumField(); i++ {
sf := rt.Field(i)
// skip unexported field
if sf.Name[0] >= 'a' && sf.Name[0] <= 'z' {
continue
}
if tval, ok := sf.Tag.Lookup(tag); ok {
if tval == "-" {
continue
}
depRt := sf.Type
if depRt.Kind() == reflect.Ptr {
depRt = depRt.Elem()
}
idAndImpl := strings.Split(tval, ",")
id := idAndImpl[0]
impl := ""
if len(idAndImpl) > 1 {
impl = idAndImpl[1]
}
comp.dependencies = append(comp.dependencies, dependency{
id: id,
name: sf.Name,
index: i,
typ: depRt,
impl: impl,
})
} else if (sf.Type.Kind() == reflect.Ptr || sf.Type.Kind() == reflect.Interface) && rv.Field(i).IsNil() {
panic(tagMissingError{field: sf})
} else if sf.Type.Kind() == reflect.Struct {
// check forgotten tag only for struct.
if _, exist := container.components[sf.Type]; exist {
panic(tagForgottenError{field: sf})
}
}
}
if len(comp.dependencies) != 0 && !ptr {
panic(incompletedError{})
}
container.components[rt] = append(container.components[rt], comp)
}
// Resolve a component with identified id.
func (container Container) Resolve(out interface{}, id ...string) {
rv := reflect.ValueOf(out)
if rv.Type().Kind() != reflect.Ptr {
panic(resolveParamError{})
}
rv = rv.Elem()
rt := rv.Type()
nam := ""
if len(id) > 0 {
nam = id[0]
}
if rt.Kind() == reflect.Ptr {
// pointer inside pointer
if gr, ok := container.components[rt.Elem()]; ok {
comp := gr.get(nam)
if comp.value.CanAddr() {
rv.Set(comp.value.Addr())
return
}
panic(notAddressableError{id: nam, paramType: rt, component: comp})
}
} else {
if gr, ok := container.components[rt]; ok {
rv.Set(gr.get(nam).value)
return
}
}
panic(typeNotFoundError{paramType: rt})
}
// Apply wiring to all components.
func (container Container) Apply() {
for _, gr := range container.components {
for _, comp := range gr {
container.fill(comp)
}
}
}
func (container Container) fill(c component) {
if len(c.dependencies) == 0 {
return
}
for i := range c.dependencies {
ptrInterface := false
dep := c.dependencies[i]
cdep := component{}
if gr, ok := container.components[dep.typ]; ok {
cdep = gr.get(dep.id)
} else {
// scan if it's interface
matches := 0
if dep.typ.Kind() == reflect.Interface {
for _, gr := range container.components {
ctyp := gr[0].value.Type()
if dep.impl != "" && dep.impl != ctyp.Name() {
continue
}
if ctyp.Implements(dep.typ) {
if fcedp, ok := gr.find(dep.id); ok {
cdep = fcedp
matches++
continue
}
}
// scan pointer type
if reflect.PtrTo(ctyp).Implements(dep.typ) {
if fcedp, ok := gr.find(dep.id); ok {
ptrInterface = true
cdep = fcedp
matches++
}
}
}
}
if matches == 0 {
panic(dependencyNotFound{id: dep.id, component: c, dependency: dep})
} else if matches > 1 {
panic(ambiguousError{component: c, dependency: dep})
}
}
container.fill(cdep)
fv := c.value.Field(dep.index)
if fv.Kind() == reflect.Ptr || ptrInterface {
if !cdep.value.CanAddr() {
panic(requiresPointerError{component: c, dependency: dep, depComponent: cdep})
}
fv.Set(cdep.value.Addr())
} else {
fv.Set(cdep.value)
}
}
c.dependencies = nil
}