-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain.go
244 lines (214 loc) · 6.26 KB
/
chain.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
package genjector
import "fmt"
// sliceBinding is a concrete implementation for Binding interface.
type sliceBinding[T any] struct {
previous *sliceBinding[T]
current Binding
}
// Instance returns a slice of T types by executing current Binding and
// all other preceding ones. First it places previous in a slice, and
// then stores the instance of the current.
//
// It respects Binding interface.
func (b *sliceBinding[T]) Instance(initialize bool) (interface{}, error) {
var result []T
if initialize && b.previous != nil {
instance, err := b.previous.Instance(initialize)
if err != nil {
return nil, err
}
transformed, ok := instance.([]T)
if !ok {
return nil, fmt.Errorf(`binding is not possible for "%v" and "%v"`, result, instance)
}
result = append(result, transformed...)
}
instance, err := b.current.Instance(initialize)
if err != nil {
return nil, err
}
transformed, ok := instance.(T)
if !ok {
return nil, fmt.Errorf(`binding is not possible for "%v" and "%v"`, result, instance)
}
result = append(result, transformed)
return result, err
}
// sliceBindingSource is a concrete implementation for BindingSource interface.
type sliceBindingSource[T any] struct {
previous Binding
source BindingSource[T]
keySource KeySource
}
// Binding returns an instance of a new Binding. If there is no any
// stored predecessor, it will deliver new Binding without containing any
// previous Binding. In case predecessor is defined, all will be returned
// together.
//
// It respects BindingSource interface.
func (b *sliceBindingSource[T]) Binding() (Binding, error) {
binding, err := b.source.Binding()
if err != nil {
return nil, err
}
instance, err := 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)
}
previous, ok := b.previous.(*sliceBinding[T])
if !ok {
return &sliceBinding[T]{
current: binding,
}, nil
}
return &sliceBinding[T]{
previous: previous,
current: binding,
}, nil
}
// SetPrevious stores preceding Binding as a previous one.
//
// It respects FollowingBindingSource interface.
func (b *sliceBindingSource[T]) SetPrevious(binding Binding) {
b.previous = binding
}
// Key executes the same method from inner KeyOption instance.
//
// It respects BindingOption interface.
func (b *sliceBindingSource[T]) Key() Key {
return b.keySource.Key()
}
// InSlice delivers a BindingSource for a slice of types T.. It is used as a wrapping
// BindingSource for any other inner. It creates complex Binding in the background
// that stores all T types in a slice and delivers it upon request by executing NewInstance
// method for a slice of T types.
//
// Example:
// err := :genjector.Bind(
//
// genjector.InSlice(
// genjector.AsInstance[SliceInterface](&SliceStruct{
// value: "concrete value",
// }),
// ),
//
// )
//
// BindingSource can be only used as the first argument to Bind method.
func InSlice[T any](source BindingSource[T]) BindingSource[T] {
return &sliceBindingSource[T]{
source: source,
keySource: sliceKeySource[T]{},
}
}
// mapBinding is a concrete implementation for Binding interface.
type mapBinding[K comparable, T any] struct {
previous *mapBinding[K, T]
key K
current Binding
}
// Instance returns a map of K-T pairs by executing current Binding and
// all other preceding ones.
//
// It respects Binding interface.
func (b *mapBinding[K, T]) Instance(initialize bool) (interface{}, error) {
result := map[K]T{}
if initialize && b.previous != nil {
instance, err := b.previous.Instance(initialize)
if err != nil {
return nil, err
}
transformed, ok := instance.(map[K]T)
if !ok {
return nil, fmt.Errorf(`binding is not possible for "%v" and "%v"`, result, instance)
}
result = transformed
}
instance, err := b.current.Instance(initialize)
if err != nil {
return nil, err
}
transformed, ok := instance.(T)
if !ok {
return nil, fmt.Errorf(`binding is not possible for "%v" and "%v"`, result, instance)
}
result[b.key] = transformed
return result, err
}
// mapBindingSource is a concrete implementation for BindingSource interface.
type mapBindingSource[K comparable, T any] struct {
previous Binding
source BindingSource[T]
key K
keySource KeySource
}
// Binding returns an instance of a new Binding. If there is no any
// stored predecessor, it will deliver new Binding without containing any
// previous Binding. In case predecessor is defined, all will be returned
// together.
//
// It respects BindingSource interface.
func (b *mapBindingSource[K, T]) Binding() (Binding, error) {
binding, err := b.source.Binding()
if err != nil {
return nil, err
}
instance, _ := binding.Instance(false)
if _, ok := instance.(T); !ok {
var initial T
return nil, fmt.Errorf(`binding is not possible for "%v" and "%v"`, initial, instance)
}
previous, ok := b.previous.(*mapBinding[K, T])
if !ok {
return &mapBinding[K, T]{
key: b.key,
current: binding,
}, nil
}
return &mapBinding[K, T]{
previous: previous,
key: b.key,
current: binding,
}, nil
}
// SetPrevious stores preceding Binding as a previous one.
//
// It respects FollowingBindingSource interface.
func (b *mapBindingSource[K, T]) SetPrevious(binding Binding) {
b.previous = binding
}
// Key executes the same method from inner KeyOption instance.
//
// It respects BindingOption interface.
func (b *mapBindingSource[K, T]) Key() Key {
return b.keySource.Key()
}
// InMap delivers a BindingSource for a type T and key's type K, that creates a map
// of K-T pairs. It is used as a wrapping BindingSource for any other inner. It creates
// complex Binding in the background that stores all T types in a map and delivers it
// upon request by executing NewInstance method for a K-T map.
//
// Example:
// err := :genjector.Bind(
//
// genjector.InMap(
// "third",
// genjector.AsInstance[MapInterface](&MapStruct{
// value: "concrete value",
// }),
// ),
//
// )
//
// BindingSource can be only used as the first argument to Bind method.
func InMap[K comparable, T any](key K, source BindingSource[T]) BindingSource[T] {
return &mapBindingSource[K, T]{
key: key,
source: source,
keySource: mapKeySource[K, T]{},
}
}