di/registry/registry.go

50 lines
1.1 KiB
Go
Raw Normal View History

2017-12-03 10:55:10 +00:00
package registry
2017-12-03 13:54:09 +00:00
import (
"fmt"
"reflect"
cdur "git.loafle.net/commons_go/di/util/reflect"
)
2017-12-03 10:55:10 +00:00
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)
}
2017-12-03 13:54:09 +00:00
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
}