Documentation
¶
Index ¶
- func AddTagged(c *Container, tag string, object any)
- func AsType[T any](c *Container) []T
- func ClearTagged(c *Container, tag string) int
- func Get[T any](c *Container) (T, bool)
- func GetNamed[T any](c *Container, name string) (T, bool)
- func Has[T any](c *Container) bool
- func HasNamed[T any](c *Container, name string) bool
- func HasTagged(c *Container, tag string) bool
- func OfType[T any](c *Container) []T
- func Remove[T any](c *Container) bool
- func RemoveNamed[T any](c *Container, name string) bool
- func RemoveTagged(c *Container, object any) int
- func RemoveTaggedFrom(c *Container, tag string, object any) bool
- func Set(c *Container, object any)
- func SetAs[T any](c *Container, object T)
- func SetNamed(c *Container, name string, object any)
- func Tagged(c *Container, tag string) []any
- func TaggedAsType[T any](c *Container, tag string) []T
- func TaggedOfType[T any](c *Container, tag string) []T
- type Application
- func (a *Application[C]) Build() error
- func (a *Application[C]) Configure(path string, opts ...*dd.Options) error
- func (a *Application[C]) Initialize(configPaths ...string) error
- func (a *Application[C]) InitializeWithOptions(opts *dd.Options, configPaths ...string) error
- func (a *Application[C]) InitializeWithPaths(configPaths ...ConfigPath) error
- func (a *Application[C]) InitializeWithPathsAndOptions(opts *dd.Options, configPaths ...ConfigPath) error
- func (a *Application[C]) Link() error
- func (a *Application[C]) Start() error
- func (a *Application[C]) Stop() error
- type ConfigPath
- type Container
- type Factory
- type FactoryFunc
- type InspectData
- type InspectFormat
- type InspectObject
- type InspectSummary
- type Linkable
- type Startable
- type Stoppable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddTagged ¶ added in v0.3.7
AddTagged adds an object to a tagged collection in the container. The same object can be added to multiple tags.
func AsType ¶
AsType visits all objects in the container and returns any that can be cast to type T. This enables finding objects by interface or supertype regardless of their registration type.
func ClearTagged ¶ added in v0.3.7
ClearTagged removes all objects with the specified tag. Returns the number of objects that were removed.
func Get ¶
Get retrieves an object of type T from the container. Returns the object and true if found, or zero value and false if not found.
func GetNamed ¶
GetNamed retrieves a named object of type T from the container. Returns the object and true if found, or zero value and false if not found.
func HasNamed ¶
HasNamed checks if a named object of type T with the given name exists in the container.
func OfType ¶
OfType retrieves all objects of type T from the container (singleton, named, and tagged). Returns a slice containing the singleton (if exists), followed by named instances, then tagged instances. Duplicate objects (same pointer instance in multiple locations) are deduplicated.
func Remove ¶
Remove removes an object of type T from the container. Returns true if the object was found and removed, false if it didn't exist.
func RemoveNamed ¶
RemoveNamed removes a named object of type T with the given name from the container. Returns true if the object was found and removed, false if it didn't exist.
func RemoveTagged ¶ added in v0.3.7
RemoveTagged removes a specific object from ALL tags. Returns the number of tags the object was removed from.
func RemoveTaggedFrom ¶ added in v0.3.7
RemoveTaggedFrom removes a specific object from a specific tag. Returns true if the object was found and removed, false otherwise.
func Set ¶
Set registers a singleton object in the container by its type. If an object of the same type already exists, it will be replaced.
func SetAs ¶
SetAs registers a singleton object in the container by the specified type. If an object of the same type already exists, it will be replaced.
func SetNamed ¶
SetNamed registers a named object in the container by its type and name. If an object with the same type and name already exists, it will be replaced.
func TaggedAsType ¶ added in v0.3.7
TaggedAsType retrieves all objects with the specified tag that can be cast to type T. This enables finding tagged objects by interface regardless of their concrete type.
func TaggedOfType ¶ added in v0.3.7
TaggedOfType retrieves all objects with the specified tag that are of type T.
Types ¶
type Application ¶
type Application[C any] struct { Cfg C // configuration object C *Container // dependency injection container Factories []Factory[C] // factories for creating and registering objects }
Application orchestrates the lifecycle of a dependency injection container with configuration. It manages object creation through factories, dependency linking, startup, and shutdown phases.
func NewApplication ¶
func NewApplication[C any](cfg C) *Application[C]
NewApplication creates a new application with the given configuration. The configuration object is automatically registered in the container.
func WithFactory ¶
func WithFactory[C any](a *Application[C], f Factory[C]) *Application[C]
WithFactory adds a factory to the application for fluent configuration. Returns the application to enable method chaining.
func WithFactoryFunc ¶
func WithFactoryFunc[C any](a *Application[C], f func(a *Application[C]) error) *Application[C]
WithFactoryFunc adds a function factory to the application for fluent configuration. Returns the application to enable method chaining.
func (*Application[C]) Build ¶
func (a *Application[C]) Build() error
Build executes all registered factories to create and register objects in the container. Factories are responsible for calling SetAs[T]() to register their created objects.
func (*Application[C]) Configure ¶
func (a *Application[C]) Configure(path string, opts ...*dd.Options) error
Configure loads additional configuration from a file and merges it with the existing configuration. Supports JSON and YAML file formats based on file extension.
func (*Application[C]) Initialize ¶
func (a *Application[C]) Initialize(configPaths ...string) error
Initialize executes Configure, Build, and Link phases in sequence. Returns on first error without proceeding to subsequent phases.
func (*Application[C]) InitializeWithOptions ¶
func (a *Application[C]) InitializeWithOptions(opts *dd.Options, configPaths ...string) error
InitializeWithOptions executes Configure, Build, and Link phases in sequence with custom options. Returns on first error without proceeding to subsequent phases.
func (*Application[C]) InitializeWithPaths ¶ added in v0.3.5
func (a *Application[C]) InitializeWithPaths(configPaths ...ConfigPath) error
InitializeWithPaths executes Configure, Build, and Link phases in sequence. Config paths can be marked as optional using OptionalPath(), which will skip missing files without returning an error. Required paths (using RequiredPath()) will fail if missing.
func (*Application[C]) InitializeWithPathsAndOptions ¶ added in v0.3.5
func (a *Application[C]) InitializeWithPathsAndOptions(opts *dd.Options, configPaths ...ConfigPath) error
InitializeWithPathsAndOptions executes Configure, Build, and Link phases in sequence with custom options. Config paths can be marked as optional using OptionalPath(), which will skip missing files without returning an error. Required paths (using RequiredPath()) will fail if missing. Non-existence errors are only ignored for optional paths; other errors (permissions, malformed files, etc.) are always returned regardless of the optional flag.
func (*Application[C]) Link ¶
func (a *Application[C]) Link() error
Link establishes dependencies between objects by calling Link() on all Linkable objects. This phase occurs after Build() to ensure all objects exist before dependency resolution. Returns the first error encountered, which stops the linking process.
func (*Application[C]) Start ¶
func (a *Application[C]) Start() error
Start initializes all Startable objects after linking is complete. Returns the first error encountered, which stops the startup process.
func (*Application[C]) Stop ¶
func (a *Application[C]) Stop() error
Stop shuts down all Stoppable objects for graceful cleanup. Returns the first error encountered, but continues attempting to stop remaining objects.
type ConfigPath ¶ added in v0.3.5
ConfigPath represents a configuration file path with optional loading behavior. When Optional is true, the file will be skipped if it doesn't exist without returning an error.
func OptionalPath ¶ added in v0.3.5
func OptionalPath(path string) ConfigPath
OptionalPath creates a ConfigPath for an optional configuration file. If the file doesn't exist, it will be silently skipped during initialization.
func RequiredPath ¶ added in v0.3.5
func RequiredPath(path string) ConfigPath
RequiredPath creates a ConfigPath for a required configuration file. If the file doesn't exist, initialization will fail with an error.
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container is an application container that manages singletons and objects by type and (optionally) by name.
func NewContainer ¶
func NewContainer() *Container
NewContainer creates and returns a new empty container.
func (*Container) Inspect ¶
func (c *Container) Inspect(format InspectFormat) (string, error)
Inspect returns a formatted representation of the container contents. Supports table, JSON, and YAML formats for human and machine consumption.
type Factory ¶
type Factory[C any] interface { Build(a *Application[C]) error }
Factory creates and registers objects in the application container. Implementations should use SetAs[T]() to register created objects with the container.
type FactoryFunc ¶
type FactoryFunc[C any] func(a *Application[C]) error
FactoryFunc is a function type that implements Factory[C]. It allows using raw functions as factories without defining separate types.
func (FactoryFunc[C]) Build ¶
func (f FactoryFunc[C]) Build(a *Application[C]) error
Build implements the Factory interface for FactoryFunc.
type InspectData ¶
type InspectData struct {
Summary InspectSummary `json:"summary" yaml:"summary"`
Objects []InspectObject `json:"objects" yaml:"objects"`
}
InspectData represents the structured data for container inspection.
type InspectFormat ¶
type InspectFormat string
InspectFormat defines the output format for container inspection.
const ( InspectHuman InspectFormat = "human" InspectJSON InspectFormat = "json" InspectYAML InspectFormat = "yaml" )
type InspectObject ¶
type InspectObject struct {
Type string `json:"type" yaml:"type"`
Storage string `json:"storage" yaml:"storage"`
Name *string `json:"name,omitempty" yaml:"name,omitempty"`
Tag *string `json:"tag,omitempty" yaml:"tag,omitempty"`
Value string `json:"value" yaml:"value"`
}
InspectObject represents a single object in the container for inspection.
type InspectSummary ¶
type InspectSummary struct {
Total int `json:"total" yaml:"total"`
Singletons int `json:"singletons" yaml:"singletons"`
Named int `json:"named" yaml:"named"`
Tagged int `json:"tagged" yaml:"tagged"`
}
InspectSummary provides aggregate statistics about the container.
type Linkable ¶
Linkable defines objects that can establish connections to other container objects during the linking phase after all objects have been created.