From 70ddbc5e5d8a02c9b4f6eaf94d64fcac609d37c2 Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 15 Mar 2018 16:32:04 +0900 Subject: [PATCH] ing --- annotation/inject_test.go | 37 +++++++++++++++++------------------ annotation/resource_test.go | 37 +++++++++++++++++------------------ registry/registry.go | 39 +++++++++++++++++++------------------ 3 files changed, 56 insertions(+), 57 deletions(-) diff --git a/annotation/inject_test.go b/annotation/inject_test.go index 42362c8..80d6073 100644 --- a/annotation/inject_test.go +++ b/annotation/inject_test.go @@ -1,30 +1,29 @@ package annotation import ( - "log" "testing" ) func TestParseInject(t *testing.T) { - a1 := "@Inject" - a, err := ParseInject(a1) - if nil != err { - log.Printf("%v", err) - } - log.Printf("%v", a) + // a1 := "@Inject" + // a, err := ParseInject(a1) + // if nil != err { + // log.Printf("%v", err) + // } + // log.Printf("%v", a) - a2 := "@Inject()" - a, err = ParseInject(a2) - if nil != err { - log.Printf("%v", err) - } - log.Printf("%v", a) + // a2 := "@Inject()" + // a, err = ParseInject(a2) + // if nil != err { + // log.Printf("%v", err) + // } + // log.Printf("%v", a) - a3 := "@Inject(name=string)" - a, err = ParseInject(a3) - if nil != err { - log.Printf("%v", err) - } - log.Printf("%v", a) + // a3 := "@Inject(name=string)" + // a, err = ParseInject(a3) + // if nil != err { + // log.Printf("%v", err) + // } + // log.Printf("%v", a) } diff --git a/annotation/resource_test.go b/annotation/resource_test.go index 85101d9..0e46987 100644 --- a/annotation/resource_test.go +++ b/annotation/resource_test.go @@ -1,30 +1,29 @@ package annotation import ( - "log" "testing" ) func TestParseResource(t *testing.T) { - a1 := "@Resource" - a, err := ParseResource(a1) - if nil != err { - log.Printf("%v", err) - } - log.Printf("%v", a) + // a1 := "@Resource" + // a, err := ParseResource(a1) + // if nil != err { + // log.Printf("%v", err) + // } + // log.Printf("%v", a) - a2 := "@Resource( )" - a, err = ParseResource(a2) - if nil != err { - log.Printf("%v", err) - } - log.Printf("%v", a) + // a2 := "@Resource( )" + // a, err = ParseResource(a2) + // if nil != err { + // log.Printf("%v", err) + // } + // log.Printf("%v", a) - a3 := "@Resource(name=string1232)" - a, err = ParseResource(a3) - if nil != err { - log.Printf("%v", err) - } - log.Printf("%v", a) + // a3 := "@Resource(name=string1232)" + // a, err = ParseResource(a3) + // if nil != err { + // log.Printf("%v", err) + // } + // log.Printf("%v", a) } diff --git a/registry/registry.go b/registry/registry.go index 2c053c5..a6a5c6e 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -22,8 +22,8 @@ type ComponentRegistry interface { RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) RegisterResource(name string, resource interface{}) error - GetInstance(t reflect.Type) (interface{}, error) - GetInstanceByName(name string) (interface{}, error) + GetInstance(t reflect.Type) interface{} + GetInstanceByName(name string) interface{} } func newRegistry() ComponentRegistry { @@ -97,20 +97,20 @@ func (cr *defaultComponentRegistry) RegisterResource(name string, resource inter // GetInstance returns instance of type // t must be pointer of struct -func GetInstance(t reflect.Type) (interface{}, error) { +func GetInstance(t reflect.Type) interface{} { return registry.GetInstance(t) } -func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, error) { +func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) interface{} { if nil == t { - return nil, fmt.Errorf("DI: t[reflect.Type] is nil") + logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] is nil")) } if reflect.Ptr != t.Kind() { - return nil, fmt.Errorf("DI: t[reflect.Type] must be pointer of struct") + logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be pointer of struct")) } if reflect.Struct != t.Elem().Kind() { - return nil, fmt.Errorf("DI: t[reflect.Type] must be pointer of struct") + logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be pointer of struct")) } var err error @@ -120,13 +120,13 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er if !ok { td, err = cr.buildDefinition(t) if nil != err { - return nil, err + logging.Logger().Panic(fmt.Sprintf("DI: %v", err)) } } comV, ok := cr.instanceByType[td.RealType] if ok { - return comV, nil + return comV } v := reflect.New(rt) @@ -139,15 +139,15 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er f := rv.FieldByName(fd.FieldName) if !f.IsValid() { - return nil, fmt.Errorf("DI: Field[%s] is not valid", fd.FieldName) + logging.Logger().Panic(fmt.Sprintf("DI: Field[%s] is not valid", fd.FieldName)) } if !f.CanSet() { - return nil, fmt.Errorf("DI: Field[%s] can not set", fd.FieldName) + logging.Logger().Panic(fmt.Sprintf("DI: Field[%s] can not set", fd.FieldName)) } annotation = fd.GetAnnotation(cda.InjectTag) if nil != annotation { - fV, err = cr.GetInstance(fd.Type) + fV = cr.GetInstance(fd.Type) } annotation = fd.GetAnnotation(cda.ResourceTag) if nil != annotation { @@ -155,29 +155,30 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er if "" == n { n = fd.FieldName } - fV, err = cr.GetInstanceByName(n) + fV = cr.GetInstanceByName(n) } if nil != err { - return nil, err + logging.Logger().Panic(fmt.Sprintf("DI: %v", err)) } f.Set(reflect.ValueOf(fV)) } cr.instanceByType[td.RealType] = v.Interface() - return v.Interface(), nil + return v.Interface() } -func GetInstanceByName(name string) (interface{}, error) { +func GetInstanceByName(name string) interface{} { return registry.GetInstanceByName(name) } -func (cr *defaultComponentRegistry) GetInstanceByName(name string) (interface{}, error) { +func (cr *defaultComponentRegistry) GetInstanceByName(name string) interface{} { v, ok := cr.resourceByName[name] if ok { - return v, nil + return v } - return nil, fmt.Errorf("DI: Resource[%s] is not exist", name) + logging.Logger().Panic(fmt.Sprintf("DI: Resource[%s] is not exist", name)) + return nil } func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefinition, error) {