-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminidic.go
256 lines (209 loc) · 6.53 KB
/
minidic.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
package minidic
import (
"fmt"
"reflect"
"regexp"
)
// Public API
type Injection interface {
InjectionId() string
WithInjectedDependencies(injectionsIds []string) Injection
MarkAsFactory() Injection
MarkAsProtected() Injection
}
type Container interface {
Add(newInjection Injection)
Get(injectionId string) interface{}
GetWithoutPanic(injectionId string) (interface{}, error)
Has(injectionId string) bool
Del(injectionId string) error
Extend(injectionId string, function interface{})
}
type injection struct {
injectionId string
value interface{}
dependenciesIds []string
asFactory bool
protected bool
}
// Injection implementation
func NewInjection(injectionId string, value interface{}) Injection {
return &injection{injectionId: injectionId, value: value}
}
func (i *injection) MarkAsFactory() Injection {
i.asFactory = true
return i
}
func (i *injection) MarkAsProtected() Injection {
i.protected = true
return i
}
func (i *injection) WithInjectedDependencies(injectionsIds []string) Injection {
i.dependenciesIds = injectionsIds
return i
}
func (i *injection) InjectionId() string {
return i.injectionId
}
type container struct {
injections map[string]*injection
functionsResultsCache map[string]*interface{}
}
// Container implementation
func NewContainer() Container {
c := new(container)
c.injections = make(map[string]*injection)
c.functionsResultsCache = make(map[string]*interface{})
return c
}
func (c *container) Add(newInjection Injection) {
if underlyingInjection, ok := newInjection.(*injection); ok {
c.injections[underlyingInjection.injectionId] = underlyingInjection
} else {
panic(fmt.Sprintf("'Container.Add()' argument must be an *injection, got %s", newInjection))
}
}
func (c *container) Get(injectionId string) interface{} {
value, err := c.GetWithoutPanic(injectionId)
if err != nil {
panic(err)
}
return value
}
func (c *container) GetWithoutPanic(injectionId string) (interface{}, error) {
if value, ok := c.functionsResultsCache[injectionId]; ok {
return *value, nil
}
injection, exists := c.injections[injectionId]
if !exists {
return nil, UnknownInjectionIdError{InjectionId: injectionId}
}
value := injection.value
if isFunction(value) && !injection.protected {
var result interface{}
var err error
if injection.dependenciesIds != nil {
result, err = triggerFunctionWithInjectedIds(injectionId, c, value, injection.dependenciesIds)
} else {
result, err = triggerFunctionWithContainer(injectionId, c, value)
}
if err != nil {
return nil, err
}
if !injection.asFactory {
c.functionsResultsCache[injectionId] = &result
}
return result, nil
}
return value, nil
}
func (c *container) Has(injectionId string) bool {
_, ok := c.injections[injectionId]
return ok
}
func (c *container) Del(injectionId string) error {
_, ok := c.injections[injectionId]
if !ok {
return UnknownInjectionIdError{InjectionId: injectionId}
}
delete(c.injections, injectionId)
return nil
}
func (c *container) Extend(injectionId string, function interface{}) {
extendedInjection, ok := c.injections[injectionId]
if !ok {
panic(UnknownInjectionIdError{InjectionId: injectionId})
}
extendedInjectionFunction := extendedInjection.value
if !isFunction(extendedInjectionFunction) {
panic(ExtendedServiceIsNotAFunctionError{ExtendedInjectionId: injectionId})
}
if !isFunction(function) {
panic(ServiceExtensionIsNotAFunctionError{ExtendedInjectionId: injectionId})
}
extendedInjection.value = func(c *container) interface{} {
decoratedInjectionResult, err := triggerFunctionWithContainer(injectionId, c, extendedInjectionFunction)
if err != nil {
panic(err)
}
result, err := triggerFunctionWithContainer(injectionId, c, function, decoratedInjectionResult)
if err != nil {
panic(err)
}
return result
}
c.injections[injectionId] = extendedInjection
}
// Errors
type UnknownInjectionIdError struct {
InjectionId string
}
func (e UnknownInjectionIdError) Error() string {
return fmt.Sprintf("Unknown injection id '%s'", e.InjectionId)
}
type ExtendedServiceIsNotAFunctionError struct {
ExtendedInjectionId string
}
func (e ExtendedServiceIsNotAFunctionError) Error() string {
return fmt.Sprintf("Extended injection id '%s' is not mapped to a function", e.ExtendedInjectionId)
}
type ServiceExtensionIsNotAFunctionError struct {
ExtendedInjectionId string
}
func (e ServiceExtensionIsNotAFunctionError) Error() string {
return fmt.Sprintf("Service extending injection id '%s' is not a function", e.ExtendedInjectionId)
}
type ServiceFunctionFirstArgumentMustBeAContainerError struct {
InjectionId string
FirstArgumentType string
}
func (e ServiceFunctionFirstArgumentMustBeAContainerError) Error() string {
return fmt.Sprintf("Service '%s' function first argument must be a pointer to the container, got ", e.InjectionId, e.FirstArgumentType)
}
// Internal utils
func isFunction(value interface{}) bool {
return reflect.TypeOf(value).Kind() == reflect.Func
}
func triggerFunctionWithContainer(injectionId string, c *container, function interface{}, args ...interface{}) (result interface{}, err error) {
functionReflection := reflect.ValueOf(function)
functionReflectionType := functionReflection.Type()
if functionReflectionType.NumIn() < 1 {
err = ServiceFunctionFirstArgumentMustBeAContainerError{injectionId, ""}
return
}
functionArgs := []reflect.Value{reflect.ValueOf(c)}
for i := 0; i < len(args); i++ {
functionArgs = append(functionArgs, reflect.ValueOf(args[i]))
}
defer func() {
callErr := recover()
if callErr == nil {
return
}
if errString, ok := callErr.(string); ok {
re := regexp.MustCompile("reflect: Call using \\*minidic.container as type (\\w+)")
if match := re.FindStringSubmatch(errString); match != nil {
err = ServiceFunctionFirstArgumentMustBeAContainerError{injectionId, match[1]}
return
}
}
panic(callErr)
}()
result = functionReflection.Call(functionArgs)[0].Interface()
return
}
func triggerFunctionWithInjectedIds(injectionId string, c *container, function interface{}, dependenciesIds []string) (result interface{}, err error) {
functionReflection := reflect.ValueOf(function)
functionArgs := []reflect.Value{}
for i := 0; i < len(dependenciesIds); i++ {
dependencyId := dependenciesIds[i]
var resolvedDependency interface{}
resolvedDependency, err = c.GetWithoutPanic(dependencyId)
if err != nil {
return
}
functionArgs = append(functionArgs, reflect.ValueOf(resolvedDependency))
}
result = functionReflection.Call(functionArgs)[0].Interface()
return
}