beans

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2021 License: GPL-3.0 Imports: 3 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear added in v1.1.0

func Clear() error

Clear clears all registered dependencies. It requires Allow Overrides to be set to TRUE. Use this with caution, it was meant for testing purposes only.

func Exists added in v1.0.2

func Exists(interfaceRef interface{}, name string) bool

Exists indicates if a dependency by the given name exists

The 'interfaceRef' is a reference pointer to the bean interface so the bean factory knows what is the bean type, and it can properly register it. It can be a nil pointer to the interface. There are 2 ways of passing this a nil pointer. Let us assume we have a bean interface called IService. To obtain a nil pointer to the interface:

Option 1: (*IService)(nil)

Eg.   bean.ExistsByType((*IService)(nil), name)

Option 2: var reference *IService

Eg.   bean.Exists(reference, name)

func ExistsByType added in v1.0.2

func ExistsByType(t reflect.Type, name string) bool

ExistsByType indicates if a dependency by the given name exists

func Get

func Get(t reflect.Type, name string) interface{}

Get gets the the instance by the specified name.

func GetPrimary

func GetPrimary(t reflect.Type) interface{}

GetPrimary gets the primary dependency registered in this factory instance, same as primary but with a reflect.Type

func GetPrimaryName

func GetPrimaryName(interfaceRef interface{}) string

GetPrimaryName returns the name of the primary bean. Returns an empty string if no beans exist as primary.

The 'interfaceRef' is a reference pointer to the bean interface so the bean factory knows what is the bean type, and it can properly register it. It can be a nil pointer to the interface. There are 2 ways of passing this a nil pointer. Let us assume we have a bean interface called IService. To obtain a nil pointer to the interface:

Option 1: (*IService)(nil)

Eg.   bean.GetPrimaryName((*IService)(nil))

Option 2: var reference *IService

Eg.   bean.GetPrimaryName(reference)

func GetPrimaryNameByType

func GetPrimaryNameByType(t reflect.Type) string

GetPrimaryNameByType returns the name of the primary bean. Returns an empty string if no beans exist as primary.

func InitComponents added in v1.2.0

func InitComponents()

InitComponents initializes all registered constructors for singleton components. This should be called after any required configuration has been loaded.

func Primary

func Primary(ref interface{}) interface{}

Primary resolves the bean type using the registered bean set as primary

The 'interfaceRef' is a reference pointer to the bean interface so the bean factory knows what is the bean type, and it can properly register it. It can be a nil pointer to the interface. There are 2 ways of passing this a nil pointer. Let us assume we have a bean interface called IService. To obtain a nil pointer to the interface:

Option 1: (*IService)(nil)

Eg.   bean.Primary((*IService)(nil))

Option 2: var reference *IService

Eg.   bean.Primary(reference)

func Register

func Register(interfaceRef interface{}, name string, component interface{}) error

Register registers a bean singleton instance into the factory.

The 'interfaceRef' is a reference pointer to the bean interface so the bean factory knows what is the bean type, and it can properly register it. It can be a nil pointer to the interface. There are 2 ways of passing this a nil pointer. Let us assume we have a bean interface called IService. To obtain a nil pointer to the interface:

Option 1: (*IService)(nil)

Eg.   bean.Register((*IService)(nil), beanName, instance)

Option 2: var reference *IService

Eg.   bean.Register(reference, beanName, instance)

func RegisterByType

func RegisterByType(t reflect.Type, name string, component interface{}) error

RegisterByType registers a bean singleton instance into the factory.

func RegisterFunc

func RegisterFunc(interfaceRef interface{}, name string, fn func() interface{}, singleton ...bool) error

RegisterFunc registers a bean function retriever into the factory. the function could return a singleton instance or could also be used for a constructor.

The 'interfaceRef' is a reference pointer to the bean interface so the bean factory knows what is the bean type, and it can properly register it. It can be a nil pointer to the interface. There are 2 ways of passing this a nil pointer. Let us assume we have a bean interface called IService. To obtain a nil pointer to the interface:

Option 1: (*IService)(nil)

