This commit is contained in:
crusader 2017-12-06 09:48:03 +09:00
parent c3f8e35ea0
commit 0a16fb1bd1
3 changed files with 54 additions and 21 deletions

View File

@ -1,12 +1,14 @@
package registry package registry
import ( import (
"fmt"
"reflect" "reflect"
cda "git.loafle.net/commons_go/di/annotation" cda "git.loafle.net/commons_go/di/annotation"
) )
type TypeDefinition struct { type TypeDefinition struct {
FullName string
PkgName string PkgName string
TypeName string TypeName string
Type reflect.Type Type reflect.Type
@ -23,3 +25,7 @@ type FieldDefinition struct {
Annotations map[string]cda.Annotation Annotations map[string]cda.Annotation
} }
func FullName(pkgName, typeName string) string {
return fmt.Sprintf("%s/%s", pkgName, typeName)
}

View File

@ -7,6 +7,7 @@ import (
cda "git.loafle.net/commons_go/di/annotation" cda "git.loafle.net/commons_go/di/annotation"
cdur "git.loafle.net/commons_go/di/util/reflect" cdur "git.loafle.net/commons_go/di/util/reflect"
"git.loafle.net/commons_go/logging"
) )
func init() { func init() {
@ -16,7 +17,7 @@ func init() {
var registry ComponentRegistry var registry ComponentRegistry
type ComponentRegistry interface { type ComponentRegistry interface {
RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) error RegisterType(t reflect.Type, ca *cda.ComponentAnnotation)
GetInstance(t reflect.Type) (interface{}, error) GetInstance(t reflect.Type) (interface{}, error)
GetInstanceByName(name string) (interface{}, error) GetInstanceByName(name string) (interface{}, error)
@ -24,45 +25,56 @@ type ComponentRegistry interface {
func newRegistry() ComponentRegistry { func newRegistry() ComponentRegistry {
r := &defaultComponentRegistry{} 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) r.definitionByName = make(map[string]*TypeDefinition, 0)
return r return r
} }
type defaultComponentRegistry struct { type defaultComponentRegistry struct {
definitionsByType map[reflect.Type]*TypeDefinition definitionByType map[reflect.Type]*TypeDefinition
definitionByName map[string]*TypeDefinition definitionByName map[string]*TypeDefinition
} }
func RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) error { func RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) {
return registry.RegisterType(t, ca) 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 { 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) { 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) td, err := cr.buildDefinition(t)
if nil != err { 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 {
if nil != ca.Names && 0 < len(ca.Names) { if nil != ca.Names && 0 < len(ca.Names) {
for _, n := range 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) { func GetInstance(t reflect.Type) (interface{}, error) {
@ -72,15 +84,19 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er
if nil == t { if nil == t {
return nil, fmt.Errorf("DI: t[reflect.Type] is nil") return nil, fmt.Errorf("DI: t[reflect.Type] is nil")
} }
var err error var err error
cd, ok := cr.definitionsByType[t]
rt, _, _ := cdur.GetTypeInfo(t)
td, ok := cr.definitionByType[rt]
if !ok { if !ok {
cd, err = cr.buildDefinition(t) td, err = cr.buildDefinition(t)
if nil != err { if nil != err {
return nil, err return nil, err
} }
} }
log.Printf("%v", cd)
log.Printf("%v", td)
return nil, nil return nil, nil
} }
@ -95,11 +111,12 @@ func (cr *defaultComponentRegistry) GetInstanceByName(name string) (interface{},
func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefinition, error) { func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefinition, error) {
if nil == t { 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) rt, pkgName, tName := cdur.GetTypeInfo(t)
cd := &TypeDefinition{} cd := &TypeDefinition{}
cd.FullName = FullName(pkgName, tName)
cd.PkgName = pkgName cd.PkgName = pkgName
cd.TypeName = tName cd.TypeName = tName
cd.Type = t cd.Type = t

View File

@ -8,16 +8,26 @@ import (
) )
func TestRegisterType(t *testing.T) { func TestRegisterType(t *testing.T) {
RegisterType(reflect.TypeOf((*TestStruct)(nil)), &cda.ComponentAnnotation{}) RegisterType(reflect.TypeOf((*TestStruct1)(nil)), &cda.ComponentAnnotation{})
RegisterType(reflect.TypeOf((*TestStruct)(nil)), &cda.ComponentAnnotation{ RegisterType(reflect.TypeOf((*TestStruct2)(nil)), &cda.ComponentAnnotation{
Names: []string{"test1", "test2"}, Names: []string{"test1", "test2"},
}) })
RegisterType(reflect.TypeOf((*TestStruct)(nil)), &cda.ComponentAnnotation{ RegisterType(reflect.TypeOf((*TestStruct3)(nil)), &cda.ComponentAnnotation{
Names: []string{"test1", "test2"}, 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"` Name1 string `annotation:"@Inject"`
Name2 string `annotation:"@Inject()"` Name2 string `annotation:"@Inject()"`
Name3 string `annotation:"@Inject(name=test)"` Name3 string `annotation:"@Inject(name=test)"`