bug fixed

This commit is contained in:
병준 박 2019-11-13 23:11:26 +09:00
parent 9508895ed0
commit d08fa9dbe7

View File

@ -17,7 +17,7 @@ func init() {
// Regist is interface // Regist is interface
type Regist interface { type Regist interface {
RegisterType(t reflect.Type) RegisterType(t reflect.Type) error
RegisterResource(name string, resource interface{}) error RegisterResource(name string, resource interface{}) error
GetInstance(t reflect.Type) (interface{}, error) GetInstance(t reflect.Type) (interface{}, error)
@ -51,31 +51,31 @@ func newRegistry() Regist {
} }
// RegisterType is function // RegisterType is function
func RegisterType(t reflect.Type) { func RegisterType(t reflect.Type) error {
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())
} }
registry.RegisterType(t) return registry.RegisterType(t)
} }
func (r *Registry) RegisterType(t reflect.Type) { func (r *Registry) RegisterType(t reflect.Type) error {
if nil == t { if nil == t {
log.Panicf("DI: t[reflect.Type] is nil") return fmt.Errorf("DI: t[reflect.Type] is nil")
} }
if !luReflect.IsTypeKind(t, reflect.Struct, true) { if !luReflect.IsTypeKind(t, reflect.Struct, true) {
log.Panicf("DI: t[reflect.Type] must be specified but is %v", t) return fmt.Errorf("DI: t[reflect.Type] must be specified but is %v", t)
} }
td, err := r.buildDefinition(t) td, err := r.buildDefinition(t)
if nil != err { if nil != err {
log.Panicf("DI: %v", err) return fmt.Errorf("DI: %v", err)
} }
if _, ok := r.definitionByType[td.Type]; ok { if _, ok := r.definitionByType[td.Type]; ok {
log.Panicf("DI: The type[%s] of Component is exist already", td.FullName) return fmt.Errorf("DI: The type[%s] of Component is exist already", td.FullName)
} }
r.definitionByType[td.Type] = td r.definitionByType[td.Type] = td
@ -89,9 +89,11 @@ func (r *Registry) RegisterType(t reflect.Type) {
} }
if eTD, ok := r.definitionByName[name]; ok { if eTD, ok := r.definitionByName[name]; ok {
log.Panicf("DI: The name[%s] of Component is exist already type[%s]", name, eTD.FullName) return fmt.Errorf("DI: The name[%s] of Component is exist already type[%s]", name, eTD.FullName)
} }
r.definitionByName[name] = td r.definitionByName[name] = td
return nil
} }
func RegisterResource(name string, resource interface{}) error { func RegisterResource(name string, resource interface{}) error {