Documentation ¶
Overview ¶
Package kinit provides tools for creating objects.
Index ¶
- type Arena
- func (a *Arena) Finalize() error
- func (a *Arena) Finalized() bool
- func (a *Arena) Get(t reflect.Type) (obj reflect.Value, ok bool)
- func (a *Arena) MustFinalize()
- func (a *Arena) MustPut(t reflect.Type, obj reflect.Value, dtor kdone.Destructor)
- func (a *Arena) Put(t reflect.Type, obj reflect.Value, dtor kdone.Destructor) error
- type Constructor
- type Container
- func (c *Container) Attach(proc Processor) error
- func (c *Container) Explore(f func(reflect.Type, Constructor, []Processor) (next bool))
- func (c *Container) Lookup(t reflect.Type) (Constructor, []Processor)
- func (c *Container) MustAttach(proc Processor)
- func (c *Container) MustProvide(ctor Constructor)
- func (c *Container) MustRun(functors ...Functor)
- func (c *Container) Provide(ctor Constructor) error
- func (c *Container) Run(functors ...Functor) (err error)
- type Functor
- type Processor
- type Runtime
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Arena ¶
type Arena struct {
// contains filtered or unexported fields
}
Arena represents an objects holder.
func (*Arena) Finalized ¶ added in v0.4.0
Finalized returns boolean specifies were objects registered on this arena destroyed.
func (*Arena) Get ¶
Get returns an object of the given type if registered on this arena or on the one of non-finalized parent arenas which will be bypassed in the order that they were passed to the NewArena.
func (*Arena) MustFinalize ¶
func (a *Arena) MustFinalize()
MustFinalize is a variant of the Finalize that panics on error.
type Constructor ¶
type Constructor interface { // Type returns a type of an object that is created by this constructor. Type() reflect.Type // Parameters returns types of objects this constructor depends on. Parameters() []reflect.Type // Create creates and returns a new object. Create(a ...reflect.Value) (reflect.Value, kdone.Destructor, error) }
Constructor represents an object constructor.
The usual identifier for variables of this type is ctor.
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container represents a dependency injection container.
The usual identifier for variables of this type is ctr.
func NewContainer ¶
func NewContainer() *Container
NewContainer returns a new dependency injection container.
func (*Container) Attach ¶ added in v0.4.0
Attach registers the given processor in this container.
Multiple processors may be registered for one type, but there are no guaranty of order of their call.
func (*Container) Explore ¶ added in v0.5.0
Explore calls f for each type presented in this container.
Nil constructor indicates that there are no registered constructor for the type but registered processors are there.
The traversal will be broken if f will return false.
func (*Container) Lookup ¶ added in v0.5.0
func (c *Container) Lookup(t reflect.Type) (Constructor, []Processor)
Lookup returns constructor and processors that are registered for the given type in this container.
Nil constructor indicates that there are no registered constructor for the type.
func (*Container) MustAttach ¶ added in v0.4.0
MustAttach is a variant of the Attach that panics on error.
func (*Container) MustProvide ¶
func (c *Container) MustProvide(ctor Constructor)
MustProvide is a variant of the Provide that panics on error.
func (*Container) Provide ¶
func (c *Container) Provide(ctor Constructor) error
Provide registers the given constructor in this container.
Only one constructor for a type may be registered.
func (*Container) Run ¶ added in v0.4.0
Run runs given functors sequentially resolving their dependencies recursively using this container. If some functor returns further functors all of them will be run before the running of functors that follows it.
All objects created during run will be automatically destroyed when it ends.
type Functor ¶ added in v0.4.0
type Functor interface { // Parameters returns types of objects this functor depends on. Parameters() []reflect.Type // Call calls a function and may return further functors. Call(a ...reflect.Value) ([]Functor, error) }
Functor represents a function.
The usual identifier for variables of this type is fun.
type Processor ¶
type Processor interface { // Type returns a type of an object that is processed by this processor. Type() reflect.Type // Parameters returns types of objects this processor depends on. Parameters() []reflect.Type // Process processes the given object. Process(obj reflect.Value, a ...reflect.Value) error }
Processor represents an object processor.
The usual identifier for variables of this type is proc.
type Runtime ¶ added in v0.4.0
type Runtime struct {
// contains filtered or unexported fields
}
Runtime represents a runtime.
func MustNewRuntime ¶ added in v0.4.0
MustNewRuntime is a variant of the NewRuntime that panics on error.
func NewRuntime ¶ added in v0.4.0
NewRuntime returns a new runtime associated with given container and arena.