50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package registry
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
|
|
cdur "git.loafle.net/commons_go/di/util/reflect"
|
|
)
|
|
|
|
type ComponentRegistry interface {
|
|
RegisterType(t reflect.Type, name string) error
|
|
RegisterFactory(i interface{}) error
|
|
|
|
GetInstance(t reflect.Type) (interface{}, error)
|
|
GetInstanceByName(name string) (interface{}, error)
|
|
}
|
|
|
|
type defaultComponentRegistry struct {
|
|
definitions map[string]reflect.Type
|
|
}
|
|
|
|
func (cr *defaultComponentRegistry) RegisterType(t reflect.Type, name string) error {
|
|
if nil == t {
|
|
return fmt.Errorf("DI: t[reflect.Type] is nil")
|
|
}
|
|
if !cdur.IsTypeKind(t, reflect.Struct, true) {
|
|
return fmt.Errorf("DI: t[reflect.Type] must be specified but is %v", t)
|
|
}
|
|
|
|
rt, pkgName, tName := cdur.GetTypeName(t, true)
|
|
cr.definitions[name] = rt
|
|
|
|
return nil
|
|
}
|
|
|
|
func (cr *defaultComponentRegistry) RegisterFactory(i interface{}) error {
|
|
|
|
return nil
|
|
}
|
|
|
|
func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
func (cr *defaultComponentRegistry) GetInstanceByName(name string) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
}
|