Documentation ¶
Overview ¶
Package container provides logic for declaring services and their relations, as well as a centralised endpoint for fetching them from the Config container.
Index ¶
- func RegisterParameters(c Container, dependenciesMaps ...interface{}) error
- func ValidateConfig(tree Tree)
- func ValidateConfigSecure(tree Tree) error
- type Builder
- type Constructor
- type Container
- type CycleDetector
- func (cd *CycleDetector) DisableCycleDetection()
- func (cd *CycleDetector) EnableCycleDetection()
- func (cd *CycleDetector) GetCycle() []string
- func (cd *CycleDetector) HasCycle() bool
- func (cd *CycleDetector) IsEnabled() bool
- func (cd *CycleDetector) Reset()
- func (cd *CycleDetector) VisitAfterRecursion(dep string)
- func (cd *CycleDetector) VisitBeforeRecursion(dep string)
- type Event
- type EventsContainer
- type GarbageCollectorFunc
- type GarbageCollectorFuncs
- type MergeableContainer
- type NewFuncConstructor
- type Node
- type Observer
- type ParametersProvider
- type RuntimeContainer
- func (rc *RuntimeContainer) AddConstructor(id string, constructor Constructor)
- func (rc *RuntimeContainer) AddDependencyObserver(eventName, observerID string, observer interface{})
- func (rc *RuntimeContainer) AddGarbageCollectFunc(serviceName string, gcFunc GarbageCollectorFunc)
- func (rc *RuntimeContainer) AddNewMethod(id string, typedConstructor interface{}, constructorArgumentNames ...string)
- func (rc *RuntimeContainer) Check()
- func (rc *RuntimeContainer) CollectGarbage() error
- func (rc *RuntimeContainer) Exists(id string) bool
- func (rc *RuntimeContainer) Get(id string, isCached bool) interface{}
- func (rc *RuntimeContainer) GetSecure(id string, isCached bool) (interface{}, error)
- func (rc *RuntimeContainer) Merge(c MergeableContainer)
- func (rc *RuntimeContainer) RegisterDependencyEvent(eventName, dependencyName string)
- func (rc *RuntimeContainer) Scan(id string, dest interface{})
- func (rc *RuntimeContainer) ScanNonCached(id string, dest interface{})
- func (rc *RuntimeContainer) ScanSecure(id string, isCached bool, dest interface{}) error
- func (rc *RuntimeContainer) SetConstructor(id string, constructor Constructor)
- func (rc *RuntimeContainer) SetNewMethod(id string, typedConstructor interface{}, constructorArgumentNames ...string)
- type RuntimeContainerBuilder
- type Services
- type Tree
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterParameters ¶
RegisterParameters adds scalar parameter values as dependencies
func ValidateConfig ¶
func ValidateConfig(tree Tree)
ValidateConfig validates a tree of config options and panics if something is wrong
func ValidateConfigSecure ¶
ValidateConfigSecure validates a tree of config options and returns error if something is wrong
Types ¶
type Constructor ¶
Constructor func to return a Service or an error
type Container ¶
type Container interface { AddConstructor(id string, constructor Constructor) AddNewMethod(id string, typedConstructor interface{}, constructorArgumentNames ...string) Scan(id string, dest interface{}) ScanNonCached(id string, dest interface{}) ScanSecure(id string, isCached bool, dest interface{}) error Get(id string, isCached bool) interface{} GetSecure(id string, isCached bool) (interface{}, error) Check() Exists(id string) bool AddGarbageCollectFunc(serviceName string, gcFunc GarbageCollectorFunc) CollectGarbage() error SetConstructor(id string, constructor Constructor) SetNewMethod(id string, typedConstructor interface{}, constructorArgumentNames ...string) }
Container main interface for registering and fetching Services
type CycleDetector ¶
type CycleDetector struct {
// contains filtered or unexported fields
}
CycleDetector used for identification of possible dependency cycles
func (*CycleDetector) DisableCycleDetection ¶
func (cd *CycleDetector) DisableCycleDetection()
DisableCycleDetection can be used to stop the cycle detection at runtime
func (*CycleDetector) EnableCycleDetection ¶
func (cd *CycleDetector) EnableCycleDetection()
EnableCycleDetection can be used to start the cycle detection at runtime
func (*CycleDetector) GetCycle ¶
func (cd *CycleDetector) GetCycle() []string
GetCycle get detected cycle info
func (*CycleDetector) HasCycle ¶
func (cd *CycleDetector) HasCycle() bool
HasCycle gives info if a cycle exists
func (*CycleDetector) IsEnabled ¶
func (cd *CycleDetector) IsEnabled() bool
IsEnabled checks if cycle detection is enabled
func (*CycleDetector) Reset ¶
func (cd *CycleDetector) Reset()
Reset removes all info about previous cycle detection
func (*CycleDetector) VisitAfterRecursion ¶
func (cd *CycleDetector) VisitAfterRecursion(dep string)
VisitAfterRecursion ends current DFS
func (*CycleDetector) VisitBeforeRecursion ¶
func (cd *CycleDetector) VisitBeforeRecursion(dep string)
VisitBeforeRecursion starts DFS
type EventsContainer ¶
type EventsContainer struct {
// contains filtered or unexported fields
}
EventsContainer contains all Observer, events and Config declarations, that you might add in your container
func NewEventsContainer ¶
func NewEventsContainer() *EventsContainer
NewEventsContainer EventsContainer Constr
type GarbageCollectorFunc ¶
type GarbageCollectorFunc func(service interface{}) error
GarbageCollectorFunc typed func to recognise user defined garbage collection funcs
type GarbageCollectorFuncs ¶
type GarbageCollectorFuncs struct {
// contains filtered or unexported fields
}
GarbageCollectorFuncs used to hold info about all garbage collectors
func NewGarbageCollectorFuncs ¶
func NewGarbageCollectorFuncs() *GarbageCollectorFuncs
NewGarbageCollectorFuncs constructor
func (*GarbageCollectorFuncs) Add ¶
func (gcf *GarbageCollectorFuncs) Add(name string, gcFunc GarbageCollectorFunc)
Add a new garbage collector func
func (*GarbageCollectorFuncs) Range ¶
func (gcf *GarbageCollectorFuncs) Range(iterFunc func(gcName string, f GarbageCollectorFunc) bool)
Range iterates over garbage collectors
type MergeableContainer ¶
type MergeableContainer interface { Merge(c MergeableContainer) // contains filtered or unexported methods }
MergeableContainer containers that support merging
type NewFuncConstructor ¶
NewFuncConstructor func type to recognise custom user construction functions
type Node ¶
type Node struct { ID string Constr Constructor NewFunc interface{} ServiceNames Services Ev Event Ob Observer Parameters map[string]interface{} ParamProvider ParametersProvider GarbageFunc GarbageCollectorFunc }
Node of a dependency
type Observer ¶
Observer is a service which is interested other services under a certain event
type ParametersProvider ¶
type ParametersProvider interface {
GetItems() map[string]interface{}
}
ParametersProvider gives container parameters
type RuntimeContainer ¶
type RuntimeContainer struct {
// contains filtered or unexported fields
}
RuntimeContainer creates Services at runtime with registered callbacks
func NewRuntimeContainer ¶
func NewRuntimeContainer() *RuntimeContainer
NewRuntimeContainer creates container
func (*RuntimeContainer) AddConstructor ¶
func (rc *RuntimeContainer) AddConstructor(id string, constructor Constructor)
AddConstructor registers a Callback to create a Service identified by id, panics if id was already declared
Example ¶
This example shows how to add a custom constructor and use an existing service in it
cont := NewRuntimeContainer() cont.AddConstructor("secretKey", func(c Container) (interface{}, error) { return "00000", nil }) cont.AddConstructor("login", func(c Container) (interface{}, error) { return "admin", nil }) cont.AddConstructor("credentials", func(c Container) (interface{}, error) { secretKey := c.Get("secretKey", true).(string) login := c.Get("login", true).(string) return fmt.Sprintf("%s:%s", login, secretKey), nil }) credentials := cont.Get("credentials", true).(string) fmt.Println(credentials)
Output: admin:00000
func (*RuntimeContainer) AddDependencyObserver ¶
func (rc *RuntimeContainer) AddDependencyObserver(eventName, observerID string, observer interface{})
AddDependencyObserver registers Service that will receive Config it is interested in
func (*RuntimeContainer) AddGarbageCollectFunc ¶
func (rc *RuntimeContainer) AddGarbageCollectFunc(serviceName string, gcFunc GarbageCollectorFunc)
AddGarbageCollectFunc registers a garbage collection function to destroy a service resources
func (*RuntimeContainer) AddNewMethod ¶
func (rc *RuntimeContainer) AddNewMethod(id string, typedConstructor interface{}, constructorArgumentNames ...string)
AddNewMethod converts a New Service method to a valid Callback Constr, panics if id already exists
Example ¶
This example shows how to add a struct constructor with some dependencies
//having declared struct dependencies: //type Db struct{} // //func NewDb() Db { // return Db{} //} // //type NameProvider struct { // db Db //} // //func (db Db) GetName() string { // return "Elton John" //} // //func NewNameProvider(db Db) NameProvider { // return NameProvider{db: db} //} // //func (dp NameProvider) GetFullName(salutation string) string { // return salutation + " " + dp.db.GetName() //} cont := NewRuntimeContainer() cont.AddNewMethod("db", NewDb) cont.AddNewMethod("nameProvider", NewNameProvider, "db") nameProvider := cont.Get("nameProvider", true).(NameProvider) fmt.Println(nameProvider.GetFullName("Sir"))
Output: Sir Elton John
func (*RuntimeContainer) Check ¶
func (rc *RuntimeContainer) Check()
Check ensures that all runtime Config are created correctly
func (*RuntimeContainer) CollectGarbage ¶
func (rc *RuntimeContainer) CollectGarbage() error
CollectGarbage will call all registered garbage collection functions and return the aggregated error result
func (*RuntimeContainer) Exists ¶
func (rc *RuntimeContainer) Exists(id string) bool
Exists ensures that all runtime Config are created correctly
func (*RuntimeContainer) Get ¶
func (rc *RuntimeContainer) Get(id string, isCached bool) interface{}
Get fetches a Service in a return argument and panics if an error happens
func (*RuntimeContainer) GetSecure ¶
func (rc *RuntimeContainer) GetSecure(id string, isCached bool) (interface{}, error)
GetSecure fetches a Service in a return argument and returns an error rather than panics
func (*RuntimeContainer) Merge ¶
func (rc *RuntimeContainer) Merge(c MergeableContainer)
Merge allows to merge containers
func (*RuntimeContainer) RegisterDependencyEvent ¶
func (rc *RuntimeContainer) RegisterDependencyEvent(eventName, dependencyName string)
RegisterDependencyEvent notifies observers about added Config
func (*RuntimeContainer) Scan ¶
func (rc *RuntimeContainer) Scan(id string, dest interface{})
Scan copies a Service identified by id into a typed destination (its a pointer reference) and panics on failure
func (*RuntimeContainer) ScanNonCached ¶
func (rc *RuntimeContainer) ScanNonCached(id string, dest interface{})
ScanNonCached creates a Service every time rc method is called and panics on failure
func (*RuntimeContainer) ScanSecure ¶
func (rc *RuntimeContainer) ScanSecure(id string, isCached bool, dest interface{}) error
ScanSecure copies a service identified by id into a typed destination (its a pointer reference) and returns error on failure
Example ¶
This example scanning of services from container with a possible error output if it happens
cont := NewRuntimeContainer() cont.AddConstructor("parameterOk", func(c Container) (interface{}, error) { return "123456", nil }) cont.AddConstructor("parameterFailed", func(c Container) (interface{}, error) { return "123456", errors.New("Some error") }) var parameterOk string err := cont.ScanSecure("parameterOk", true, ¶meterOk) fmt.Println(err) err = cont.ScanSecure("parameterFailed", true, ¶meterOk) fmt.Println(err) err = cont.ScanSecure("unknownParameter", true, ¶meterOk) fmt.Println(err)
Output: <nil> Some error [check 'parameterFailed' service] Unknown dependency 'unknownParameter'
func (*RuntimeContainer) SetConstructor ¶
func (rc *RuntimeContainer) SetConstructor(id string, constructor Constructor)
SetConstructor adds a new service if it's not existing or overrides an existing one
func (*RuntimeContainer) SetNewMethod ¶
func (rc *RuntimeContainer) SetNewMethod(id string, typedConstructor interface{}, constructorArgumentNames ...string)
SetNewMethod overrides an existing service declaration or adds a new one if it doesn't exist
type RuntimeContainerBuilder ¶
type RuntimeContainerBuilder struct{}
RuntimeContainerBuilder builds a Runtime container
func (RuntimeContainerBuilder) BuildContainerFromConfig ¶
func (rc RuntimeContainerBuilder) BuildContainerFromConfig(trees ...Tree) Container
BuildContainerFromConfig given a config it will build a container, panics if config is wrong
func (RuntimeContainerBuilder) BuildContainerFromConfigSecure ¶
func (rc RuntimeContainerBuilder) BuildContainerFromConfigSecure(trees ...Tree) (Container, error)
BuildContainerFromConfigSecure given a config it will build a container, if config is wrong an error is returned
Source Files ¶
- config.go
- configContainerBuilder.go
- configValidator.go
- constructors.go
- container.go
- containerBuilder.go
- containerHelper.go
- cycleDetector.go
- dependencyCache.go
- errorsChecker.go
- events.go
- garbageCollector.go
- garbageCollectorFuncs.go
- reflectionValidator.go
- runtimeContainer.go
- typedConstructor.go
- utils.go