-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_container.go
180 lines (136 loc) · 4.08 KB
/
default_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
package simpledi
import (
"fmt"
"github.com/Nealoth/simpledi/internal/reflections"
"sort"
)
var _ IDiContainer = &defaultDiContainer{}
type defaultDiContainer struct {
initialized bool
definitions definitionsContainer
components componentsContainer
}
func (d *defaultDiContainer) Start() {
errDef := "cannot start DI container"
if !d.initialized {
panic(fmt.Sprintf("%s: container has not been initialized", errDef))
}
// --- VERIFICATION STAGE. VERIFY THAT ALL OF DEPS ARE EXIST AND THERE ARE NO CIRCULAR INJECTIONS
verificationErr := d.definitions.
verify(
verifyDefinitionsDependencies,
verifyCircularDependencyInjections,
)
if verificationErr != nil {
panic(fmt.Sprintf("%s: %s", errDef, verificationErr))
}
// --- PREPARING TO INIT LOOP
definitionsList := make([]*componentDefinition, 0, len(d.definitions))
for _, definition := range d.definitions {
definitionsList = append(definitionsList, definition)
}
sort.Slice(definitionsList, func(i, j int) bool {
return len(definitionsList[i].dependencies) < len(definitionsList[j].dependencies)
})
// --- INIT LOOP
componentsToInitLeft := len(definitionsList)
for componentsToInitLeft > 0 {
componentsInitialized := 0
for _, definition := range definitionsList {
if !definition.initialized {
// --- CHECK IF COMPONENT READY TO INIT
componentReadyToInit := true
// check deps for init ready
for _, depName := range definition.dependencies {
dep, depFound := d.definitions[depName]
// if error which dep is not found
if !depFound {
componentReadyToInit = false
panic(fmt.Sprintf("%s: something went wrong, dep '%s' of component '%s' has not found",
errDef,
depName,
definition.fullName,
))
}
// if dep not yet initialized
if !dep.initialized {
componentReadyToInit = false
continue
}
}
if !componentReadyToInit {
continue
}
// --- INIT
// pre init stage
definition.rawComponent.PreInit()
// inject stage
for injectorName, injectorFunc := range definition.injectors {
fieldsValues := reflections.GetTypeFieldsValuesByTagValue(definition.rawComponent, injectTagName, injectorName)
if err := injectorFunc(definition, fieldsValues, d.components); err != nil {
panic(fmt.Sprintf(
"%s: injection error occured. component: '%s', injector: '%s', err: %s",
errDef,
definition.fullName,
injectorName,
err,
))
}
}
// post init
definition.rawComponent.PostInit()
componentsInitialized++
componentsToInitLeft--
definition.initialized = true
d.components.AddComponent(definition.fullName, definition.rawComponent)
}
}
if componentsInitialized == 0 {
// Log initialized components and not initialized components
panic(fmt.Sprintf("%s: components initialization infinite loop",
errDef))
}
}
// --- POST INIT STAGE
// Log total initialized/not initialized
for _, def := range d.definitions {
if !def.initialized {
panic(fmt.Sprintf("%s: something went wrong: component %s has not been initialized by init loop",
errDef, def.fullName))
}
}
d.purifyDefinitions()
d.afterContainerStart()
}
func (d *defaultDiContainer) afterContainerStart() {
// TODO log after container start
for _, comp := range d.components {
comp.OnContainerReady()
}
}
func (d *defaultDiContainer) purifyDefinitions() {
for key, value := range d.definitions {
value.rawComponent = nil
delete(d.definitions, key)
}
d.definitions = nil
}
func (d *defaultDiContainer) Init() {
d.definitions = make(definitionsContainer, 0)
d.components = make(componentsContainer, 0)
d.initialized = true
}
func (d *defaultDiContainer) RegisterComponent(cmp IComponent) {
errDef := "component registration error"
if !d.initialized {
panic(fmt.Sprintf("%s: container has not been initialized", errDef))
}
if _, err := d.definitions.register(cmp); err != nil {
panic(fmt.Sprintf("%s: %s", errDef, err))
}
}
func (d *defaultDiContainer) Destroy() {
for _, component := range d.components {
component.OnDestroy()
}
}