registry

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 5 Imported by: 0

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

View Source
const EmbedValue = "embed"
View Source
const InjectTag = "aperture"
View Source
const OptionalValue = "optional"

Variables

This section is empty.

Functions

func Export added in v0.4.0

func Export[T any](container *Container, value T, name ...string) T

Export declares value and returns it unchanged. T is used as the default dependency key.

func Load

func Load[T any](container *Container, value T) T

Load declares value and returns it unchanged.

func Lookup added in v0.4.0

func Lookup[T any](name ...string) (T, bool)

Lookup resolves a dependency without panicking when it is unavailable.

func LookupIn added in v0.4.0

func LookupIn[T any](scope *Scope, name ...string) (T, bool)

LookupIn resolves a dependency from scope without panicking when it is unavailable.

func Provide added in v0.3.7

func Provide[T any](name ...string) T

func ProvideIn added in v0.4.0

func ProvideIn[T any](scope *Scope, name ...string) T

ProvideIn resolves T from scope and panics when it is unavailable.

func Register

func Register[T any](container *Container, value T, name ...string) T

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

func Set[T any](value T, name ...string) T

Set registers a process-level value used before a module graph is active. It is intended for global dependencies such as configuration.

func SetIn added in v0.4.0

func SetIn[T any](scope *Scope, value T, name ...string) T

SetIn registers a global value in scope.

func Use

func Use[T any](val T) T

Use returns val when it is usable, otherwise it resolves T from the active module graph or the global values.

func UseIn added in v0.4.0

func UseIn[T any](scope *Scope, val T) T

UseIn returns val when it is usable, otherwise it resolves T from scope.

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

Container declares and owns the objects belonging to one module.

func NewContainer

func NewContainer(name ...string) *Container

func (*Container) Export added in v0.4.0

func (c *Container) Export(value any, name string)

Export binds a value to name for injection inside this container and into other containers.

func (*Container) Import added in v0.4.0

func (c *Container) Import(name string, imported any)

Import adds an external dependency to this container.

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

func (c *Container) Load(value any)

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

func (c *Container) Provides() []string

Provides returns the dependencies exported by this container, sorted by key.

func (*Container) Register added in v0.4.0

func (c *Container) Register(value any, name string)

Register binds a value to name for injection inside this container.

func (*Container) Requires added in v0.4.0

func (c *Container) Requires(optional bool) []string

Requires returns the required or optional dependencies that may be exported by other containers, in declaration order.

func (*Container) Resolve added in v0.4.0

func (c *Container) Resolve(name string) (any, bool)

Resolve returns a dependency exported by 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 NewScope added in v0.4.0

func NewScope() *Scope

func (*Scope) Build added in v0.4.0

func (s *Scope) Build(containers ...*Container) error

Build validates, connects, and injects a flat set of module containers.

func (*Scope) OrderedValues added in v0.4.0

func (s *Scope) OrderedValues() []any

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)

func (*Scope) Reset added in v0.4.0

func (s *Scope) Reset()

Reset removes the built module dependencies while preserving global values and injection hooks so this Scope can build another module set.

Jump to

Keyboard shortcuts

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