Documentation
¶
Index ¶
- func Build(options ...Option)
- func Cleanup()
- func Extract(target interface{}, options ...ExtractOption) (err error)
- func Invoke(fn interface{}) error
- func MustExtract(target interface{}, options ...ExtractOption)
- func MustInvoke(fn interface{})
- type Container
- type ExtractOption
- type Option
- type ParameterBag
- type ProvideOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Extract ¶
func Extract(target interface{}, options ...ExtractOption) (err error)
Extract populates given target pointer with type instance provided in the container.
var server *http.Server
if err = container.Extract(&server); err != nil {
// extract failed
}
If the target type does not exist in a container or instance type building failed, Extract() returns an error. Use ExtractOption for modifying the behavior of this function.
func Invoke ¶
func Invoke(fn interface{}) error
Invoke invokes custom function. Dependencies of function will be resolved via container.
func MustExtract ¶ added in v1.0.1
func MustExtract(target interface{}, options ...ExtractOption)
func MustInvoke ¶ added in v1.0.1
func MustInvoke(fn interface{})
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container is a dependency injection container.
func (*Container) Extract ¶
func (c *Container) Extract(target interface{}, options ...ExtractOption) (err error)
Extract populates given target pointer with type instance provided in the container.
var server *http.Server
if err = container.Extract(&server); err != nil {
// extract failed
}
If the target type does not exist in a container or instance type building failed, Extract() returns an error. Use ExtractOption for modifying the behavior of this function.
func (*Container) Invoke ¶
Invoke invokes custom function. Dependencies of function will be resolved via container.
func (*Container) MustExtract ¶ added in v1.0.1
func (c *Container) MustExtract(target interface{}, options ...ExtractOption)
func (*Container) MustInvoke ¶ added in v1.0.1
func (c *Container) MustInvoke(fn interface{})
type ExtractOption ¶
type ExtractOption interface {
// contains filtered or unexported methods
}
ExtractOption modifies default extract behavior. See container.Name().
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures container. See container.Provide(), container.Bundle(), container.Replace().
func Bundle ¶
Bundle group together container options.
accountBundle := container.Bundle( container.Provide(NewAccountController), container.Provide(NewAccountRepository), ) authBundle := container.Bundle( container.Provide(NewAuthController), container.Provide(NewAuthRepository), ) container, _ := New( accountBundle, authBundle, )
func Provide ¶
func Provide(provider interface{}, options ...ProvideOption) Option
Provide returns container option that explains how to create an instance of a type inside a container.
The first argument is the constructor function. A constructor is a function that creates an instance of the required type. It can take an unlimited number of arguments needed to create an instance - the first returned value.
func NewServer(mux *http.ServeMux) *http.Server {
return &http.Server{
Handle: mux,
}
}
Optionally, you can return a cleanup function and initializing error.
func NewServer(mux *http.ServeMux) (*http.Server, cleanup func(), err error) {
if time.Now().Day = 1 {
return nil, nil, errors.New("the server is down on the first day of a month")
}
server := &http.Server{
Handler: mux,
}
cleanup := func() {
_ = server.Close()
}
return server, cleanup, nil
}
Other function signatures will cause error.
type ParameterBag ¶
type ParameterBag map[string]interface{}
ParameterBag is a provider parameter bag. It stores a construction parameters. It is a alternative way to configure type.
container.Provide(NewServer, container.ParameterBag{
"addr": ":8080",
})
NewServer(pb container.ParameterBag) *http.Server {
return &http.Server{
Addr: pb.RequireString("addr"),
}
}
type ProvideOption ¶
type ProvideOption interface {
// contains filtered or unexported methods
}
ProvideOption modifies default provide behavior. See container.WithName(), container.As(), container.Prototype().
func As ¶
func As(ifaces ...interface{}) ProvideOption
As specifies interfaces that implement provider instance. Provide with As() automatically checks that constructor result implements interface and creates slice group with it.
Provide(&http.ServerMux{}, container.As(new(http.Handler)))
var handler http.Handler
container.Extract(&handler) // extract as interface
var handlers []http.Handler
container.Extract(&handlers) // extract group
func Prototype ¶
func Prototype() ProvideOption
Prototype modifies Provide() behavior. By default, each type resolves as a singleton. This option sets that each type resolving creates a new instance of the type.
Provide(&http.Server{], container.Prototype())
var server1 *http.Server
var server2 *http.Server
container.Extract(&server1, &server2)
func WithName ¶
func WithName(name string) ProvideOption
WithName sets string identifier for provided value.
container.Provide(&http.Server{}, container.WithName("first"))
container.Provide(&http.Server{}, container.WithName("second"))
container.Extract(&server, container.Name("second"))
