diff --git a/registry/registry.go b/registry/registry.go index a714e23..6fc00a4 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -24,6 +24,7 @@ type ComponentRegistry interface { RegisterResource(name string, resource interface{}) error GetInstance(t reflect.Type) interface{} + GetInstances(ts []reflect.Type) []interface{} GetInstanceByName(name string) interface{} GetInstancesByAnnotationName(n string) []interface{} } @@ -186,6 +187,23 @@ func (cr *defaultComponentRegistry) GetInstanceByName(name string) interface{} { return nil } +// GetInstances returns instance of annotated +// n must be name of registered annotation +func GetInstances(ts []reflect.Type) []interface{} { + return registry.GetInstances(ts) +} +func (cr *defaultComponentRegistry) GetInstances(ts []reflect.Type) []interface{} { + instances := make([]interface{}, 0) + + for _, t := range ts { + if i := cr.GetInstance(t); nil != i { + instances = append(instances, i) + } + } + + return instances +} + // GetInstancesByAnnotationName returns instance of annotated // n must be name of registered annotation func GetInstancesByAnnotationName(n string) []interface{} { diff --git a/registry/registry_test.go b/registry/registry_test.go index 8ca40df..00cb24e 100644 --- a/registry/registry_test.go +++ b/registry/registry_test.go @@ -27,8 +27,13 @@ func TestRegisterType(t *testing.T) { css := GetInstancesByAnnotationName(cdia.ComponentTag) log.Printf("%v", css) - cs := GetInstance(reflect.TypeOf((*CService)(nil))).(*CService) + css2 := GetInstances([]reflect.Type{ + reflect.TypeOf((*AService)(nil)), + reflect.TypeOf((*BService)(nil)), + }) + log.Printf("%v", css2) + cs := GetInstance(reflect.TypeOf((*CService)(nil))).(*CService) log.Printf("%v", cs) }