-
Notifications
You must be signed in to change notification settings - Fork 2
/
component.go
executable file
·380 lines (352 loc) · 11.6 KB
/
component.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package ltick
import (
"context"
"reflect"
"strings"
"github.com/fatih/structs"
"github.com/juju/errors"
"github.com/ltick/tick-framework/database"
"github.com/ltick/tick-framework/filesystem"
"github.com/ltick/tick-framework/kvstore"
"github.com/ltick/tick-framework/logger"
"github.com/ltick/tick-framework/queue"
"github.com/ltick/tick-framework/session"
"github.com/ltick/tick-framework/utility"
)
var (
errComponentExists = "ltick: component '%s' exists"
errComponentNotExists = "ltick: component '%s' not exists"
errRegisterComponent = "ltick: register component '%s' error"
errUnregisterComponent = "ltick: unregister component '%s' error"
errInjectComponent = "ltick: inject component '%s' field '%s' error"
errInjectComponentTo = "ltick: inject component '%s' field '%s' error"
errUseComponent = "ltick: use component '%s' error"
errValueExists = "ltick: value '%s' exists"
errValueNotExists = "ltick: value '%s' not exists"
errConfigureComponentFileConfigByName = "ltick: configure component '%s' file config error"
errConfigureComponentFileConfig = "ltick: configure component '%v' file config error"
)
type ComponentState int8
const (
COMPONENT_STATE_INIT ComponentState = iota
COMPONENT_STATE_PREPARED
COMPONENT_STATE_INITIATED
COMPONENT_STATE_STARTUP
COMPONENT_STATE_SHUTDOWN
)
type ComponentInterface interface {
Prepare(ctx context.Context) (context.Context, error)
Initiate(ctx context.Context) (context.Context, error)
OnStartup(ctx context.Context) (context.Context, error)
OnShutdown(ctx context.Context) (context.Context, error)
}
type Components []*Component
func (cs Components) Get(name string) *Component {
for _, c := range cs {
canonicalName := canonicalName(name)
if canonicalName == c.Name {
return c
}
}
return nil
}
type Component struct {
Name string
Component ComponentInterface
ConfigurePath string
Dependencies []*Component
}
var (
OptionalComponents = Components{
&Component{Name: "Log", Component: &log.Logger{}, ConfigurePath: "components.log"},
&Component{Name: "Database", Component: &database.Database{}, ConfigurePath: "components.database"},
&Component{Name: "Kvstore", Component: &kvstore.Kvstore{}, ConfigurePath: "components.kvstore"},
&Component{Name: "Queue", Component: &queue.Queue{}, ConfigurePath: "components.queue"},
&Component{Name: "Filesystem", Component: &filesystem.Filesystem{}, ConfigurePath: "components.filesystem"},
&Component{Name: "Session", Component: &session.Session{}, ConfigurePath: "components.session"},
}
)
/**************** Component ****************/
func (r *Registry) UseComponent(componentNames ...string) error {
var err error
components := make([]*Component, 0)
for _, componentName := range componentNames {
canonicalComponentName := canonicalName(componentName)
component := OptionalComponents.Get(canonicalComponentName)
if component != nil {
components = append(components, component)
err = r.RegisterComponent(component, true)
if err != nil {
return errors.Annotatef(err, errUseComponent, component.Name)
}
} else {
return errors.Annotatef(err, errComponentNotExists, canonicalComponentName)
}
}
sortedComponents := SortComponent(components)
for _, name := range sortedComponents {
component, err := r.GetComponentByName(name)
if err != nil {
return errors.Annotatef(err, errUseComponent, name)
}
err = r.InjectComponentTo([]interface{}{component})
if err != nil {
return errors.Annotatef(err, errUseComponent, name)
}
}
return nil
}
// Register As Component
func (r *Registry) RegisterComponent(component *Component, ignoreIfExistses ...bool) error {
if _, ok := r.ComponentStates[component.Name]; !ok {
r.ComponentStates[component.Name] = COMPONENT_STATE_INIT
}
canonicalName := canonicalName(component.Name)
ignoreIfExists := false
if len(ignoreIfExistses) > 0 {
ignoreIfExists = ignoreIfExistses[0]
}
if _, ok := r.ComponentMap[canonicalName]; ok {
if !ignoreIfExists {
return errors.Errorf(errComponentExists, canonicalName)
}
err := r.UnregisterComponent(canonicalName)
if err != nil {
return errors.Annotatef(err, errRegisterComponent+": %s", canonicalName)
}
}
r.SortedComponentName = append(r.SortedComponentName, canonicalName)
r.Components = append(r.Components, component)
r.ComponentMap[canonicalName] = component
return nil
}
// Unregister As Component
func (r *Registry) UnregisterComponent(componentNames ...string) error {
if len(componentNames) > 0 {
for _, componentName := range componentNames {
canonicalComponentName := canonicalName(componentName)
// r.ComponentMap
delete(r.ComponentMap, canonicalComponentName)
// r.SortedComponentName
for index, sortedComponentName := range r.SortedComponentName {
if canonicalComponentName == sortedComponentName {
r.SortedComponentName = append(r.SortedComponentName[:index], r.SortedComponentName[index+1:]...)
}
}
// r.Components
for index, c := range r.Components {
if canonicalComponentName == c.Name {
r.Components = append(r.Components[:index], r.Components[index+1:]...)
}
}
}
}
return nil
}
func (r *Registry) GetComponentMap() map[string]*Component {
return r.ComponentMap
}
func (r *Registry) GetComponentByName(name string) (*Component, error) {
name = canonicalName(name)
if _, ok := r.ComponentMap[name]; !ok {
return nil, errors.Errorf(errComponentNotExists, name)
}
return r.ComponentMap[name], nil
}
func (r *Registry) GetSortedComponentName() []string {
return r.SortedComponentName
}
func (r *Registry) GetSortedComponents(reverses ...bool) []*Component {
components := make([]*Component, len(r.Components))
if len(r.SortedComponentName) > 0 {
index := 0
reverse := false
if len(reverses) > 0 {
reverse = reverses[0]
}
if reverse {
for i := len(r.SortedComponentName) - 1; i >= 0; i-- {
if component, ok := r.ComponentMap[r.SortedComponentName[i]]; ok {
components[index] = component
index++
}
}
} else {
for i := 0; i < len(r.SortedComponentName); i++ {
if component, ok := r.ComponentMap[r.SortedComponentName[i]]; ok {
components[index] = component
index++
}
}
}
}
return components
}
// Register As Value
func (r *Registry) RegisterValue(key string, value interface{}, forceOverwrites ...bool) error {
key = canonicalName(key)
forceOverwrite := false
if len(forceOverwrites) > 0 {
forceOverwrite = forceOverwrites[0]
}
if _, ok := r.Values[key]; ok && !forceOverwrite {
return errors.Errorf(errValueExists, key)
}
r.Values[key] = value
return nil
}
// Unregister As Value
func (r *Registry) UnregisterValue(keys ...string) error {
if len(keys) > 0 {
for _, key := range keys {
key = canonicalName(key)
if _, ok := r.Values[key]; !ok {
return errors.Errorf(errValueNotExists, key)
}
delete(r.Values, key)
}
}
return nil
}
func (r *Registry) GetValue(key string) (interface{}, error) {
key = canonicalName(key)
if _, ok := r.Values[key]; !ok {
return nil, errors.Errorf(errValueNotExists, key)
}
return r.Values[key], nil
}
func (r *Registry) GetValues() map[string]interface{} {
return r.Values
}
func (r *Registry) InjectComponent() error {
components := r.GetSortedComponents()
injectTargets := make([]interface{}, len(components))
for i, c := range components {
injectTargets[i] = c.Component
}
return r.InjectComponentTo(injectTargets)
}
func (r *Registry) InjectComponentByName(componentNames []string) error {
componentMap := r.GetComponentMap()
injectTargets := make([]interface{}, 0)
for _, componentName := range componentNames {
canonicalComponentName := canonicalName(componentName)
if injectTarget, ok := componentMap[canonicalComponentName]; ok {
injectTargets = append(injectTargets, injectTarget.Component)
}
}
return r.InjectComponentTo(injectTargets)
}
func (r *Registry) InjectComponentTo(injectTargets []interface{}) error {
for _, injectTarget := range injectTargets {
injectTargetValue := reflect.ValueOf(injectTarget)
for injectTargetValue.Kind() == reflect.Ptr {
injectTargetValue = injectTargetValue.Elem()
}
if injectTargetValue.Kind() != reflect.Struct {
continue
}
s := structs.New(injectTarget)
componentType := reflect.TypeOf((*ComponentInterface)(nil)).Elem()
for _, f := range s.Fields() {
if f.IsExported() && f.Tag(INJECT_TAG) == "true" {
if reflect.TypeOf(f.Value()).Implements(componentType) {
if _, ok := f.Value().(ComponentInterface); ok {
fieldInjected := false
if _, ok := r.ComponentMap[f.Name()]; ok {
err := f.Set(r.ComponentMap[f.Name()].Component)
if err != nil {
return errors.Annotatef(err, errInjectComponentTo, injectTargetValue.String(), f.Name())
}
fieldInjected = true
}
if !fieldInjected {
return errors.Errorf(errInjectComponentTo+": component or key not exists", injectTargetValue.String(), f.Name())
}
}
}
if _, ok := r.Values[f.Name()]; ok {
err := f.Set(r.Values[f.Name()])
if err != nil {
return errors.Annotatef(err, errInjectComponentTo, injectTargetValue.String(), f.Name())
}
}
}
}
}
return nil
}
/******** Component Dependency Manage ********/
// SortComponent - Sort user components.
func SortComponent(components []*Component) []string {
// 初始化依赖关系
for _, c := range components {
if c.Dependencies == nil {
c.Dependencies = make([]*Component, 0)
}
s := structs.New(c.Component)
componentType := reflect.TypeOf((*ComponentInterface)(nil)).Elem()
for _, f := range s.Fields() {
if f.IsExported() && f.Tag(INJECT_TAG) == "true" {
if reflect.TypeOf(f.Value()).Implements(componentType) {
if dc, ok := f.Value().(ComponentInterface); ok {
c.Dependencies = append(c.Dependencies, &Component{
Name: f.Name(),
Component: dc,
})
}
}
}
}
}
// root components
roots := []*Component{}
rootDependenciesCount := 0
for {
for _, c := range components {
if len(c.Dependencies) == rootDependenciesCount {
roots = append(roots, c)
}
}
if len(roots) > 0 {
break
} else {
rootDependenciesCount++
}
}
sortedComponents := make([]string, 0)
return sortComponent(components, roots, sortedComponents)
}
// sortComponent
func sortComponent(components []*Component, currentComponents []*Component, sortedComponents []string) []string {
if components != nil && currentComponents != nil && sortedComponents != nil {
// 没有下级依赖
if len(currentComponents) == 0 {
return sortedComponents
}
for _, currentComponent := range currentComponents {
// 当前层级组件
index := utility.InArrayString(currentComponent.Name, sortedComponents, false)
if index == nil {
sortedComponents = append(sortedComponents, currentComponent.Name)
} else {
sortedComponents = append(sortedComponents[:*index], append(sortedComponents[*index+1:], sortedComponents[*index])...)
}
// 依赖当前级别组件的组件
componentsNextLevel := make([]*Component, 0)
for _, component := range components {
for _, componentDependencie := range component.Dependencies {
if strings.Compare(componentDependencie.Name, currentComponent.Name) == 0 {
componentsNextLevel = append(componentsNextLevel, component)
}
}
}
sortedComponents = sortComponent(components, componentsNextLevel, sortedComponents)
}
}
return sortedComponents
}
/******** Common Function ********/
func canonicalName(name string) string {
return strings.ToUpper(name[0:1]) + name[1:]
}