Documentation ¶
Index ¶
- func Build(containerConfig Config, outputs ...interface{}) error
- func BuildDebug(debugOpt DebugOption, config Config, outputs ...interface{}) error
- type Config
- type DebugOption
- func AutoDebug() DebugOption
- func Debug() DebugOption
- func DebugCleanup(cleanup func()) DebugOption
- func DebugOptions(options ...DebugOption) DebugOption
- func FileVisualizer(filename string) DebugOption
- func LogVisualizer() DebugOption
- func Logger(logger func(string)) DebugOption
- func OnError(option DebugOption) DebugOption
- func OnSuccess(option DebugOption) DebugOption
- func StderrLogger() DebugOption
- func StdoutLogger() DebugOption
- func Visualizer(visualizer func(dotGraph string)) DebugOption
- type In
- type Location
- type ManyPerContainerType
- type ModuleKey
- type OnePerModuleType
- type Out
- type OwnModuleKey
- type ProviderDescriptor
- type ProviderInput
- type ProviderOutput
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Build ¶
Build builds the container specified by containerConfig and extracts the requested outputs from the container or returns an error. It is the single entry point for building and running a dependency injection container. Each of the values specified as outputs must be pointers to types that can be provided by the container.
Ex:
var x int Build(Provide(func() int { return 1 }), &x)
Build uses the debug mode provided by AutoDebug which means there will be verbose debugging information if there is an error and nothing upon success. Use BuildDebug to configure debug behavior.
func BuildDebug ¶
func BuildDebug(debugOpt DebugOption, config Config, outputs ...interface{}) error
BuildDebug is a version of Build which takes an optional DebugOption for logging and visualization.
Types ¶
type Config ¶
type Config interface {
// contains filtered or unexported methods
}
Config is a functional configuration of a container.
func Error ¶
Error defines configuration which causes the dependency injection container to fail immediately.
func Provide ¶
func Provide(providers ...interface{}) Config
Provide defines a container configuration which registers the provided dependency injection providers. Each provider will be called at most once with the exception of module-scoped providers which are called at most once per module (see ModuleKey).
func ProvideInModule ¶
ProvideInModule defines container configuration which registers the provided dependency injection providers that are to be run in the named module. Each provider will be called at most once.
type DebugOption ¶
type DebugOption interface {
// contains filtered or unexported methods
}
DebugOption is a functional option for running a container that controls debug logging and visualization output.
func AutoDebug ¶
func AutoDebug() DebugOption
AutoDebug does the same thing as Debug when there is an error and deletes the debug_container.dot if it exists when there is no error. This is the default debug mode of Run.
func Debug ¶
func Debug() DebugOption
Debug is a default debug option which sends log output to stderr, dumps the container in the graphviz DOT and SVG formats to debug_container.dot and debug_container.svg respectively.
func DebugCleanup ¶
func DebugCleanup(cleanup func()) DebugOption
DebugCleanup specifies a clean-up function to be called at the end of processing to clean up any resources that may be used during debugging.
func DebugOptions ¶
func DebugOptions(options ...DebugOption) DebugOption
DebugOptions creates a debug option which bundles together other debug options.
func FileVisualizer ¶
func FileVisualizer(filename string) DebugOption
FileVisualizer is a debug option which dumps a graphviz DOT rendering of the container to the specified file.
func LogVisualizer ¶
func LogVisualizer() DebugOption
LogVisualizer is a debug option which dumps a graphviz DOT rendering of the container to the log.
func Logger ¶
func Logger(logger func(string)) DebugOption
Logger creates an option which provides a logger function which will receive all log messages from the container.
func OnError ¶
func OnError(option DebugOption) DebugOption
OnError is a debug option that allows setting debug options that are conditional on an error happening. Any loggers added error will receive the full dump of logs since the start of container processing.
func OnSuccess ¶
func OnSuccess(option DebugOption) DebugOption
OnSuccess is a debug option that allows setting debug options that are conditional on successful container resolution. Any loggers added on success will receive the full dump of logs since the start of container processing.
func StderrLogger ¶
func StderrLogger() DebugOption
StderrLogger is a debug option which routes logging output to stderr.
func StdoutLogger ¶
func StdoutLogger() DebugOption
StdoutLogger is a debug option which routes logging output to stdout.
func Visualizer ¶
func Visualizer(visualizer func(dotGraph string)) DebugOption
Visualizer creates an option which provides a visualizer function which will receive a rendering of the container in the Graphiz DOT format whenever the container finishes building or fails due to an error. The graph is color-coded to aid debugging with black representing success, red representing an error, and gray representing unused types or functions. Graph rendering should be deterministic for a given version of the container module and container options so that graphs can be used in tests.
type In ¶
type In struct{}
In can be embedded in another struct to inform the container that the fields of the struct should be treated as dependency inputs. This allows a struct to be used to specify dependencies rather than positional parameters.
Fields of the struct may support the following tags:
optional if set to true, the dependency is optional and will be set to its default value if not found, rather than causing an error
type Location ¶
type Location interface { Name() string fmt.Stringer fmt.Formatter // contains filtered or unexported methods }
func LocationFromCaller ¶
func LocationFromPC ¶
type ManyPerContainerType ¶
type ManyPerContainerType interface {
// IsManyPerContainerType is a marker function which just indicates that this is a many-per-container type.
IsManyPerContainerType()
}
ManyPerContainerType marks a type which automatically gets grouped together. For an ManyPerContainerType T, T and []T can be declared as output parameters for providers as many times within the container as desired. All of the provided values for T can be retrieved by declaring an []T input parameter.
type ModuleKey ¶
type ModuleKey struct {
// contains filtered or unexported fields
}
ModuleKey is a special type used to scope a provider to a "module".
Special module-scoped providers can be used with Provide and ProvideInModule by declaring a provider with an input parameter of type ModuleKey. These providers may construct a unique value of a dependency for each module and will be called at most once per module.
When being used with ProvideInModule, the provider will not receive its own ModuleKey but rather the key of the module requesting the dependency so that modules can provide module-scoped dependencies to other modules.
In order for a module to retrieve their own module key they can define a provider which requires the OwnModuleKey type and DOES NOT require ModuleKey.
type OnePerModuleType ¶
type OnePerModuleType interface {
// IsOnePerModuleType is a marker function just indicates that this is a one-per-module type.
IsOnePerModuleType()
}
OnePerModuleType marks a type which can have up to one value per module. All of the values for a one-per-module type T and their respective modules, can be retrieved by declaring an input parameter map[string]T.
type Out ¶
type Out struct{}
Out can be embedded in another struct to inform the container that the fields of the struct should be treated as dependency outputs. This allows a struct to be used to specify outputs rather than positional return values.
type OwnModuleKey ¶
type OwnModuleKey ModuleKey
OwnModuleKey is a type which can be used in a module to retrieve its own ModuleKey. It MUST NOT be used together with a ModuleKey dependency.
type ProviderDescriptor ¶
type ProviderDescriptor struct { // Inputs defines the in parameter types to Fn. Inputs []ProviderInput // Outputs defines the out parameter types to Fn. Outputs []ProviderOutput // Fn defines the provider function. Fn func([]reflect.Value) ([]reflect.Value, error) // Location defines the source code location to be used for this provider // in error messages. Location Location }
ProviderDescriptor defines a special provider type that is defined by reflection. It should be passed as a value to the Provide function. Ex:
option.Provide(ProviderDescriptor{ ... })
func ExtractProviderDescriptor ¶
func ExtractProviderDescriptor(provider interface{}) (ProviderDescriptor, error)