xmodule

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2021 License: MIT Imports: 4 Imported by: 0

README

xmodule

Dependencies

  • xcolor
  • xtesting*

Documents

Types
  • type ModuleName string
  • type ModuleContainer struct
  • type LogLevel int8
  • type Logger interface
Variables
  • var LogLeftArrow func(arg1, arg2, arg3 string)
  • var LogRightArrow func(arg1, arg2, arg3 string)
Constants
  • const LogName LogLevel
  • const LogType LogLevel
  • const LogImpl LogLevel
  • const LogInject LogLevel
  • const LogAll LogLevel
  • const LogSilent LogLevel
Functions
  • func NewModuleContainer() *ModuleContainer
  • func SetLogger(logger Logger)
  • func ProvideName(name ModuleName, module interface{})
  • func ProvideType(module interface{})
  • func ProvideImpl(interfacePtr interface{}, moduleImpl interface{})
  • func GetByName(name ModuleName) (module interface{}, exist bool)
  • func MustGetByName(name ModuleName) interface{}
  • func GetByType(moduleType interface{}) (module interface{}, exist bool)
  • func MustGetByType(moduleType interface{}) interface{}
  • func GetByImpl(interfacePtr interface{}) (module interface{}, exist bool)
  • func MustGetByImpl(interfacePtr interface{}) interface{}
  • func Inject(ctrl interface{}) (allInjected bool)
  • func MustInject(ctrl interface{})
  • func DefaultLogger(level LogLevel) Logger
Methods
  • func (m ModuleName) String() string
  • func (m *ModuleContainer) SetLogger(logger Logger)
  • func (m *ModuleContainer) ProvideName(name ModuleName, module interface{})
  • func (m *ModuleContainer) ProvideType(module interface{})
  • func (m *ModuleContainer) ProvideImpl(interfacePtr interface{}, moduleImpl interface{})
  • func (m *ModuleContainer) GetByName(name ModuleName) (module interface{}, exist bool)
  • func (m *ModuleContainer) MustGetByName(name ModuleName) interface{}
  • func (m *ModuleContainer) GetByType(moduleType interface{}) (module interface{}, exist bool)
  • func (m *ModuleContainer) MustGetByType(moduleType interface{}) interface{}
  • func (m *ModuleContainer) GetByImpl(interfacePtr interface{}) (module interface{}, exist bool)
  • func (m *ModuleContainer) MustGetByImpl(interfacePtr interface{}) interface{}
  • func (m *ModuleContainer) Inject(ctrl interface{}) (allInjected bool)
  • func (m *ModuleContainer) MustInject(ctrl interface{})

Documentation

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
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.

func SetLogger

func SetLogger(logger Logger)

SetLogger sets the Logger for ModuleContainer.

Example:

SetLogger(DefaultLogger(LogAll))    // set to default logger
SetLogger(DefaultLogger(LogSilent)) // disable logger

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

func DefaultLogger(level LogLevel) Logger

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.

Jump to

Keyboard shortcuts

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