ing
This commit is contained in:
parent
1a82266014
commit
f2756c481d
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user