container

package module
v0.0.0-...-a315113 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(invoker interface{}, opts ...Option) error

Run runs the provided invoker function with values provided by the provided options. It is the single entry point for building and running a dependency injection container. Invoker should be a function taking one or more dependencies from the container, optionally returning an error.

Ex:

Run(func (x int) error { println(x) }, Provide(func() int { return 1 }))

func RunDebug

func RunDebug(invoker interface{}, debugOpt DebugOption, opts ...Option) error

RunDebug is a version of Run which takes an optional DebugOption for logging and visualization.

Types

type AutoGroupType

type AutoGroupType interface {
	// IsAutoGroupType is a marker function which just indicates that this is a auto-group type.
	IsAutoGroupType()
}

AutoGroupType marks a type which automatically gets grouped together. For an AutoGroupType 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 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 Debug

func Debug() DebugOption

Debug is a default debug option which sends log output to stdout, dumps the container in the graphviz DOT format to stdout, and to the file container_dump.svg.

func DebugOptions

func DebugOptions(options ...DebugOption) DebugOption

DebugOptions creates a debug option which bundles together other debug options.

func FileVisualizer

func FileVisualizer(filename, format string) DebugOption

FileVisualizer is a debug option which dumps a graphviz rendering of the container to the specified file with the specified format. Currently supported formats are: dot, svg, png and jpg.

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 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.

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 LocationFromCaller(skip int) Location

func LocationFromPC

func LocationFromPC(pc uintptr) Location

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 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.

Providers passed to ProvideInModule can also declare an input parameter of type ModuleKey to retrieve their module key but these providers will be called at most once.

func (ModuleKey) Name

func (k ModuleKey) Name() string

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 Option

type Option interface {
	// contains filtered or unexported methods
}

Option is a functional option for a container.

func Error

func Error(err error) Option

Error creates an option which causes the dependency injection container to fail immediately.

func Options

func Options(opts ...Option) Option

Options creates an option which bundles together other options.

func Provide

func Provide(providers ...interface{}) Option

Provide creates a container option 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

func ProvideInModule(moduleName string, providers ...interface{}) Option

ProvideInModule creates a container option which registers the provided dependency injection providers that are to be run in the named module. Each provider will be called at most once.

func Supply

func Supply(values ...interface{}) Option

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 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)

type ProviderInput

type ProviderInput struct {
	Type     reflect.Type
	Optional bool
}

type ProviderOutput

type ProviderOutput struct {
	Type reflect.Type
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL