Documentation
¶
Overview ¶
Package registry implements module-scoped dependency declaration and injection.
A Container owns one module's objects. Load declares an owned object, Register also makes it injectable inside that Container, and Export also makes it available to other Containers. Requires and Provides expose the resulting module boundary without constructing a second metadata model. Fields tagged with `aperture` contribute to Requires. Load, Register, and Export accept existing values, and their generic forms return the value unchanged so declaration and assignment can share one statement.
Module factories create and connect their internal objects with ordinary Go code. External module dependencies are injected after every module has declared its objects, so field-based dependency cycles are supported.
Scope owns global values, injection hooks, and one flat set of module Containers. Build validates and imports exports, orders the Containers, and performs the injection pass. Reset removes that module set while preserving global values and hooks. Create a Scope per test or independent runtime. Package-level Set, Use, and hook functions delegate to the default Scope. Registry deliberately has no lifecycle behavior; the root scene package owns module setup and teardown.
Index ¶
- Constants
- func Export[T any](container *Container, value T, name ...string) T
- func Load[T any](container *Container, value T) T
- func Lookup[T any](name ...string) (T, bool)
- func LookupIn[T any](scope *Scope, name ...string) (T, bool)
- func Provide[T any](name ...string) T
- func ProvideIn[T any](scope *Scope, name ...string) T
- func Register[T any](container *Container, value T, name ...string) T
- func RegisterInjectHookFunc(ifaceName string, hook InjectHookFunc)
- func RegisterInjectHooks(hooks ...InjectHook)
- func Set[T any](value T, name ...string) T
- func SetIn[T any](scope *Scope, value T, name ...string) T
- func Use[T any](val T) T
- func UseIn[T any](scope *Scope, val T) T
- type Container
- func (c *Container) Export(value any, name string)
- func (c *Container) Import(name string, imported any)
- func (c *Container) Inject(hooks ...InjectHookFunc)
- func (c *Container) Load(value any)
- func (c *Container) Provides() []string
- func (c *Container) Register(value any, name string)
- func (c *Container) Requires(optional bool) []string
- func (c *Container) Resolve(name string) (any, bool)
- type InjectHook
- type InjectHookFunc
- type Scope
Constants ¶
const EmbedValue = "embed"
const InjectTag = "aperture"
const OptionalValue = "optional"
Variables ¶
This section is empty.
Functions ¶
func Export ¶ added in v0.4.0
Export declares value and returns it unchanged. T is used as the default dependency key.
func Lookup ¶ added in v0.4.0
Lookup resolves a dependency without panicking when it is unavailable.
func LookupIn ¶ added in v0.4.0
LookupIn resolves a dependency from scope without panicking when it is unavailable.
func Register ¶
Register declares value and returns it unchanged. T is used as the default dependency key.
func RegisterInjectHookFunc ¶ added in v0.2.8
func RegisterInjectHookFunc(ifaceName string, hook InjectHookFunc)
func RegisterInjectHooks ¶ added in v0.2.8
func RegisterInjectHooks(hooks ...InjectHook)
func Set ¶ added in v0.4.0
Set registers a process-level value used before a module graph is active. It is intended for global dependencies such as configuration.
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container declares and owns the objects belonging to one module.
func NewContainer ¶
func (*Container) Export ¶ added in v0.4.0
Export binds a value to name for injection inside this container and into other containers.
func (*Container) Inject ¶ added in v0.4.0
func (c *Container) Inject(hooks ...InjectHookFunc)
Inject injects every declared value using this container's local and imported bindings.
func (*Container) Load ¶ added in v0.4.0
Load declares a value owned and injected by the container. The value cannot be resolved as a dependency.
func (*Container) Provides ¶ added in v0.4.0
Provides returns the dependencies exported by this container, sorted by key.
func (*Container) Register ¶ added in v0.4.0
Register binds a value to name for injection inside this container.
type InjectHook ¶ added in v0.2.8
type InjectHook struct {
Interface string
Hook InjectHookFunc
}
type InjectHookFunc ¶ added in v0.2.8
type InjectHookFunc func( iface string, obj reflect.Value, field reflect.Value, instance *interface{})
InjectHookFunc runs during dependency injection when a field is about to be set. It is NOT called during registration.
Parameters:
- iface: the lookup key/name used for this injection (usually interface type string or custom tag)
- obj: the target object currently being injected (addressable reflect.Value)
- field: the target field reflect.Value that will receive the dependency
- instance: pointer to the resolved dependency instance; hook may replace it before field assignment
If no dependency was resolved, this hook is not executed.
type Scope ¶ added in v0.4.0
type Scope struct {
// contains filtered or unexported fields
}
Scope owns the global values, injection hooks, and module dependencies for one isolated registry environment.
func DefaultScope ¶ added in v0.4.0
func DefaultScope() *Scope
DefaultScope returns the Scope used by package-level registry functions.
func (*Scope) Build ¶ added in v0.4.0
Build validates, connects, and injects a flat set of module containers.
func (*Scope) OrderedValues ¶ added in v0.4.0
OrderedValues returns module-owned values in dependency order and in each container's declaration order.
func (*Scope) RegisterInjectHookFunc ¶ added in v0.4.0
func (s *Scope) RegisterInjectHookFunc(ifaceName string, hook InjectHookFunc)
func (*Scope) RegisterInjectHooks ¶ added in v0.4.0
func (s *Scope) RegisterInjectHooks(hooks ...InjectHook)