-
Notifications
You must be signed in to change notification settings - Fork 1
/
cronos.go
180 lines (131 loc) · 3.91 KB
/
cronos.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 cronos
import (
"fmt"
"reflect"
"runtime"
"sort"
)
type key struct {
typed reflect.Type
}
type Cronos struct {
initialized bool
cache
}
type constructor interface{}
type component interface{}
type cache struct {
components map[key]component
constructors map[key]constructor
notSingletons map[key]bool
options OptionsList
}
// New - initializes the dependency injection container
func New() Cronos {
return Cronos{
cache: cache{
components: map[key]component{},
constructors: map[key]constructor{},
options: OptionsList{},
notSingletons: map[key]bool{},
},
initialized: false,
}
}
func (cronos *Cronos) proccessOptions() {
if cronos.initialized {
return
}
sort.Sort(cronos.cache.options)
for i := 0; i < len(cronos.cache.options); i++ {
op := cronos.cache.options[i]
task := op.task
task(op.key, cronos)
}
cronos.initialized = true
}
func (cronos *Cronos) invokeConstructor(constructor constructor, args []reflect.Type) (returns []reflect.Value) {
dependencies := make([]reflect.Value, len(args))
for i := 0; i < len(dependencies); i++ {
dependencie := cronos.Fetch(args[i])
dependencies[i] = reflect.ValueOf(dependencie)
}
constructorValue := reflect.ValueOf(constructor)
returns = constructorValue.Call(dependencies)
return
}
func (cronos *Cronos) validateConstructor(valueFunc reflect.Value) error {
typed := valueFunc.Type()
if typed.Kind() != reflect.Func {
return fmt.Errorf("The %s constructor must be a function", typed.Kind().String())
}
if typed.NumOut() > 2 || typed.NumOut() < 1 {
return fmt.Errorf("The number of elements returned in the %s constructor must be a minimum of 1 and a maximum of 2", cronos.getFunctionName(valueFunc))
}
if typed.NumOut() == 2 {
errorInterface := reflect.TypeOf((*error)(nil)).Elem()
secondTypeReturn := typed.Out(1)
if !secondTypeReturn.Implements(errorInterface) {
return fmt.Errorf("The second element returned in the %s constructor must be an error", cronos.getFunctionName(valueFunc))
}
}
return nil
}
func (cronos *Cronos) getFunctionName(valueFunc reflect.Value) string {
return runtime.FuncForPC(valueFunc.Pointer()).Name()
}
func (cronos *Cronos) getArgs(constructor constructor) []reflect.Type {
typec := reflect.TypeOf(constructor)
deps := make([]reflect.Type, typec.NumIn())
for i := 0; i < len(deps); i++ {
deps[i] = typec.In(i)
}
return deps
}
// Register - register dependencies in the container
func (cronos *Cronos) Register(constructor constructor, options ...Options) {
valued := reflect.Indirect(reflect.ValueOf(constructor))
err := cronos.validateConstructor(valued)
if err != nil {
panic(err)
}
k := key{valued.Type().Out(0)}
optionsRegistered := []Options{}
for _, op := range options {
op.key = k
optionsRegistered = append(optionsRegistered, op)
}
cronos.cache.constructors[k] = constructor
cronos.cache.options = append(cronos.cache.options, optionsRegistered[0:len(options)]...)
}
// Fetch - build requested dependency
func (cronos *Cronos) Fetch(typed reflect.Type) interface{} {
key := key{typed}
if object, found := cronos.cache.components[key]; found {
return object
}
constructor := cronos.cache.constructors[key]
args := cronos.getArgs(constructor)
returns := cronos.invokeConstructor(constructor, args)
if len(returns) == 2 && !returns[1].IsNil() {
err := returns[1].Interface().(error)
panic(err)
}
object := returns[0].Interface()
isNotSingleton := cronos.cache.notSingletons[key]
if !isNotSingleton {
cronos.cache.components[key] = object
}
return object
}
// Init - initializes the dependency injection container
func (cronos *Cronos) Init(initFunc constructor) {
cronos.proccessOptions()
args := cronos.getArgs(initFunc)
cronos.invokeConstructor(initFunc, args)
}
func Lookup[T interface{}](container Cronos, typed T) T {
container.proccessOptions()
typec := reflect.TypeOf(typed)
return container.Fetch(typec).(T)
}