This commit is contained in:
crusader 2018-03-15 22:49:25 +09:00
parent 1a82266014
commit f2756c481d
2 changed files with 22 additions and 3 deletions

View File

@ -25,6 +25,7 @@ type ComponentRegistry interface {
GetInstance(t reflect.Type) interface{} GetInstance(t reflect.Type) interface{}
GetInstanceByName(name string) interface{} GetInstanceByName(name string) interface{}
GetInstancesByAnnotationName(n string) []interface{}
} }
func newRegistry() ComponentRegistry { func newRegistry() ComponentRegistry {
@ -185,6 +186,23 @@ func (cr *defaultComponentRegistry) GetInstanceByName(name string) interface{} {
return nil return nil
} }
// GetInstancesByAnnotationName returns instance of annotated
// n must be name of registered annotation
func GetInstancesByAnnotationName(n string) []interface{} {
return registry.GetInstancesByAnnotationName(n)
}
func (cr *defaultComponentRegistry) GetInstancesByAnnotationName(n string) []interface{} {
instances := make([]interface{}, 0)
for _, td := range cr.definitionByType {
if nil != td.GetAnnotation(n) {
instances = append(instances, cr.GetInstance(td.Type))
}
}
return instances
}
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("t[reflect.Type] is nil") return nil, fmt.Errorf("t[reflect.Type] is nil")
@ -242,10 +260,7 @@ func parseFields(t reflect.Type, td *TypeDefinition) {
} }
func parseAnonymousField(f reflect.StructField, td *TypeDefinition) bool { func parseAnonymousField(f reflect.StructField, td *TypeDefinition) bool {
logging.Logger().Debug(fmt.Sprintf("Anonymous Pkg:%s, Type: %s", f.Type.PkgPath(), f.Type.Name()))
ct := reflect.TypeOf((*cda.TypeAnnotation)(nil)).Elem() ct := reflect.TypeOf((*cda.TypeAnnotation)(nil)).Elem()
logging.Logger().Debug(fmt.Sprintf("TypeAnnon Pkg:%s, Type: %s", ct.PkgPath(), ct.Name()))
if f.Type != ct { if f.Type != ct {
return false return false

View File

@ -6,6 +6,7 @@ import (
"testing" "testing"
"git.loafle.net/commons_go/di/annotation" "git.loafle.net/commons_go/di/annotation"
cdia "git.loafle.net/commons_go/di/injection/annotation"
) )
func TestRegisterType(t *testing.T) { func TestRegisterType(t *testing.T) {
@ -23,6 +24,9 @@ func TestRegisterType(t *testing.T) {
RegisterResource("List", []string{"dfdkf", "skgkfg"}) RegisterResource("List", []string{"dfdkf", "skgkfg"})
css := GetInstancesByAnnotationName(cdia.ComponentTag)
log.Printf("%v", css)
cs := GetInstance(reflect.TypeOf((*CService)(nil))).(*CService) cs := GetInstance(reflect.TypeOf((*CService)(nil))).(*CService)
log.Printf("%v", cs) log.Printf("%v", cs)