Eg.   bean.RegisterFunc((*IService)(nil), beanName, func() interface{}) { ...

Option 2: var reference *IService

Eg.   bean.RegisterFunc(reference, beanName, func() interface{}) { ...

Parameters:

{interfaceRef}  - The interface reference type for the bean
{name}          - The name of the bean
{fn}            - A constructor function for the bean
{singleton}     - (Optional) a boolean that indicates if the bean should be treated as a singleton.
                  which means, once the constructor is used to create the instance, that instance will
                  be always returned when requesting the bean by the given name. This is useful for lazy
                  initializations of instances where the constructor will only be called when requested
                  rather than on an init.

func RegisterFuncByType

func RegisterFuncByType(t reflect.Type, name string, fn func() interface{}, singleton ...bool) error

RegisterFuncByType registers a bean function retriever into the factory, the function could return a singleton instance or could also be used for a constructor.

Parameters:

{t}          - The reference type for the bean
{name}       - The name of the bean
{fn}         - A constructor function for the bean
{singleton}  - (Optional) a boolean that indicates if the bean should be treated as a singleton.
               which means, once the constructor is used to create the instance, that instance will
               be always returned when requesting the bean by the given name. This is useful for lazy
               initializations of instances where the constructor will only be called when requested
               rather than on an init.

func Resolve

func Resolve(ref interface{}, name string) interface{}

Resolve resolves the bean type by the given name.

The 'interfaceRef' is a reference pointer to the bean interface so the bean factory knows what is the bean type, and it can properly register it. It can be a nil pointer to the interface. There are 2 ways of passing this a nil pointer. Let us assume we have a bean interface called IService. To obtain a nil pointer to the interface:

Option 1: (*IService)(nil)

Eg.   bean.Resolve((*IService)(nil), beanName)

Option 2: var reference *IService

Eg.   bean.Resolve(reference, beanName)

func SetAllowOverrides

func SetAllowOverrides(allow bool)

SetAllowOverrides is normally used when testing. It allows a registered bean to be overwritten by another implementation, like a Mock

func SetLogger

func SetLogger(l ILogger)

SetLogger sets an implementation of ILogger to be used as the logger for the beans package

func SetPrimary

func SetPrimary(interfaceRef interface{}, name string, replace ...bool) error

SetPrimary sets the primary bean name to be used.

The 'interfaceRef' is a reference pointer to the bean interface so the bean factory knows what is the bean type, and it can properly register it. It can be a nil pointer to the interface. There are 2 ways of passing this a nil pointer. Let us assume we have a bean interface called IService. To obtain a nil pointer to the interface:

Option 1: (*IService)(nil)

Eg.   bean.SetPrimary((*IService)(nil), beanName)

Option 2: var reference *IService

Eg.   bean.SetPrimary(reference, beanName)

func SetPrimaryByType

func SetPrimaryByType(t reflect.Type, name string, replace ...bool) error

SetPrimaryByType sets the primary bean name to be used.

Types

type ErrorCallback added in v1.1.1

type ErrorCallback func(error)

ErrorCallback defines a function callback that can be registered when errors occur

type IFirstTimeResolveHandler added in v1.2.0

type IFirstTimeResolveHandler interface {
	OnFirstTimeResolve()
}

IFirstTimeResolveHandler defines an optional contract for dependencies to trigger a function on the first time of a successful get/resolve. This function will not get triggered on sub-sequent resolved. It is meant for lazy initialization of components where said initialization is only desired when the component is used.

The implementation of this interface is optional, the beans manager will verify if this contract is implemented in a bean and trigger the handler(s) accordingly

type ILogCallback added in v1.2.0

type ILogCallback interface {
	// SetErrorCallback sets a callback that will be triggered when an error occurs
	SetErrorCallback(callback ErrorCallback)
	// SetInfoCallback sets a callback that will be triggered when an info message is generated
	SetInfoCallback(callback MessageCallback)
	// SetDebugCallback sets a callback that will be triggered when a debug message is generated
	SetDebugCallback(callback MessageCallback)
}

ILogCallback defines the contract for logging callback assignments for this package

func LogCallbacks added in v1.2.0

func LogCallbacks() ILogCallback

LogCallbacks returns a handler that allows to register individual callbacks to be used by the beans package to report errors, info messages and/or debug messages

type ILogger added in v1.2.0

type ILogger interface {
	// Error logs an error to the logger
	Error(err error)
	// Info logs an info message to the logger
	Info(msg string)
	// Debug logs a debug message to the logger
	Debug(msg string)
}

ILogger defines the contract for a full logger that can be used by this package

type IResolveHandler added in v1.2.0

type IResolveHandler interface {
	// OnResolve is a handler that gets triggered when a bean is resolved/fetched from the beans manager.
	OnResolve()
}

IResolveHandler defines an optional contract for dependencies to trigger a function on a successful get/resolve

The implementation of this interface is optional, the beans manager will verify if this contract is implemented in a bean and trigger the handler(s) accordingly

type MessageCallback added in v1.2.0

type MessageCallback func(string)

MessageCallback defines a function callback that can be registered to broadcast messages

Jump to

Keyboard shortcuts

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