Documentation
¶
Index ¶
Constants ¶
const App = "app"
App is the name of the application scope.
const Request = "request"
Request is the name of the request scope.
const SubRequest = "subrequest"
SubRequest is the name of the subrequest scope.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BasicLogger ¶ added in v1.1.0
type BasicLogger struct{}
BasicLogger is a Logger that uses log.Println to write the error on the standard output.
func (*BasicLogger) Error ¶ added in v1.1.0
func (l *BasicLogger) Error(args ...interface{})
type Builder ¶ added in v1.1.0
type Builder struct {
Logger Logger
// contains filtered or unexported fields
}
Builder is the only way to create a working Context. The scopes and object definitions are set in the Builder that can create a Context based on these information.
func NewBuilder ¶ added in v1.1.0
NewBuilder is the only way to create a working Builder. It initializes the Builder with a list of scopes. The scope are ordered from the wider to the narrower. If no scope is provided, the default scopes are used : [App, Request, SubRequest]
func (*Builder) AddDefinition ¶ added in v1.1.0
func (b *Builder) AddDefinition(def Definition) error
AddDefinition adds an object Definition in the Builder. It returns an error if the Definition can't be added.
func (*Builder) Build ¶ added in v1.1.0
Build creates a Context in the wider scope with all the current scopes and definitions.
func (*Builder) Definitions ¶ added in v1.1.0
func (b *Builder) Definitions() map[string]Definition
Definitions returs a map with the objects definitions added with the AddDefinition method. The key of the map is the name of the Definition.
func (*Builder) IsDefined ¶ added in v1.1.0
IsDefined returns true if there is already a definition with the given name.
type Context ¶
type Context interface {
// Definition returns the map of the available Definitions ordered by Definition name.
// These Definitions represent all the objects that this Context can build.
Definitions() map[string]Definition
// Scope returns the Context scope.
Scope() string
// Scopes returns the list of available scopes.
Scopes() []string
// ParentScopes returns the list of scopes wider than the Context scope.
ParentScopes() []string
// SubScopes returns the list of scopes narrower than the Context scope.
SubScopes() []string
// Parent returns the parent Context.
Parent() Context
// SubContext creates a new Context in the next subscope
// that will have this Container as parent.
SubContext() (Context, error)
// SafeGet retrieves an object from the Context.
// The object has to belong to this scope or a wider one.
// If the object does not already exist, it is created and saved in the Context.
// If the object can't be created, it returns an error.
SafeGet(name string) (interface{}, error)
// Get is similar to SafeGet but it does not return the error.
Get(name string) interface{}
// Fill is similar to SafeGet but it does not return the object.
// Instead it fills the provided object with the value returned by SafeGet.
// The provided object must be a pointer to the value returned by SafeGet.
Fill(name string, dst interface{}) error
// NastySafeGet retrieves an object from the Context, like SafeGet.
// The difference is that the object can be retrieved
// even if it belongs to a narrower scope.
// To do so NastySafeGet creates a subcontext.
// When the created object is no longer needed,
// it is important to use the Clean method to Delete this subcontext.
NastySafeGet(name string) (interface{}, error)
// NastyGet is similar to NastySafeGet but it does not return the error.
NastyGet(name string) interface{}
// NastyFill is similar to NastySafeGet but copies the object in dst instead of returning it.
NastyFill(name string, dst interface{}) error
// Clean deletes the subcontext created by NastySafeGet, NastyGet or NastyFill.
Clean()
// DeleteWithSubContexts takes all the objects saved in this Context
// and calls the Close function of their Definition on them.
// It will also call DeleteWithSubContexts on each child and remove its reference in the parent Context.
// After deletion, the Context can no longer be used.
DeleteWithSubContexts()
// Delete works like DeleteWithSubContexts but do not delete the subcontexts.
// If the Context has subcontexts, it will not be deleted right away.
// The deletion only occurs when all the subcontexts have been deleted.
Delete()
// IsClosed returns true if the Context has been deleted.
IsClosed() bool
}
Context represents a dependency injection container. A Context has a scope and may have a parent with a wider scope and children with a narrower scope. Objects can be retrieved from the Context. If the desired object does not already exist in the Context, it is built thanks to the object Definition. The following attempts to get this object will return the same object.
type Definition ¶ added in v1.1.0
type Definition struct {
Name string
Scope string
Build func(ctx Context) (interface{}, error)
Close func(obj interface{})
Tags []Tag
}
Definition contains information to build and close an object inside a Context.
type DefinitionMap ¶ added in v1.2.0
type DefinitionMap map[string]Definition
DefinitionMap is a collection of Definition ordered by name.
func (DefinitionMap) Copy ¶ added in v1.2.0
func (m DefinitionMap) Copy() map[string]Definition
Copy retuns a copy of the DefinitionMap.
type Logger ¶ added in v1.1.0
type Logger interface {
Error(args ...interface{})
}
Logger is the interface used to log errors that occurred while an object is built or closed.
type MuteLogger ¶ added in v1.1.0
type MuteLogger struct{}
MuteLogger is a Logger that doesn't log anything.
func (*MuteLogger) Error ¶ added in v1.1.0
func (l *MuteLogger) Error(args ...interface{})