This commit is contained in:
crusader 2018-03-21 21:33:53 +09:00
parent e1da1222c6
commit dc36d262ae

View File

@ -19,7 +19,7 @@ func init() {
var registry ComponentRegistry var registry ComponentRegistry
type ComponentRegistry interface { type ComponentRegistry interface {
RegisterType(t reflect.Type) error RegisterType(t reflect.Type)
RegisterResource(name string, resource interface{}) error RegisterResource(name string, resource interface{}) error
GetInstance(t reflect.Type) (interface{}, error) GetInstance(t reflect.Type) (interface{}, error)
@ -45,30 +45,30 @@ type defaultComponentRegistry struct {
resourceByName map[string]interface{} resourceByName map[string]interface{}
} }
func RegisterType(t reflect.Type) error { func RegisterType(t reflect.Type) {
pc, _, _, ok := runtime.Caller(1) pc, _, _, ok := runtime.Caller(1)
details := runtime.FuncForPC(pc) details := runtime.FuncForPC(pc)
if ok && details != nil { if ok && details != nil {
log.Printf("called from %s", details.Name()) log.Printf("called from %s", details.Name())
} }
return registry.RegisterType(t) registry.RegisterType(t)
} }
func (cr *defaultComponentRegistry) RegisterType(t reflect.Type) error { func (cr *defaultComponentRegistry) RegisterType(t reflect.Type) {
if nil == t { if nil == t {
return fmt.Errorf("DI: t[reflect.Type] is nil") log.Panicf("DI: t[reflect.Type] is nil")
} }
if !cur.IsTypeKind(t, reflect.Struct, true) { if !cur.IsTypeKind(t, reflect.Struct, true) {
return fmt.Errorf("DI: t[reflect.Type] must be specified but is %v", t) log.Panicf("DI: t[reflect.Type] must be specified but is %v", t)
} }
td, err := cr.buildDefinition(t) td, err := cr.buildDefinition(t)
if nil != err { if nil != err {
return fmt.Errorf("DI: %v", err) log.Panicf("DI: %v", err)
} }
if _, ok := cr.definitionByType[td.RealType]; ok { if _, ok := cr.definitionByType[td.RealType]; ok {
return fmt.Errorf("DI: The type[%s] of Component is exist already", td.FullName) log.Panicf("DI: The type[%s] of Component is exist already", td.FullName)
} }
cr.definitionByType[td.RealType] = td cr.definitionByType[td.RealType] = td
@ -82,11 +82,9 @@ func (cr *defaultComponentRegistry) RegisterType(t reflect.Type) error {
} }
if eTD, ok := cr.definitionByName[name]; ok { if eTD, ok := cr.definitionByName[name]; ok {
return fmt.Errorf("DI: The name[%s] of Component is exist already type[%s]", name, eTD.FullName) log.Panicf("DI: The name[%s] of Component is exist already type[%s]", name, eTD.FullName)
} }
cr.definitionByName[name] = td cr.definitionByName[name] = td
return nil
} }
func RegisterResource(name string, resource interface{}) error { func RegisterResource(name string, resource interface{}) error {