Documentation ¶
Index ¶
- Variables
- func GetByImpl(interfacePtr interface{}) (module interface{}, exist bool)
- func GetByName(name ModuleName) (module interface{}, exist bool)
- func GetByType(moduleType interface{}) (module interface{}, exist bool)
- func Inject(ctrl interface{}) (allInjected bool)
- func MustGetByImpl(interfacePtr interface{}) interface{}
- func MustGetByName(name ModuleName) interface{}
- func MustGetByType(moduleType interface{}) interface{}
- func MustInject(ctrl interface{})
- func ProvideImpl(interfacePtr interface{}, moduleImpl interface{})
- func ProvideName(name ModuleName, module interface{})
- func ProvideType(module interface{})
- func SetLogger(logger Logger)
- type LogLevel
- type Logger
- type ModuleContainer
- func (m *ModuleContainer) GetByImpl(interfacePtr interface{}) (module interface{}, exist bool)
- func (m *ModuleContainer) GetByName(name ModuleName) (module interface{}, exist bool)
- func (m *ModuleContainer) GetByType(moduleType interface{}) (module interface{}, exist bool)
- func (m *ModuleContainer) Inject(ctrl interface{}) (allInjected bool)
- func (m *ModuleContainer) MustGetByImpl(interfacePtr interface{}) interface{}
- func (m *ModuleContainer) MustGetByName(name ModuleName) interface{}
- func (m *ModuleContainer) MustGetByType(moduleType interface{}) interface{}
- func (m *ModuleContainer) MustInject(ctrl interface{})
- func (m *ModuleContainer) ProvideImpl(interfacePtr interface{}, moduleImpl interface{})
- func (m *ModuleContainer) ProvideName(name ModuleName, module interface{})
- func (m *ModuleContainer) ProvideType(module interface{})
- func (m *ModuleContainer) SetLogger(logger Logger)
- type ModuleName
Constants ¶
This section is empty.
Variables ¶
var LogLeftArrow = func(arg1, arg2, arg3 string) { fmt.Printf("[XMODULE] %-4s %-30s <-- %s\n", arg1, arg2, arg3) }
LogLeftArrow is the logger function with <-- (used in LogName, LogType, LogImpl). You can overwrite this function.
var LogRightArrow = func(arg1, arg2, arg3 string) { fmt.Printf("[XMODULE] %-4s %-30s --> %s\n", arg1, arg2, arg3) }
LogRightArrow is the logger function with --> (used in LogInject, LogInjectField). You can overwrite this function.
Functions ¶
func GetByImpl ¶
func GetByImpl(interfacePtr interface{}) (module interface{}, exist bool)
GetByImpl returns a module by interface pointer, panics when using invalid interface pointer.
func GetByName ¶
func GetByName(name ModuleName) (module interface{}, exist bool)
GetByName returns the module provided by name, panics when using invalid module name.
func GetByType ¶
func GetByType(moduleType interface{}) (module interface{}, exist bool)
GetByType returns a module provided by type, panics when using nil type.
func Inject ¶
func Inject(ctrl interface{}) (allInjected bool)
Inject injects into struct fields using its module tag, returns true if all fields with `module` tag has been injected.
Example:
type AStruct struct { unexportedField string // -> ignore ExportedField1 string // -> ignore ExportedField2 string `module:""` // -> ignore ExportedField3 string `module:"-"` // -> ignore ExportedField4 string `module:"name"` // -> inject by name ExportedField5 string `module:"~"` // -> inject by type or impl }
func MustGetByImpl ¶
func MustGetByImpl(interfacePtr interface{}) interface{}
MustGetByImpl returns a module by moduleType, panics when using invalid interface pointer or module not found.
func MustGetByName ¶
func MustGetByName(name ModuleName) interface{}
MustGetByName returns a module provided by name, panics when using invalid module name or module not found.
func MustGetByType ¶
func MustGetByType(moduleType interface{}) interface{}
MustGetByType returns a module provided by type, panics when using nil type or module not found.
func MustInject ¶
func MustInject(ctrl interface{})
MustInject injects into struct fields using its module tag, panics when not all fields with `module` tag are injected.
Example:
type AStruct struct { unexportedField string // -> ignore ExportedField1 string // -> ignore ExportedField2 string `module:""` // -> ignore ExportedField3 string `module:"-"` // -> ignore ExportedField4 string `module:"name"` // -> inject by name ExportedField5 string `module:"~"` // -> inject by type or impl }
func ProvideImpl ¶
func ProvideImpl(interfacePtr interface{}, moduleImpl interface{})
ProvideImpl provides a module using the interface type, panics when using invalid interface pointer or nil module.
Example:
ProvideImpl((*Interface)(nil), &Module{}) GetByImpl((*Interface)(nil))
func ProvideName ¶
func ProvideName(name ModuleName, module interface{})
ProvideName provides a module using a ModuleName, panics when using invalid module name or nil module.
func ProvideType ¶
func ProvideType(module interface{})
ProvideType provides a module using its type, panics when using nil module.
Types ¶
type LogLevel ¶
type LogLevel uint8
LogLevel represents ModuleContainer's logger level.
const ( // LogName logs only when ModuleContainer.ProvideName invoked. LogName LogLevel = 1 << iota // LogType logs only when ModuleContainer.ProvideType invoked. LogType // LogImpl logs only when ModuleContainer.ProvideImpl invoked. LogImpl // LogInject logs only when ModuleContainer.Inject invoked. LogInject // LogAll logs when ModuleContainer.ProvideName, ModuleContainer.ProvideType, ModuleContainer.ProvideImpl, ModuleContainer.Inject invoked. LogAll = LogName | LogType | LogImpl | LogInject // LogSilent never logs, equals to disable the logger. LogSilent = LogLevel(0) )
type Logger ¶
type Logger interface { // LogName invoked by ModuleContainer.ProvideName. LogName(moduleName, moduleTyp string) // LogType invoked by ModuleContainer.ProvideType. LogType(moduleTyp string) // LogImpl invoked by ModuleContainer.ProvideImpl. LogImpl(interfaceTyp, moduleTyp string) // LogInjectField invoked by ModuleContainer.Inject. LogInjectField(moduleName, structTyp, fieldName, fieldTyp string) // LogInject invoked by ModuleContainer.Inject. LogInject(structTyp string, num int) }
Logger represents ModuleContainer's logger.
func DefaultLogger ¶
DefaultLogger creates a default Logger instance. Log style see LogName, LogType, LogImpl, LogInject. Note that the red color represents the module and field name (~ represents no module name), and the yellow color represents the module and field type.
type ModuleContainer ¶
type ModuleContainer struct {
// contains filtered or unexported fields
}
ModuleContainer represents a module container.
func NewModuleContainer ¶
func NewModuleContainer() *ModuleContainer
NewModuleContainer creates an empty ModuleContainer with Logger with LogAll flag.
func (*ModuleContainer) GetByImpl ¶
func (m *ModuleContainer) GetByImpl(interfacePtr interface{}) (module interface{}, exist bool)
GetByImpl returns a module by interface pointer, panics when using invalid interface pointer.
func (*ModuleContainer) GetByName ¶
func (m *ModuleContainer) GetByName(name ModuleName) (module interface{}, exist bool)
GetByName returns the module provided by name, panics when using invalid module name.
func (*ModuleContainer) GetByType ¶
func (m *ModuleContainer) GetByType(moduleType interface{}) (module interface{}, exist bool)
GetByType returns a module provided by type, panics when using nil type.
func (*ModuleContainer) Inject ¶
func (m *ModuleContainer) Inject(ctrl interface{}) (allInjected bool)
Inject injects into struct fields using its module tag, returns true if all fields with `module` tag has been injected.
Example:
type AStruct struct { unexportedField string // -> ignore ExportedField1 string // -> ignore ExportedField2 string `module:""` // -> ignore ExportedField3 string `module:"-"` // -> ignore ExportedField4 string `module:"name"` // -> inject by name ExportedField5 string `module:"~"` // -> inject by type or impl }
func (*ModuleContainer) MustGetByImpl ¶
func (m *ModuleContainer) MustGetByImpl(interfacePtr interface{}) interface{}
MustGetByImpl returns a module by moduleType, panics when using invalid interface pointer or module not found.
func (*ModuleContainer) MustGetByName ¶
func (m *ModuleContainer) MustGetByName(name ModuleName) interface{}
MustGetByName returns a module provided by name, panics when using invalid module name or module not found.
func (*ModuleContainer) MustGetByType ¶
func (m *ModuleContainer) MustGetByType(moduleType interface{}) interface{}
MustGetByType returns a module provided by type, panics when using nil type or module not found.
func (*ModuleContainer) MustInject ¶
func (m *ModuleContainer) MustInject(ctrl interface{})
MustInject injects into struct fields using its module tag, panics when not all fields with `module` tag are injected.
Example:
type AStruct struct { unexportedField string // -> ignore ExportedField1 string // -> ignore ExportedField2 string `module:""` // -> ignore ExportedField3 string `module:"-"` // -> ignore ExportedField4 string `module:"name"` // -> inject by name ExportedField5 string `module:"~"` // -> inject by type or impl }
func (*ModuleContainer) ProvideImpl ¶
func (m *ModuleContainer) ProvideImpl(interfacePtr interface{}, moduleImpl interface{})
ProvideImpl provides a module using the interface type, panics when using invalid interface pointer or nil module.
Example:
ProvideImpl((*Interface)(nil), &Module{}) GetByImpl((*Interface)(nil))
func (*ModuleContainer) ProvideName ¶
func (m *ModuleContainer) ProvideName(name ModuleName, module interface{})
ProvideName provides a module using a ModuleName, panics when using invalid module name or nil module.
func (*ModuleContainer) ProvideType ¶
func (m *ModuleContainer) ProvideType(module interface{})
ProvideType provides a module using its type, panics when using nil module.
func (*ModuleContainer) SetLogger ¶
func (m *ModuleContainer) SetLogger(logger Logger)
SetLogger sets the Logger for ModuleContainer.
Example:
SetLogger(DefaultLogger(LogAll)) // set to default logger SetLogger(DefaultLogger(LogSilent)) // disable logger
type ModuleName ¶
type ModuleName string
ModuleName represents a global module name, and it could not be empty, - and ~.
func (ModuleName) String ¶
func (m ModuleName) String() string
String returns the string value of ModuleName.