container

package
v0.0.0-...-7a52cca Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2019 License: MPL-2.0 Imports: 5 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterParameters

func RegisterParameters(c Container, dependenciesMaps ...interface{}) error

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

func ValidateConfigSecure(tree Tree) error

ValidateConfigSecure validates a tree of config options and returns error if something is wrong

Types

type Builder

type Builder interface {
	BuildContainer(trees ...Tree) Container
}

Builder interface which can be used to declare multiple different container builders

type Constructor

type Constructor func(c Container) (interface{}, error)

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 NewCycleDetector

func NewCycleDetector() *CycleDetector

NewCycleDetector constructor

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 Event

type Event struct {
	Name    string
	Service string
}

Event about registration of a specific service

func (Event) IsEmpty

func (e Event) IsEmpty() bool

IsEmpty checks if Event node contains any data

func (Event) String

func (e Event) String() string

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

type NewFuncConstructor func(c Container, isCached bool) (interface{}, error)

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

func (Node) String

func (n Node) String() string

type Observer

type Observer struct {
	Event    string
	Name     string
	Callback interface{}
}

Observer is a service which is interested other services under a certain event

func (Observer) IsEmpty

func (o Observer) IsEmpty() bool

IsEmpty checks if Observer node contains any data

func (Observer) String

func (o Observer) String() string

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, &parameterOk)
fmt.Println(err)

err = cont.ScanSecure("parameterFailed", true, &parameterOk)
fmt.Println(err)

err = cont.ScanSecure("unknownParameter", true, &parameterOk)
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

type Services

type Services []string

Services list of dependencies

func (Services) String

func (ss Services) String() string

type Tree

type Tree []Node

Tree of dependency nodes

func (Tree) ServiceExists

func (t Tree) ServiceExists(serviceID string) bool

ServiceExists checks if the provided name was already registered for a service

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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