ing
This commit is contained in:
parent
ae21044261
commit
52e9415760
|
@ -4,8 +4,6 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"git.loafle.net/commons_go/logging"
|
||||
|
||||
cur "git.loafle.net/commons_go/util/reflect"
|
||||
)
|
||||
|
||||
|
@ -15,9 +13,9 @@ func init() {
|
|||
annotationRegistry = make(map[string]*AnnotationDefinition, 0)
|
||||
}
|
||||
|
||||
func RegisterAnnotation(name string, t reflect.Type) {
|
||||
func RegisterAnnotation(name string, t reflect.Type) error {
|
||||
if _, ok := annotationRegistry[name]; ok {
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: name[%s] of annotation exist already.", name))
|
||||
return fmt.Errorf("DI: name[%s] of annotation exist already", name)
|
||||
}
|
||||
|
||||
meta := getMetaFields(t)
|
||||
|
@ -30,6 +28,8 @@ func RegisterAnnotation(name string, t reflect.Type) {
|
|||
}
|
||||
|
||||
annotationRegistry[name] = def
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type AnnotationDefinition struct {
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
|
||||
cda "git.loafle.net/commons_go/di/annotation"
|
||||
cdia "git.loafle.net/commons_go/di/injection/annotation"
|
||||
"git.loafle.net/commons_go/logging"
|
||||
cur "git.loafle.net/commons_go/util/reflect"
|
||||
)
|
||||
|
||||
|
@ -19,13 +18,13 @@ func init() {
|
|||
var registry ComponentRegistry
|
||||
|
||||
type ComponentRegistry interface {
|
||||
RegisterType(t reflect.Type)
|
||||
RegisterType(t reflect.Type) error
|
||||
RegisterResource(name string, resource interface{}) error
|
||||
|
||||
GetInstance(t reflect.Type) interface{}
|
||||
GetInstances(ts []reflect.Type) []interface{}
|
||||
GetInstanceByName(name string) interface{}
|
||||
GetInstancesByAnnotationName(n string) []interface{}
|
||||
GetInstance(t reflect.Type) (interface{}, error)
|
||||
GetInstances(ts []reflect.Type) ([]interface{}, error)
|
||||
GetInstanceByName(name string) (interface{}, error)
|
||||
GetInstancesByAnnotationName(n string) ([]interface{}, error)
|
||||
}
|
||||
|
||||
func newRegistry() ComponentRegistry {
|
||||
|
@ -45,30 +44,30 @@ type defaultComponentRegistry struct {
|
|||
resourceByName map[string]interface{}
|
||||
}
|
||||
|
||||
func RegisterType(t reflect.Type) {
|
||||
func RegisterType(t reflect.Type) error {
|
||||
pc, _, _, ok := runtime.Caller(1)
|
||||
details := runtime.FuncForPC(pc)
|
||||
if ok && details != nil {
|
||||
logging.Logger().Debug(fmt.Sprintf("called from %s", details.Name()))
|
||||
return fmt.Errorf("called from %s", details.Name())
|
||||
}
|
||||
|
||||
registry.RegisterType(t)
|
||||
return registry.RegisterType(t)
|
||||
}
|
||||
func (cr *defaultComponentRegistry) RegisterType(t reflect.Type) {
|
||||
func (cr *defaultComponentRegistry) RegisterType(t reflect.Type) error {
|
||||
if nil == t {
|
||||
logging.Logger().Panic("DI: t[reflect.Type] is nil")
|
||||
return fmt.Errorf("DI: t[reflect.Type] is nil")
|
||||
}
|
||||
if !cur.IsTypeKind(t, reflect.Struct, true) {
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be specified but is %v", t))
|
||||
return fmt.Errorf("DI: t[reflect.Type] must be specified but is %v", t)
|
||||
}
|
||||
|
||||
td, err := cr.buildDefinition(t)
|
||||
if nil != err {
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: %v", err))
|
||||
return fmt.Errorf("DI: %v", err)
|
||||
}
|
||||
|
||||
if _, ok := cr.definitionByType[td.RealType]; ok {
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: The type[%s] of Component is exist already", td.FullName))
|
||||
return fmt.Errorf("DI: The type[%s] of Component is exist already", td.FullName)
|
||||
}
|
||||
cr.definitionByType[td.RealType] = td
|
||||
|
||||
|
@ -82,9 +81,11 @@ func (cr *defaultComponentRegistry) RegisterType(t reflect.Type) {
|
|||
}
|
||||
|
||||
if eTD, ok := cr.definitionByName[name]; ok {
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: The name[%s] of Component is exist already type[%s]", name, eTD.FullName))
|
||||
return fmt.Errorf("DI: The name[%s] of Component is exist already type[%s]", name, eTD.FullName)
|
||||
}
|
||||
cr.definitionByName[name] = td
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RegisterResource(name string, resource interface{}) error {
|
||||
|
@ -101,20 +102,20 @@ func (cr *defaultComponentRegistry) RegisterResource(name string, resource inter
|
|||
|
||||
// GetInstance returns instance of type
|
||||
// t must be pointer of struct
|
||||
func GetInstance(t reflect.Type) interface{} {
|
||||
func GetInstance(t reflect.Type) (interface{}, error) {
|
||||
return registry.GetInstance(t)
|
||||
}
|
||||
func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) interface{} {
|
||||
func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, error) {
|
||||
if nil == t {
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] is nil"))
|
||||
return nil, fmt.Errorf("DI: t[reflect.Type] is nil")
|
||||
}
|
||||
|
||||
if reflect.Ptr != t.Kind() {
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be pointer of struct"))
|
||||
return nil, fmt.Errorf("DI: t[reflect.Type] must be pointer of struct")
|
||||
}
|
||||
|
||||
if reflect.Struct != t.Elem().Kind() {
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be pointer of struct"))
|
||||
return nil, fmt.Errorf("DI: t[reflect.Type] must be pointer of struct")
|
||||
}
|
||||
|
||||
var err error
|
||||
|
@ -124,13 +125,13 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) interface{} {
|
|||
if !ok {
|
||||
td, err = cr.buildDefinition(t)
|
||||
if nil != err {
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: %v", err))
|
||||
return nil, fmt.Errorf("DI: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
comV, ok := cr.instanceByType[td.RealType]
|
||||
if ok {
|
||||
return comV
|
||||
return comV, nil
|
||||
}
|
||||
|
||||
v := reflect.New(rt)
|
||||
|
@ -143,14 +144,16 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) interface{} {
|
|||
f := rv.FieldByName(fd.FieldName)
|
||||
|
||||
if !f.IsValid() {
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: Field[%s] is not valid", fd.FieldName))
|
||||
return nil, fmt.Errorf("DI: Field[%s] is not valid", fd.FieldName)
|
||||
}
|
||||
if !f.CanSet() {
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: Field[%s] can not set", fd.FieldName))
|
||||
return nil, fmt.Errorf("DI: Field[%s] can not set", fd.FieldName)
|
||||
}
|
||||
|
||||
if annotation = fd.GetAnnotationByType(reflect.TypeOf((*cdia.Inject)(nil)), false); nil != annotation {
|
||||
fV = cr.GetInstance(fd.Type)
|
||||
if fV, err = cr.GetInstance(fd.Type); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if annotation = fd.GetAnnotationByType(reflect.TypeOf((*cdia.Resource)(nil)), false); nil != annotation {
|
||||
|
@ -158,64 +161,76 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) interface{} {
|
|||
if "" == n {
|
||||
n = fd.FieldName
|
||||
}
|
||||
fV = cr.GetInstanceByName(n)
|
||||
if fV, err = cr.GetInstanceByName(n); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if nil != err {
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: %v", err))
|
||||
return nil, fmt.Errorf("DI: %v", err)
|
||||
}
|
||||
f.Set(reflect.ValueOf(fV))
|
||||
}
|
||||
|
||||
cr.instanceByType[td.RealType] = v.Interface()
|
||||
|
||||
return v.Interface()
|
||||
return v.Interface(), nil
|
||||
}
|
||||
|
||||
func GetInstanceByName(name string) interface{} {
|
||||
func GetInstanceByName(name string) (interface{}, error) {
|
||||
return registry.GetInstanceByName(name)
|
||||
}
|
||||
func (cr *defaultComponentRegistry) GetInstanceByName(name string) interface{} {
|
||||
func (cr *defaultComponentRegistry) GetInstanceByName(name string) (interface{}, error) {
|
||||
v, ok := cr.resourceByName[name]
|
||||
if ok {
|
||||
return v
|
||||
return v, nil
|
||||
}
|
||||
logging.Logger().Panic(fmt.Sprintf("DI: Resource[%s] is not exist", name))
|
||||
return nil
|
||||
return nil, fmt.Errorf("DI: Resource[%s] is not exist", name)
|
||||
}
|
||||
|
||||
// GetInstances returns instance of annotated
|
||||
// n must be name of registered annotation
|
||||
func GetInstances(ts []reflect.Type) []interface{} {
|
||||
func GetInstances(ts []reflect.Type) ([]interface{}, error) {
|
||||
return registry.GetInstances(ts)
|
||||
}
|
||||
func (cr *defaultComponentRegistry) GetInstances(ts []reflect.Type) []interface{} {
|
||||
func (cr *defaultComponentRegistry) GetInstances(ts []reflect.Type) ([]interface{}, error) {
|
||||
var (
|
||||
i interface{}
|
||||
err error
|
||||
)
|
||||
instances := make([]interface{}, 0)
|
||||
|
||||
for _, t := range ts {
|
||||
if i := cr.GetInstance(t); nil != i {
|
||||
instances = append(instances, i)
|
||||
if i, err = cr.GetInstance(t); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
instances = append(instances, i)
|
||||
}
|
||||
|
||||
return instances
|
||||
return instances, nil
|
||||
}
|
||||
|
||||
// GetInstancesByAnnotationName returns instance of annotated
|
||||
// n must be name of registered annotation
|
||||
func GetInstancesByAnnotationName(n string) []interface{} {
|
||||
func GetInstancesByAnnotationName(n string) ([]interface{}, error) {
|
||||
return registry.GetInstancesByAnnotationName(n)
|
||||
}
|
||||
func (cr *defaultComponentRegistry) GetInstancesByAnnotationName(n string) []interface{} {
|
||||
func (cr *defaultComponentRegistry) GetInstancesByAnnotationName(n string) ([]interface{}, error) {
|
||||
var (
|
||||
i interface{}
|
||||
err error
|
||||
)
|
||||
instances := make([]interface{}, 0)
|
||||
|
||||
for _, td := range cr.definitionByType {
|
||||
if nil != td.GetAnnotation(n) {
|
||||
instances = append(instances, cr.GetInstance(td.Type))
|
||||
if i, err = cr.GetInstance(td.Type); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
instances = append(instances, i)
|
||||
}
|
||||
}
|
||||
|
||||
return instances
|
||||
return instances, nil
|
||||
}
|
||||
|
||||
func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefinition, error) {
|
||||
|
|
|
@ -10,6 +10,11 @@ import (
|
|||
)
|
||||
|
||||
func TestRegisterType(t *testing.T) {
|
||||
var (
|
||||
err error
|
||||
css []interface{}
|
||||
cs interface{}
|
||||
)
|
||||
RegisterType(reflect.TypeOf((*AService)(nil)))
|
||||
RegisterType(reflect.TypeOf((*BService)(nil)))
|
||||
// RegisterType(reflect.TypeOf((*TestStruct2)(nil)), &cda.ComponentAnnotation{
|
||||
|
@ -24,17 +29,23 @@ func TestRegisterType(t *testing.T) {
|
|||
|
||||
RegisterResource("List", []string{"dfdkf", "skgkfg"})
|
||||
|
||||
css := GetInstancesByAnnotationName(cdia.ComponentTag)
|
||||
if css, err = GetInstancesByAnnotationName(cdia.ComponentTag); nil != err {
|
||||
log.Printf("%v \n", err)
|
||||
}
|
||||
log.Printf("%v", css)
|
||||
|
||||
css2 := GetInstances([]reflect.Type{
|
||||
if css, err = GetInstances([]reflect.Type{
|
||||
reflect.TypeOf((*AService)(nil)),
|
||||
reflect.TypeOf((*BService)(nil)),
|
||||
})
|
||||
log.Printf("%v", css2)
|
||||
}); nil != err {
|
||||
log.Printf("%v \n", err)
|
||||
}
|
||||
log.Printf("%v", css)
|
||||
|
||||
cs := GetInstance(reflect.TypeOf((*CService)(nil))).(*CService)
|
||||
log.Printf("%v", cs)
|
||||
if cs, err = GetInstance(reflect.TypeOf((*CService)(nil))); nil != err {
|
||||
log.Printf("%v \n", err)
|
||||
}
|
||||
log.Printf("%v", cs.(*CService))
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user