Documentation ¶
Overview ¶
The modules package implements binders for dependency injection of tagged struct fields.
Example ¶
package main import ( "fmt" ) // The KVClient interface models a simple key/value store. type KVClient interface { Get(key string) string Put(key, value string) } // A MapDBClient is a simple mock KVClient implementation backed by a map and configured with a default value for missing keys. type MapDBClient struct { defaultValue string db map[string]string } func (client *MapDBClient) Get(key string) string { if value, ok := client.db[key]; ok { return value } else { return client.defaultValue } } func (client *MapDBClient) Put(key, value string) { client.db[key] = value } // A service module has a 'GetData' service which utilizes an injected DBClient. type ServiceModule struct { KVClient KVClient `inject:""` } func (service *ServiceModule) GetData(key string) string { return service.KVClient.Get(key) } func (service *ServiceModule) StoreData(key, value string) { service.KVClient.Put(key, value) } type defaultValue string // This data module provides a KVClient. type DataModule struct { DefaultValue defaultValue KVClient KVClient `provide:""` } func (data *DataModule) Provide() error { data.KVClient = &MapDBClient{defaultValue: string(data.DefaultValue), db: make(map[string]string)} return nil } func main() { serviceModule := &ServiceModule{} dataModule := &DataModule{DefaultValue: "default"} binder := NewBinder() if err := binder.Bind(serviceModule, dataModule); err != nil { panic(err) } fmt.Println(serviceModule.GetData("key")) serviceModule.StoreData("key", "value") fmt.Println(serviceModule.GetData("key")) }
Output: default value
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AnnotatedError ¶
type AnnotatedError struct {
// contains filtered or unexported fields
}
An AnnotatedError holds a message and wraps another error.
func (*AnnotatedError) Error ¶
func (e *AnnotatedError) Error() string
type Binder ¶
type Binder struct {
// contains filtered or unexported fields
}
A Binder holds a configuration for module binding.
func NewBinder ¶
func NewBinder(options ...BinderOption) *Binder
NewBinder initializes a new Binder instance, and applies options.
type BinderOption ¶
type BinderOption interface {
// contains filtered or unexported methods
}
A functional option for configuring a Binder.
type BindingError ¶
type BindingError struct {
// contains filtered or unexported fields
}
A BindingError indicates failure during binding. Holds one or more errors which prevented binding.
func (*BindingError) Error ¶
func (e *BindingError) Error() string
type Provider ¶
type Provider interface { // Provide is called once to set provided fields. // Returns nil for success, or an error in the case of failed binding. // This method is called prior to field injection, so injected fields may not be directly referenced, but may be closed over. Provide() error }
A Provider is a binding module that implements the Provide() method. When a Provider is bound, Provide() will be called (prior to having fields injected).
Directories ¶
Path | Synopsis |
---|---|
Package inject contains code for working with Injectors.
|
Package inject contains code for working with Injectors. |
env
Package env provides a inject.Injector to set values from environment variables.
|
Package env provides a inject.Injector to set values from environment variables. |
file
Package file provides an inject.Injector for file input.
|
Package file provides an inject.Injector for file input. |
flag
Package flag provides an inject.Injector to set values from command line flags.
|
Package flag provides an inject.Injector to set values from command line flags. |
literal
Package literal provides an inject.Injector that parses string literals into values.
|
Package literal provides an inject.Injector that parses string literals into values. |
Package tags contains code for parsing struct tags.
|
Package tags contains code for parsing struct tags. |
Click to show internal directories.
Click to hide internal directories.