diff --git a/registry/registry.go b/registry/registry.go index 29e5664..a714e23 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -25,6 +25,7 @@ type ComponentRegistry interface { GetInstance(t reflect.Type) interface{} GetInstanceByName(name string) interface{} + GetInstancesByAnnotationName(n string) []interface{} } func newRegistry() ComponentRegistry { @@ -185,6 +186,23 @@ func (cr *defaultComponentRegistry) GetInstanceByName(name string) interface{} { 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) { if nil == t { 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 { - logging.Logger().Debug(fmt.Sprintf("Anonymous Pkg:%s, Type: %s", f.Type.PkgPath(), f.Type.Name())) - 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 { return false diff --git a/registry/registry_test.go b/registry/registry_test.go index 1549ef9..8ca40df 100644 --- a/registry/registry_test.go +++ b/registry/registry_test.go @@ -6,6 +6,7 @@ import ( "testing" "git.loafle.net/commons_go/di/annotation" + cdia "git.loafle.net/commons_go/di/injection/annotation" ) func TestRegisterType(t *testing.T) { @@ -23,6 +24,9 @@ func TestRegisterType(t *testing.T) { RegisterResource("List", []string{"dfdkf", "skgkfg"}) + css := GetInstancesByAnnotationName(cdia.ComponentTag) + log.Printf("%v", css) + cs := GetInstance(reflect.TypeOf((*CService)(nil))).(*CService) log.Printf("%v", cs)