This commit is contained in:
crusader 2018-03-15 23:08:51 +09:00
parent f2756c481d
commit e92a9693a0
2 changed files with 24 additions and 1 deletions

View File

@ -24,6 +24,7 @@ type ComponentRegistry interface {
RegisterResource(name string, resource interface{}) error RegisterResource(name string, resource interface{}) error
GetInstance(t reflect.Type) interface{} GetInstance(t reflect.Type) interface{}
GetInstances(ts []reflect.Type) []interface{}
GetInstanceByName(name string) interface{} GetInstanceByName(name string) interface{}
GetInstancesByAnnotationName(n string) []interface{} GetInstancesByAnnotationName(n string) []interface{}
} }
@ -186,6 +187,23 @@ func (cr *defaultComponentRegistry) GetInstanceByName(name string) interface{} {
return nil 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 // GetInstancesByAnnotationName returns instance of annotated
// n must be name of registered annotation // n must be name of registered annotation
func GetInstancesByAnnotationName(n string) []interface{} { func GetInstancesByAnnotationName(n string) []interface{} {

View File

@ -27,8 +27,13 @@ func TestRegisterType(t *testing.T) {
css := GetInstancesByAnnotationName(cdia.ComponentTag) css := GetInstancesByAnnotationName(cdia.ComponentTag)
log.Printf("%v", css) 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) log.Printf("%v", cs)
} }