From 0a16fb1bd1313e8cec68603be7628255d7318baa Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 6 Dec 2017 09:48:03 +0900 Subject: [PATCH] ing --- registry/definition.go | 6 +++++ registry/registry.go | 51 ++++++++++++++++++++++++++------------- registry/registry_test.go | 18 +++++++++++--- 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/registry/definition.go b/registry/definition.go index 7329985..61dc103 100644 --- a/registry/definition.go +++ b/registry/definition.go @@ -1,12 +1,14 @@ package registry import ( + "fmt" "reflect" cda "git.loafle.net/commons_go/di/annotation" ) type TypeDefinition struct { + FullName string PkgName string TypeName string Type reflect.Type @@ -23,3 +25,7 @@ type FieldDefinition struct { Annotations map[string]cda.Annotation } + +func FullName(pkgName, typeName string) string { + return fmt.Sprintf("%s/%s", pkgName, typeName) +} diff --git a/registry/registry.go b/registry/registry.go index c3ec83a..bec07b8 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -7,6 +7,7 @@ import ( cda "git.loafle.net/commons_go/di/annotation" cdur "git.loafle.net/commons_go/di/util/reflect" + "git.loafle.net/commons_go/logging" ) func init() { @@ -16,7 +17,7 @@ func init() { var registry ComponentRegistry type ComponentRegistry interface { - RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) error + RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) GetInstance(t reflect.Type) (interface{}, error) GetInstanceByName(name string) (interface{}, error) @@ -24,45 +25,56 @@ type ComponentRegistry interface { func newRegistry() ComponentRegistry { r := &defaultComponentRegistry{} - r.definitionsByType = make(map[reflect.Type]*TypeDefinition, 0) + r.definitionByType = make(map[reflect.Type]*TypeDefinition, 0) r.definitionByName = make(map[string]*TypeDefinition, 0) return r } type defaultComponentRegistry struct { - definitionsByType map[reflect.Type]*TypeDefinition - + definitionByType map[reflect.Type]*TypeDefinition definitionByName map[string]*TypeDefinition } -func RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) error { - return registry.RegisterType(t, ca) +func RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) { + registry.RegisterType(t, ca) } -func (cr *defaultComponentRegistry) RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) error { +func (cr *defaultComponentRegistry) RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) { if nil == t { - return fmt.Errorf("DI: t[reflect.Type] is nil") + logging.Logger().Panic("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) + logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be specified but is %v", t)) } td, err := cr.buildDefinition(t) if nil != err { - return err + logging.Logger().Panic(fmt.Sprintf("DI: %v", err)) } - cr.definitionsByType[td.RealType] = td + if _, ok := cr.definitionByType[td.RealType]; ok { + logging.Logger().Panic(fmt.Sprintf("DI: The type[%s] of Component is exist already", td.FullName)) + } + cr.definitionByType[td.RealType] = td + names := make([]string, 0) if nil != ca { if nil != ca.Names && 0 < len(ca.Names) { for _, n := range ca.Names { - cr.definitionByName[n] = td + names = append(names, n) } } } + if 0 == len(names) { + names = append(names, td.FullName) + } - return nil + for _, n := range names { + if eTD, ok := cr.definitionByName[n]; ok { + logging.Logger().Panic(fmt.Sprintf("DI: The name[%s] of Component is exist already type[%s]", n, eTD.FullName)) + } + cr.definitionByName[n] = td + } } func GetInstance(t reflect.Type) (interface{}, error) { @@ -72,15 +84,19 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er if nil == t { return nil, fmt.Errorf("DI: t[reflect.Type] is nil") } + var err error - cd, ok := cr.definitionsByType[t] + + rt, _, _ := cdur.GetTypeInfo(t) + td, ok := cr.definitionByType[rt] if !ok { - cd, err = cr.buildDefinition(t) + td, err = cr.buildDefinition(t) if nil != err { return nil, err } } - log.Printf("%v", cd) + + log.Printf("%v", td) return nil, nil } @@ -95,11 +111,12 @@ func (cr *defaultComponentRegistry) GetInstanceByName(name string) (interface{}, func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefinition, error) { if nil == t { - return nil, fmt.Errorf("DI: t[reflect.Type] is nil") + return nil, fmt.Errorf("t[reflect.Type] is nil") } rt, pkgName, tName := cdur.GetTypeInfo(t) cd := &TypeDefinition{} + cd.FullName = FullName(pkgName, tName) cd.PkgName = pkgName cd.TypeName = tName cd.Type = t diff --git a/registry/registry_test.go b/registry/registry_test.go index abe06ba..7601aee 100644 --- a/registry/registry_test.go +++ b/registry/registry_test.go @@ -8,16 +8,26 @@ import ( ) func TestRegisterType(t *testing.T) { - RegisterType(reflect.TypeOf((*TestStruct)(nil)), &cda.ComponentAnnotation{}) - RegisterType(reflect.TypeOf((*TestStruct)(nil)), &cda.ComponentAnnotation{ + RegisterType(reflect.TypeOf((*TestStruct1)(nil)), &cda.ComponentAnnotation{}) + RegisterType(reflect.TypeOf((*TestStruct2)(nil)), &cda.ComponentAnnotation{ Names: []string{"test1", "test2"}, }) - RegisterType(reflect.TypeOf((*TestStruct)(nil)), &cda.ComponentAnnotation{ + RegisterType(reflect.TypeOf((*TestStruct3)(nil)), &cda.ComponentAnnotation{ Names: []string{"test1", "test2"}, }) } -type TestStruct struct { +type TestStruct1 struct { + Name1 string `annotation:"@Inject"` + Name2 string `annotation:"@Inject()"` + Name3 string `annotation:"@Inject(name=test)"` +} +type TestStruct2 struct { + Name1 string `annotation:"@Inject"` + Name2 string `annotation:"@Inject()"` + Name3 string `annotation:"@Inject(name=test)"` +} +type TestStruct3 struct { Name1 string `annotation:"@Inject"` Name2 string `annotation:"@Inject()"` Name3 string `annotation:"@Inject(name=test)"`