-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
240 lines (213 loc) · 5.58 KB
/
provider.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
package provide
import (
"errors"
"reflect"
"strings"
)
// A Provider is a dependency injector.
// Using the Provide method, it can automatically construct and initialize new values.
//
// It contains a pool of values as well as rules it can use to construct new ones.
// A Provider only ever contains at most a single value for any given type,
// and it can only have a single rule used to construct the value
// for any given type.
//
// If you need to dependency inject multiple different values of the same type,
// you will have to use a wrapper type in order to distinguish them.
//
// A Provider is no longer valid to use after it returns an
// error, such as when conflicting rules are added, or a rule returns
// an error instead of successfully constructing the required value.
//
type Provider struct {
tasks map[task]state
values map[reflect.Type]reflect.Value
}
// NewProvider constructs a Provider given a list of rules to use to
// construct values from their dependencies.
//
// p, err := NewProvider(rule1, rule2, ...)
//
// is equivalent to
//
// p := &Provider{}
// err := p.AddRule(rule1)
// err = p.AddRule(rule2)
// ...
//
// aborting if there's a non-nil error.
//
func NewProvider(provideFns ...interface{}) (*Provider, error) {
provider := &Provider{}
provider.init()
for _, provideFn := range provideFns {
if err := provider.AddRule(provideFn); err != nil {
return nil, err
}
}
return provider, nil
}
// AddRule gives a Provider a way to construct more types.
// All rules should be added before a Provider is used to provide any values.
//
// A rule is any function from dependencies to new values or an error:
//
// provider.AddRule(func(a A, b B, c C) (X, Y, error) {
// x := makeX(a, b, c)
// y := makeY(a, b, c)
// return x, y, nil
// })
//
// The error return is optional, but any interface value that implements
// error will be considered to be an error.
//
func (p *Provider) AddRule(provideFn interface{}) error {
p.init()
initializers, err := customProvide(provideFn)
if err != nil {
return err
}
for _, init := range initializers {
tasks := [...]task{
{init.Type, false},
{init.Type, true},
}
for _, t := range tasks {
if _, ok := p.tasks[t]; ok {
return errors.New("trying to provide the same type " + t.Type.String() + " in multiple ways")
}
}
p.tasks[tasks[0]] = init.Partial
p.tasks[tasks[1]] = init.Complete
}
return nil
}
// Provide, given a set of non-nil pointers, will construct, initialize,
// and set the values they point to using the rules the Provider has been given.
//
// Note that if the value you are trying to get is itself a pointer (and it often is,)
// you will have to give Provide a pointer-to-pointer:
//
// var aPtr *A
// var bInterface B
// err := p.Provide(&aPtr, &b)
// // aPtr and bInterface are now non-nil
//
func (p *Provider) Provide(ptrsToRequests ...interface{}) error {
p.init()
for _, ptr := range ptrsToRequests {
vptr := reflect.ValueOf(ptr)
if vptr.Kind() != reflect.Ptr || vptr.IsNil() {
return errors.New("arguments to Provide must be non-nil pointers (that Provide will set)")
}
v := vptr.Elem()
t := v.Type()
if isErrorType(t) {
return errors.New("since " + t.String() + " implements error, it is considered an error and cannot be provided")
}
if err := p.complete(t); err != nil {
return err
}
value, ok := p.values[t]
if !ok {
return errors.New("should never happen: couldn't find value")
}
v.Set(value)
}
return nil
}
func (p *Provider) init() {
if p.tasks == nil {
p.tasks = make(map[task]state)
}
if p.values == nil {
p.values = make(map[reflect.Type]reflect.Value)
}
}
func (p *Provider) complete(typ reflect.Type) error {
goals := []task{{typ, true}}
for i := 0; i < len(goals); i++ {
newlyDone, err := p.do(goals[i])
if err != nil {
return err
}
for _, done := range newlyDone {
if !done.Complete {
goals = append(goals, task{done.Type, true})
}
}
}
return nil
}
func (p *Provider) do(t task) ([]task, error) {
stack := []task{t}
newlyDone := make([]task, 0, 2)
for len(stack) > 0 {
t = stack[len(stack)-1]
s, err := p.state(t)
if err != nil {
return nil, err
}
if !s.Done && !s.InProgress {
// The task's dependencies need to be scheduled.
hasDeps := false
for _, dep := range s.DependsOn {
depState, err := p.state(dep)
if err != nil {
return nil, err
}
if depState.Done {
continue
}
if depState.InProgress {
i := 0
for ; i < len(stack); i++ {
if stack[i] == t {
break
}
}
cycle := make([]string, 0, len(stack)-i)
for ; i < len(stack); i++ {
cycle = append(cycle, stack[i].Type.String())
}
return nil, errors.New("cycle: " + strings.Join(cycle, " --> "))
}
stack = append(stack, dep)
hasDeps = true
}
s.InProgress = true
p.tasks[t] = s
if hasDeps {
continue
}
}
if !s.Done && s.InProgress {
// We're returning after dependencies have been completed.
if s.Do != nil {
if err = s.Do(p.values); err != nil {
return nil, err
}
newlyDone = append(newlyDone, t)
}
s.Done = true
s.DependsOn = nil
s.Do = nil
p.tasks[t] = s
}
// This task has been completed.
stack = stack[:len(stack)-1]
}
return newlyDone, nil
}
func (p *Provider) state(t task) (state, error) {
if s, ok := p.tasks[t]; ok {
return s, nil
}
init, err := autoProvide(t.Type)
if err != nil {
return state{}, err
}
p.tasks[task{t.Type, false}] = init.Partial
p.tasks[task{t.Type, true}] = init.Complete
return p.tasks[t], nil
}