-
Notifications
You must be signed in to change notification settings - Fork 13
/
options.go
293 lines (260 loc) · 7.96 KB
/
options.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
package di
// Option is a functional option that configures container. If you don't know about functional
// options, see https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis.
// Below presented all possible options with their description:
//
// - di.Provide - provide constructors
// - di.ProvideValue - provide value
// - di.Invoke - add invocations
// - di.Resolve - resolves type
type Option interface {
apply(c *diopts)
}
// Provide returns container option that provides to container reliable way to build type. The constructor will
// be invoked lazily on-demand. For more information about constructors see Constructor interface. ProvideOption can
// add additional behavior to the process of type resolving.
func Provide(constructor Constructor, options ...ProvideOption) Option {
frame := stacktrace(0)
return option(func(c *diopts) {
c.provides = append(c.provides, provideOptions{
frame,
constructor,
options,
})
})
}
// ProvideValue provides value as is.
func ProvideValue(value Value, options ...ProvideOption) Option {
frame := stacktrace(0)
return option(func(c *diopts) {
c.values = append(c.values, provideValueOptions{
frame,
value,
options,
})
})
}
// Constructor is a function with follow signature:
//
// func NewHTTPServer(addr string, handler http.Handler) (server *http.Server, cleanup func(), err error) {
// server := &http.Server{
// Addr: addr,
// }
// cleanup = func() {
// server.Close()
// }
// return server, cleanup, nil
// }
//
// This constructor function teaches container how to build server. Arguments (addr and handler) in this function
// is a dependencies. They will be resolved automatically when someone needs a server. Constructor may have unlimited
// count of dependencies, but note that container should know how build each of them.
// Second result of this function is a optional cleanup callback. It describes that container will do on shutdown.
// Third result is a optional error. Sometimes our types cannot be constructed.
type Constructor interface{}
// Value is a variable of provided or resolved type.
type Value interface{}
// ProvideOption is a functional option interface that modify provide behaviour. See di.As(), di.WithName().
type ProvideOption interface {
applyProvide(params *ProvideParams)
}
// As returns provide option that specifies interfaces for constructor resultant type.
//
// INTERFACE USAGE:
//
// You can provide type as interface and resolve it later without using of direct implementation.
// This creates less cohesion of code and promotes be more testable.
//
// Create type constructors:
//
// func NewServeMux() *http.ServeMux {
// return &http.ServeMux{}
// }
//
// func NewServer(handler *http.Handler) *http.Server {
// return &http.Server{
// Handler: handler,
// }
// }
//
// Build container with di.As provide option:
//
// container, err := di.New(
// di.Provide(NewServer),
// di.Provide(NewServeMux, di.As(new(http.Handler)),
// )
// if err != nil {
// // handle error
// }
// var server *http.Server
// if err := container.Resolve(&http.Server); err != nil {
// // handle error
// }
//
// In this example you can see how container inject type *http.ServeMux as http.Handler
// interface into the server constructor.
//
// GROUP USAGE:
//
// Container automatically creates group for interfaces. For example, you can use type []http.Handler in
// previous example.
//
// var handlers []http.Handler
// if err := container.Resolve(&handlers); err != nil {
// // handle error
// }
//
// Container checks that provided type implements interface if not cause compile error.
func As(interfaces ...Interface) ProvideOption {
return provideOption(func(params *ProvideParams) {
params.Interfaces = append(params.Interfaces, interfaces...)
})
}
// Interface is a pointer to interface, like new(http.Handler). Tell container that provided
// type may be used as interface.
type Interface interface{}
// WithName modifies Provide() behavior. It adds name identity for provided type.
// Deprecated: use di.Tags.
func WithName(name string) ProvideOption {
return provideOption(func(params *ProvideParams) {
if params.Tags == nil {
params.Tags = Tags{}
}
params.Tags["name"] = name
})
}
// Decorator can modify container instance.
type Decorator func(value Value) error
// Decorate will be called after type construction. You can modify your pointer types.
func Decorate(decorators ...Decorator) ProvideOption {
return provideOption(func(params *ProvideParams) {
params.Decorators = append(params.Decorators, decorators...)
})
}
// Resolve returns container options that resolves type into target. All resolves will be done on compile stage
// after call invokes.
func Resolve(target Pointer, options ...ResolveOption) Option {
frame := stacktrace(0)
return option(func(c *diopts) {
c.resolves = append(c.resolves, resolveOptions{
frame,
target,
options,
})
})
}
// Invoke returns container option that registers container invocation. All invocations
// will be called on di.New() after processing di.Provide() options.
// See Container.Invoke() for details.
func Invoke(fn Invocation, options ...InvokeOption) Option {
frame := stacktrace(0)
return option(func(c *diopts) {
c.invokes = append(c.invokes, invokeOptions{
frame,
fn,
options,
})
})
}
// Options group together container options.
//
// account := di.Options(
// di.Provide(NewAccountController),
// di.Provide(NewAccountRepository),
// )
// auth := di.Options(
// di.Provide(NewAuthController),
// di.Provide(NewAuthRepository),
// )
// container, err := di.New(
// account,
// auth,
// )
// if err != nil {
// // handle error
// }
func Options(options ...Option) Option {
return option(func(container *diopts) {
for _, opt := range options {
opt.apply(container)
}
})
}
// ProvideParams is a Provide() method options. Name is a unique identifier of type instance. Provider is a constructor
// function. Interfaces is a interface that implements a provider result type.
type ProvideParams struct {
Tags Tags
Interfaces []Interface
Decorators []Decorator
}
func (p ProvideParams) applyProvide(params *ProvideParams) {
*params = p
}
// InvokeOption is a functional option interface that modify invoke behaviour.
type InvokeOption interface {
apply(params *InvokeParams)
}
// InvokeParams is a invoke parameters.
type InvokeParams struct {
// The function
Fn interface{}
}
func (p InvokeParams) apply(params *InvokeParams) {
*params = p
}
// ResolveOption is a functional option interface that modify resolve behaviour.
type ResolveOption interface {
applyResolve(params *ResolveParams)
}
// Name specifies provider string identity. It needed when you have more than one
// definition of same type. You can identity type by name.
// Deprecated: use di.Tags
func Name(name string) ResolveOption {
return resolveOption(func(params *ResolveParams) {
if params.Tags == nil {
params.Tags = Tags{}
}
params.Tags["name"] = name
})
}
// ResolveParams is a resolve parameters.
type ResolveParams struct {
Tags Tags
}
func (p ResolveParams) applyResolve(params *ResolveParams) {
*params = p
}
type option func(c *diopts)
func (o option) apply(c *diopts) { o(c) }
type provideOption func(params *ProvideParams)
func (o provideOption) applyProvide(params *ProvideParams) {
o(params)
}
type resolveOption func(params *ResolveParams)
func (o resolveOption) applyResolve(params *ResolveParams) {
o(params)
}
// struct that contains constructor with options.
type provideOptions struct {
frame callerFrame
constructor Constructor
options []ProvideOption
}
// struct that contains value with options.
type provideValueOptions struct {
frame callerFrame
value Value
options []ProvideOption
}
// struct that contains invoke function with options.
type invokeOptions struct {
frame callerFrame
fn Invocation
options []InvokeOption
}
// struct that container resolve target with options.
type resolveOptions struct {
frame callerFrame
target Pointer
options []ResolveOption
}