-
Notifications
You must be signed in to change notification settings - Fork 3
/
registry.go
304 lines (279 loc) · 7.38 KB
/
registry.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
// Copyright 2018 ekino. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package godim
import (
"fmt"
"reflect"
"sort"
"strings"
)
const (
defaultInject = "inject"
defaultConfig = "config"
defaultPriority = 0
)
// Registry the internal registry
type Registry struct {
inject string
config string
appProfile *AppProfile
values map[string]map[string]*holder
tags map[reflect.Type]*TagConfig
inits map[int]map[reflect.Type]reflect.Value
closers map[reflect.Type]reflect.Value
eventSwitch *EventSwitch
}
type holder struct {
o interface{}
typ reflect.Type
prio int
}
// TagConfig internal configuration tag
type TagConfig struct {
configs map[string]string
injects map[string]string
}
func newRegistry() *Registry {
return &Registry{
inject: defaultInject,
config: defaultConfig,
appProfile: newAppProfile(),
values: make(map[string]map[string]*holder),
tags: make(map[reflect.Type]*TagConfig),
inits: make(map[int]map[reflect.Type]reflect.Value),
closers: make(map[reflect.Type]reflect.Value),
}
}
func newRegistryFromConfig(config *Config) *Registry {
r := &Registry{
inject: config.injectString,
config: config.configString,
appProfile: config.appProfile,
values: make(map[string]map[string]*holder),
tags: make(map[reflect.Type]*TagConfig),
inits: make(map[int]map[reflect.Type]reflect.Value),
closers: make(map[reflect.Type]reflect.Value),
}
if config.activateES {
r.eventSwitch = config.eventSwitch
}
return r
}
func (registry *Registry) declare(label string, o interface{}) error {
typ := reflect.TypeOf(o)
if typ.Kind() == reflect.Ptr {
// in case of a Ptr to interface
typ = reflect.ValueOf(o).Elem().Type()
}
if !registry.appProfile.validate(label) {
return newError(fmt.Errorf(" %s is not a declared profile", label)).SetErrType(ErrTypeRegistry)
}
v, ok := registry.values[label]
if !ok {
v = make(map[string]*holder)
registry.values[label] = v
}
key := getKey(typ, o)
prio := getPriority(typ, o)
_, ok = v[key]
if ok {
return newError(fmt.Errorf(" %s already defined in registry", o)).SetErrType(ErrTypeRegistry)
}
v[key] = &holder{o: o, typ: typ, prio: prio}
err := registry.declareTags(typ, label)
if err != nil {
return err
}
return registry.declareInterfaces(o, typ)
}
var (
initType = reflect.TypeOf((*Initializer)(nil)).Elem()
closeType = reflect.TypeOf((*Closer)(nil)).Elem()
keyType = reflect.TypeOf((*Identifier)(nil)).Elem()
prioType = reflect.TypeOf((*Prioritizer)(nil)).Elem()
emitType = reflect.TypeOf((*Emitter)(nil)).Elem()
recType = reflect.TypeOf((*EventReceiver)(nil)).Elem()
interceptType = reflect.TypeOf((*EventInterceptor)(nil)).Elem()
finalizerType = reflect.TypeOf((*EventFinalizer)(nil)).Elem()
)
func getKey(typ reflect.Type, o interface{}) string {
ptyp := reflect.PtrTo(typ)
ok := typ.Implements(keyType)
if ptyp.Implements(keyType) || ok {
return reflect.ValueOf(o).MethodByName("Key").Call([]reflect.Value{})[0].Interface().(string)
}
return strings.Split(typ.String(), ".")[1]
}
func getPriority(typ reflect.Type, o interface{}) int {
ptyp := reflect.PtrTo(typ)
ok := typ.Implements(prioType)
if ptyp.Implements(prioType) || ok {
return reflect.ValueOf(o).MethodByName("Priority").Call([]reflect.Value{})[0].Interface().(int)
}
return defaultPriority
}
func (registry *Registry) declareInterfaces(o interface{}, typ reflect.Type) error {
prio := getPriority(typ, o)
_, exists := registry.inits[prio][typ]
if exists {
return newError(fmt.Errorf("OnInit Method already declared for type %s", typ)).SetErrType(ErrTypeRegistry)
}
ptyp := reflect.PtrTo(typ)
if ptyp.Implements(initType) {
_, ok := registry.inits[prio]
if !ok {
registry.inits[prio] = make(map[reflect.Type]reflect.Value)
}
registry.inits[prio][typ] = reflect.ValueOf(o).MethodByName("OnInit")
}
if ptyp.Implements(closeType) {
registry.closers[typ] = reflect.ValueOf(o).MethodByName("OnClose")
}
if registry.eventSwitch != nil {
if ptyp.Implements(emitType) {
registry.eventSwitch.AddEmitter(o.(Emitter))
}
if ptyp.Implements(recType) {
registry.eventSwitch.AddReceiver(o.(EventReceiver))
}
if ptyp.Implements(interceptType) {
registry.eventSwitch.AddInterceptor(o.(EventInterceptor))
}
if ptyp.Implements(finalizerType) {
registry.eventSwitch.WithEventFinalizer(o.(EventFinalizer))
}
}
return nil
}
func (registry *Registry) declareTags(typ reflect.Type, label string) error {
tc := registry.getTagConfig(typ)
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tag := field.Tag
ctag := tag.Get(registry.config)
if len(ctag) > 0 {
tc.configs[field.Name] = ctag
}
itag := typ.Field(i).Tag.Get(registry.inject)
if len(itag) > 0 {
_, err := registry.appProfile.validateTag(label, itag)
if err != nil {
return newError(err).SetErrType(ErrTypeRegistry)
}
tc.injects[typ.Field(i).Name] = itag
}
}
return nil
}
func (registry *Registry) getTagConfig(typ reflect.Type) *TagConfig {
tc := registry.tags[typ]
if tc == nil {
tc = &TagConfig{
configs: make(map[string]string),
injects: make(map[string]string),
}
registry.tags[typ] = tc
}
return tc
}
func (registry *Registry) configure(f func(key string, val reflect.Value) (interface{}, error)) error {
for _, mv := range registry.values {
if mv == nil {
continue
}
for _, h := range mv {
typ := h.typ
tc := registry.tags[typ]
if tc == nil {
continue
}
elem := reflect.ValueOf(h.o).Elem()
for fieldname, key := range tc.configs {
err := setFieldOnValue(elem, fieldname, key, f)
if err != nil {
return err
}
}
}
}
return nil
}
func setFieldOnValue(v reflect.Value, fieldname, key string, f func(key string, val reflect.Value) (interface{}, error)) error {
field := v.FieldByName(fieldname)
toSet, err := f(key, field)
if err != nil {
return err
}
field.Set(reflect.ValueOf(toSet))
return nil
}
func (registry *Registry) injection() error {
for _, mv := range registry.values {
if mv == nil {
continue
}
for _, h := range mv {
typ := h.typ
tc := registry.tags[typ]
if tc == nil {
continue
}
elem := reflect.ValueOf(h.o).Elem()
for fieldname, key := range tc.injects {
elts := strings.Split(key, ":")
toInject := registry.getElement(elts[0], elts[1])
if toInject != nil {
elem.FieldByName(fieldname).Set(reflect.ValueOf(toInject))
}
}
}
}
return nil
}
func (registry *Registry) getElement(label, key string) interface{} {
m := registry.values[label]
if m == nil {
return nil
}
for k, h := range m {
if key == k {
return h.o
}
}
return nil
}
func (registry *Registry) initializeAll() error {
// Sort by priority
priorities := make([]int, len(registry.inits))
i := 0
for k := range registry.inits {
priorities[i] = k
i++
}
sort.Ints(priorities)
for _, p := range priorities {
for _, m := range registry.inits[p] {
ret := m.Call([]reflect.Value{})
if len(ret) > 0 {
err := ret[0]
if !err.IsNil() {
return err.Interface().(error)
}
}
}
}
return nil
}
func (registry *Registry) closeAll() error {
for _, m := range registry.closers {
ret := m.Call([]reflect.Value{})
if len(ret) > 0 {
err := ret[0]
if !err.IsNil() {
return err.Interface().(error)
}
}
}
return nil
}