-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinding.go
308 lines (275 loc) · 8.87 KB
/
binding.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
package genjector
import "fmt"
// bindingSource is a concrete implementation for BindingSource interface.
type bindingSource[T any] struct {
binding Binding
keySource baseKeySource[T]
}
// Binding returns containing instance of Binding interface. Initially it makes
// the concrete instance, to check if instance matches desired type of Binding.
//
// It respects BindingSource interface.
func (s *bindingSource[T]) Binding() (Binding, error) {
instance, err := s.binding.Instance(false)
if err != nil {
return nil, err
}
if _, ok := instance.(T); !ok {
var initial T
return nil, fmt.Errorf(`binding is not possible for "%v" and "%v"`, initial, instance)
}
return s.binding, nil
}
// Key executes the same method from inner KeyOption instance.
//
// It respects BindingSource interface.
func (s *bindingSource[T]) Key() Key {
return s.keySource.Key()
}
// Initializable represents any struct that contains a method Init.
// When such struct as defined AsPointer or AsValue, method Init will be
// called during initialization process.
type Initializable interface {
Init()
}
// valueBinding is a concrete implementation for Binding interface.
type valueBinding[S any] struct{}
// Instance delivers the value of the concrete instance of type S.
// If the pointer to the struct respects Initializable interface,
// Init method will be called.
//
// It respects Binding interface.
func (valueBinding[S]) Instance(initialize bool) (interface{}, error) {
initial := *new(S)
var instance interface{} = &initial
if !initialize {
return initial, nil
}
if value, ok := instance.(Initializable); ok {
value.Init()
}
return initial, nil
}
// AsValue delivers a BindingSource for a type T, by binding a value of a struct
// to the concrete interface (or the struct itself). It must be only used with value and
// not pointer. In case pointer is used, code will return a nil value for the instance.
//
// Example:
// err := genjector.Bind(genjector.AsValue[ValueInterface, ValueStruct]())
//
// BindingSource can be only used as the first argument to Bind method.
func AsValue[T any, S any]() BindingSource[T] {
return &bindingSource[T]{
binding: valueBinding[S]{},
keySource: baseKeySource[T]{},
}
}
// pointerBinding is a concrete implementation for Binding interface.
type pointerBinding[R any] struct{}
// Instance delivers the pointer of the concrete instance of type S.
// If the struct respects Initializable interface, Init method will be called.
//
// It respects Binding interface.
func (pointerBinding[R]) Instance(initialize bool) (interface{}, error) {
var instance interface{} = new(R)
if !initialize {
return instance, nil
}
if value, ok := instance.(Initializable); ok {
value.Init()
}
return instance, nil
}
// AsPointer delivers a BindingSource for a type T, by binding pointer of a struct
// to the concrete interface (or the struct itself). It must be only used with pointers and
// not values. In case values is used, code will panic.
//
// Example:
// err := genjector.Bind(genjector.AsPointer[PointerInterface, *PointerStruct]())
//
// BindingSource can be only used as the first argument to Bind method.
func AsPointer[T any, S *R, R any]() BindingSource[T] {
return &bindingSource[T]{
binding: pointerBinding[R]{},
keySource: baseKeySource[T]{},
}
}
// ProviderMethod defines a type of a method that should delivers
// an instance od type S. This method acts as an constructor method
// and it is executed at the time of NewInstance method.
//
// It respects Binding interface.
type ProviderMethod[S any] func() (S, error)
// Instance delivers the concrete instance of type S, by executing
// root ProviderMethod itself.
//
// It respects Binding interface.
func (s ProviderMethod[S]) Instance(bool) (interface{}, error) {
return s()
}
// AsProvider delivers a BindingSource for a type T, by defining a ProviderMethod
// (or constructor method) for the new instance of some interface (or a struct).
//
// Example:
//
// err := genjector.Bind(genjector.AsProvider[ProviderInterface](func() (*ProviderStruct, error) {
// return &ProviderStruct{
// value: "value provided inside the ProviderMethod",
// }, nil
// }))
//
// BindingSource can be only used as the first argument to Bind method.
func AsProvider[T any, S any](provider ProviderMethod[S]) BindingSource[T] {
return &bindingSource[T]{
binding: provider,
keySource: baseKeySource[T]{},
}
}
// instanceBinding is a concrete implementation for Binding interface.
type instanceBinding[S any] struct {
instance S
}
// Instance delivers the concrete instance of type S, by returning already
// initialized instance that instanceBinding holds.
//
// It respects Binding interface.
func (s *instanceBinding[S]) Instance(bool) (interface{}, error) {
return s.instance, nil
}
// AsInstance delivers a BindingSource for a type T, by using a concrete
// instance that is passed as an argument to AsInstance method, to returns
// that instance whenever it is required from Binding.
//
// Example:
//
// err := genjector.Bind(genjector.AsInstance[*InstanceStruct](&InstanceStruct{
// value: "value provided in concrete instance",
// }))
//
// BindingSource can be only used as the first argument to Bind method.
func AsInstance[T any, S any](instance S) BindingSource[T] {
return &bindingSource[T]{
binding: &instanceBinding[S]{
instance: instance,
},
keySource: baseKeySource[T]{},
}
}
// bindingOption is a concrete implementation for BindingOption interface.
type bindingOption struct {
bindingFunc func(binding Binding) (Binding, error)
keyOption KeyOption
}
// Binding executes the inner bindingFunc method.
//
// It respects BindingOption interface.
func (b *bindingOption) Binding(binding Binding) (Binding, error) {
return b.bindingFunc(binding)
}
// Key executes the same method from inner KeyOption instance.
//
// It respects BindingOption interface.
func (b *bindingOption) Key(key Key) Key {
return b.keyOption.Key(key)
}
// Container executes the same method from inner KeyOption instance.
//
// It respects BindingOption interface.
func (b *bindingOption) Container(container Container) Container {
return b.keyOption.Container(container)
}
// singletonBinding is a concrete implementation for Binding interface.
type singletonBinding struct {
parent Binding
singleton interface{}
initialized bool
}
// Instance delivers already stored instance, which should be present if this
// method was already executed before. Otherwise it retrieves the instance from
// a child Binding and stores it internally for the next calls.
//
// It respects Binding interface.
func (b *singletonBinding) Instance(initialize bool) (interface{}, error) {
if b.initialized {
return b.singleton, nil
}
instance, err := b.parent.Instance(initialize)
if err != nil {
return nil, err
}
b.initialized = true
b.singleton = instance
return instance, nil
}
// AsSingleton delivers a BindingOption that defines the instance of desired
// Binding as a singleton. That means only first time the Init method (or ProviderMethod)
// will be called, and every next time the same instance will be delivered
// as a result of NewInstance method.
//
// Example:
// err := genjector.Bind(
//
// genjector.AsPointer[SingletonInterface, *SingletonStruct](),
// genjector.AsSingleton(),
//
// )
//
// AsSingleton should be only used as a BindingOption for Bind method, as it
// does not affect functionality if it is used in NewInstance method.
func AsSingleton() BindingOption {
return &bindingOption{
bindingFunc: func(binding Binding) (Binding, error) {
return &singletonBinding{
parent: binding,
}, nil
},
keyOption: sameKeyOption{},
}
}
// WithAnnotation delivers a BindingOption that allows to name specific Binding
// with any annotation desired.
//
// Example:
// err = genjector.Bind(
//
// genjector.AsPointer[AnnotationInterface, *AnnotationStruct](),
// genjector.WithAnnotation("first"),
//
// )
//
// To properly use a customer Container, WithAnnotation should be used in both
// Bind and NewInstance methods.
func WithAnnotation(annotation string) BindingOption {
return &bindingOption{
bindingFunc: func(binding Binding) (Binding, error) {
return binding, nil
},
keyOption: &annotatedKeyOption{
annotation: annotation,
},
}
}
// WithContainer delivers a BindingOption that overrides the usage of standard
// internal (global) Container. It allows to provide a fresh, a custom instance
// of Container, that can be made from NewContainer method.
//
// Example:
// err := genjector.Bind(
//
// genjector.AsPointer[ContainerInterface, *ContainerStruct](),
// genjector.WithContainer(customContainer),
//
// )
//
// To properly use a customer Container, WithContainer should be used in both
// Bind and NewInstance methods.
func WithContainer(container Container) BindingOption {
return &bindingOption{
bindingFunc: func(binding Binding) (Binding, error) {
return binding, nil
},
keyOption: &containerKeyOption{
container: container,
},
}
}