This repository was archived by the owner on May 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontainer.go
194 lines (157 loc) · 5.24 KB
/
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package foundation
import (
"github.com/confetti-framework/contract/inter"
"github.com/confetti-framework/errors"
"github.com/confetti-framework/support"
"reflect"
)
type Container struct {
// The container created at boot time
bootContainer inter.Container
// A key value store. Callbacks are not automatically executed and stored.
bindings inter.Bindings
// A key value store. If the value is a callback, it will be executed
// once per request and the result will be saved.
singletons inter.Bindings
}
func NewContainer() *Container {
containerStruct := Container{}
containerStruct.bindings = make(inter.Bindings)
containerStruct.singletons = make(inter.Bindings)
return &containerStruct
}
func NewContainerByBoot(bootContainer inter.Container) inter.Container {
container := NewContainer()
container.bootContainer = bootContainer
return container
}
func NewTestApp(decorator func(container inter.Container) inter.Container) inter.App {
app := NewApp()
container := inter.Container(NewContainer())
container = decorator(container)
app.SetContainer(NewContainerByBoot(container))
return app
}
// Determine if the given abstract type has been bound.
func (c *Container) Bound(abstract string) bool {
_, bound := c.bindings[abstract]
_, boundSingleton := c.singletons[abstract]
return bound || boundSingleton
}
// Register a binding with the container.
func (c *Container) Bind(abstract interface{}, concrete interface{}) {
abstractString := support.Name(abstract)
c.bindings[abstractString] = concrete
}
// Register a shared binding in the container.
func (c *Container) Singleton(abstract interface{}, concrete interface{}) {
abstractString := support.Name(abstract)
c.singletons[abstractString] = concrete
}
// Register an existing instance as shared in the container without an abstract
func (c *Container) Instance(concrete interface{}) interface{} {
c.Bind(concrete, concrete)
return concrete
}
// GetE the container's bindings.
func (c *Container) Bindings() inter.Bindings {
result := inter.Bindings{}
if c.bootContainer != nil {
result = c.bootContainer.Bindings()
}
for abstract, concrete := range c.singletons {
result[abstract] = concrete
}
for abstract, concrete := range c.bindings {
result[abstract] = concrete
}
return result
}
// MakeE the given type from the container.
func (c *Container) Make(abstract interface{}) interface{} {
concrete, err := c.MakeE(abstract)
if nil != err {
panic(err)
}
return concrete
}
// MakeE the given type from the container.
func (c *Container) MakeE(abstract interface{}) (interface{}, error) {
var concrete interface{}
var err error = nil
var abstractName = support.Name(abstract)
kind := support.Kind(abstract)
if support.Kind(abstract) == reflect.Ptr && abstract == nil {
return nil, errors.New("can't resolve interface. To resolve an interface, " +
"use the following syntax: (*interface)(nil), use a string or use the struct itself")
}
if object, present := c.bindings[abstractName]; present {
concrete = object
} else if object, present := c.singletons[abstractName]; present {
concrete, err = c.getConcreteBinding(concrete, object, abstractName)
} else if c.bootContainer != nil && c.bootContainer.Bound(abstractName) {
// Check the container that was created at boot time
concrete, err = c.bootContainer.MakeE(abstract)
c.bindings[abstractName] = concrete
} else if kind == reflect.Struct {
// If struct cannot be found, we simply have to use the struct itself.
concrete = abstract
} else if kind == reflect.String {
var instances support.Map
instances, err = support.NewMapE(c.bindings)
if err == nil {
var value support.Value
if c.bootContainer != nil {
bootBindings := c.bootContainer.Bindings()
bootInstances, err := support.NewMapE(bootBindings)
if err != nil {
return nil, err
}
instances.Merge(bootInstances)
}
value, err = instances.GetE(abstract.(string))
//goland:noinspection GoNilness
concrete = value.Raw()
}
}
if err != nil {
err = errors.Wrap(err, "get instance '%s' from container", abstractName)
}
resolvePointerValue(abstract, concrete)
return concrete, err
}
func (c *Container) getConcreteBinding(
concrete interface{},
object interface{},
abstractName string,
) (interface{}, error) {
// If abstract is bound, use that object.
concrete = object
value := reflect.ValueOf(concrete)
// If concrete is a callback, run it and save the result.
if value.Kind() == reflect.Func {
if value.Type().NumIn() != 0 {
return nil, errors.WithStack(CanNotInstantiateCallbackWithParameters)
}
concrete = value.Call([]reflect.Value{})[0].Interface()
}
// Don't save result in bootContainer. We don't want to share the result across multiple requests
if c.bootContainer != nil {
c.bindings[abstractName] = concrete
}
return concrete, nil
}
// Extend an abstract type in the container.
func (c *Container) Extend(abstract interface{}, function func(service interface{}) interface{}) {
concrete := c.Make(abstract)
newConcrete := function(concrete)
c.Bind(abstract, newConcrete)
}
func resolvePointerValue(abstract interface{}, concrete interface{}) {
if support.Kind(abstract) == reflect.Ptr {
of := reflect.ValueOf(abstract)
if !of.IsNil() {
of.Elem().Set(reflect.ValueOf(concrete))
}
}
}