@@ -11,44 +11,34 @@ type Option interface {
11
11
12
12
// Provide returns container option that explains how to create an instance of a type inside a container.
13
13
//
14
- // The first argument is the provider. The provider can be constructor function, a pointer to a structure (or just
15
- // structure) or everything else. There are some differences between these providers.
16
- //
17
- // A constructor function is a function that creates an instance of the required type. It can take an unlimited
18
- // number of arguments needed to create an instance - the first returned value.
14
+ // The first argument is the constructor function. A constructor is a function that creates an instance of the required
15
+ // type. It can take an unlimited number of arguments needed to create an instance - the first returned value.
19
16
//
20
17
// func NewServer(mux *http.ServeMux) *http.Server {
21
18
// return &http.Server{
22
19
// Handle: mux,
23
20
// }
24
21
// }
25
22
//
26
- // Optionally, you can return a initializing error.
23
+ // Optionally, you can return a cleanup function and initializing error.
27
24
//
28
- // func NewServer(mux *http.ServeMux) (*http.Server, err error) {
25
+ // func NewServer(mux *http.ServeMux) (*http.Server, cleanup func(), err error) {
29
26
// if time.Now().Day = 1 {
30
- // return nil, errors.New("the server is down on the first day of a month")
27
+ // return nil, nil, errors.New("the server is down on the first day of a month")
31
28
// }
32
- // return &http.Server{
29
+ //
30
+ // server := &http.Server{
33
31
// Handler: mux,
34
32
// }
35
- // }
36
- //
37
- // Other function signatures will cause error.
38
33
//
39
- // For advanced providing use inject.dependencyProvider.
40
- //
41
- // type AdminServerProvider struct {
42
- // inject.dependencyProvider
34
+ // cleanup := func() {
35
+ // _ = server.Close()
36
+ // }
43
37
//
44
- // AdminMux http.Handler `inject:"admin"` // use named definition
38
+ // return &server, cleanup, nil
45
39
// }
46
40
//
47
- // func (p *AdminServerProvider) Provide() *http.Server {
48
- // return &http.Server{
49
- // Handler: p.AdminMux,
50
- // }
51
- // }
41
+ // Other function signatures will cause error.
52
42
func Provide (provider interface {}, options ... ProvideOption ) Option {
53
43
return option (func (container * Container ) {
54
44
var po = di.ProvideParams {
@@ -89,7 +79,7 @@ func Bundle(options ...Option) Option {
89
79
})
90
80
}
91
81
92
- // ProvideOption modifies default provide behavior. See inject.WithName(), inject.As(), inject.Exported ().
82
+ // ProvideOption modifies default provide behavior. See inject.WithName(), inject.As(), inject.Prototype ().
93
83
type ProvideOption interface {
94
84
apply (params * di.ProvideParams )
95
85
}
@@ -106,8 +96,8 @@ func WithName(name string) ProvideOption {
106
96
})
107
97
}
108
98
109
- // As specifies interfaces that implement provider instance. Provide with As() automatically checks that instance interfaces
110
- // interface and creates slice group with it.
99
+ // As specifies interfaces that implement provider instance. Provide with As() automatically checks that constructor
100
+ // result implements interface and creates slice group with it.
111
101
//
112
102
// Provide(&http.ServerMux{}, inject.As(new(http.Handler)))
113
103
//
@@ -123,14 +113,32 @@ func As(ifaces ...interface{}) ProvideOption {
123
113
})
124
114
}
125
115
126
- // Prototype
116
+ // Prototype modifies Provide() behavior. By default, each type resolves as a singleton. This option sets that
117
+ // each type resolving creates a new instance of the type.
118
+ //
119
+ // Provide(&http.Server{], inject.Prototype())
120
+ //
121
+ // var server1 *http.Server
122
+ // container.Extract(&server1, &server1)
123
+ //
124
+ //
127
125
func Prototype () ProvideOption {
128
126
return provideOption (func (provider * di.ProvideParams ) {
129
127
provider .IsPrototype = true
130
128
})
131
129
}
132
130
133
- // Parameters
131
+ // ParameterBag is a provider parameter bag. It stores a construction parameters.
132
+ //
133
+ // inject.Provide(NewServer, inject.ParameterBag{
134
+ // "addr": ":8080",
135
+ // })
136
+ //
137
+ // NewServer(pb inject.ParameterBag) *http.Server {
138
+ // return &http.Server{
139
+ // Addr: pb.RequireString("addr"),
140
+ // }
141
+ // }
134
142
type ParameterBag map [string ]interface {}
135
143
136
144
func (p ParameterBag ) apply (provider * di.ProvideParams ) {
0 commit comments