Skip to content
This repository was archived by the owner on Jan 18, 2021. It is now read-only.

Commit 5b860a1

Browse files
author
Maxim Bovtunov
committed
Improve docs
1 parent 949b59a commit 5b860a1

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *Container) Extract(target interface{}, options ...ExtractOption) (err e
4848
return c.container.Extract(params)
4949
}
5050

51-
// Cleanup
51+
// Cleanup cleanup container.
5252
func (c *Container) Cleanup() {
5353
c.container.Cleanup()
5454
}

options.go

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,34 @@ type Option interface {
1111

1212
// Provide returns container option that explains how to create an instance of a type inside a container.
1313
//
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.
1916
//
2017
// func NewServer(mux *http.ServeMux) *http.Server {
2118
// return &http.Server{
2219
// Handle: mux,
2320
// }
2421
// }
2522
//
26-
// Optionally, you can return a initializing error.
23+
// Optionally, you can return a cleanup function and initializing error.
2724
//
28-
// func NewServer(mux *http.ServeMux) (*http.Server, err error) {
25+
// func NewServer(mux *http.ServeMux) (*http.Server, cleanup func(), err error) {
2926
// 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")
3128
// }
32-
// return &http.Server{
29+
//
30+
// server := &http.Server{
3331
// Handler: mux,
3432
// }
35-
// }
36-
//
37-
// Other function signatures will cause error.
3833
//
39-
// For advanced providing use inject.dependencyProvider.
40-
//
41-
// type AdminServerProvider struct {
42-
// inject.dependencyProvider
34+
// cleanup := func() {
35+
// _ = server.Close()
36+
// }
4337
//
44-
// AdminMux http.Handler `inject:"admin"` // use named definition
38+
// return &server, cleanup, nil
4539
// }
4640
//
47-
// func (p *AdminServerProvider) Provide() *http.Server {
48-
// return &http.Server{
49-
// Handler: p.AdminMux,
50-
// }
51-
// }
41+
// Other function signatures will cause error.
5242
func Provide(provider interface{}, options ...ProvideOption) Option {
5343
return option(func(container *Container) {
5444
var po = di.ProvideParams{
@@ -89,7 +79,7 @@ func Bundle(options ...Option) Option {
8979
})
9080
}
9181

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().
9383
type ProvideOption interface {
9484
apply(params *di.ProvideParams)
9585
}
@@ -106,8 +96,8 @@ func WithName(name string) ProvideOption {
10696
})
10797
}
10898

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.
111101
//
112102
// Provide(&http.ServerMux{}, inject.As(new(http.Handler)))
113103
//
@@ -123,14 +113,32 @@ func As(ifaces ...interface{}) ProvideOption {
123113
})
124114
}
125115

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+
//
127125
func Prototype() ProvideOption {
128126
return provideOption(func(provider *di.ProvideParams) {
129127
provider.IsPrototype = true
130128
})
131129
}
132130

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+
// }
134142
type ParameterBag map[string]interface{}
135143

136144
func (p ParameterBag) apply(provider *di.ProvideParams) {

0 commit comments

Comments
 (0)