ing
This commit is contained in:
parent
c6d6690cee
commit
70ddbc5e5d
|
@ -1,30 +1,29 @@
|
||||||
package annotation
|
package annotation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseInject(t *testing.T) {
|
func TestParseInject(t *testing.T) {
|
||||||
a1 := "@Inject"
|
// a1 := "@Inject"
|
||||||
a, err := ParseInject(a1)
|
// a, err := ParseInject(a1)
|
||||||
if nil != err {
|
// if nil != err {
|
||||||
log.Printf("%v", err)
|
// log.Printf("%v", err)
|
||||||
}
|
// }
|
||||||
log.Printf("%v", a)
|
// log.Printf("%v", a)
|
||||||
|
|
||||||
a2 := "@Inject()"
|
// a2 := "@Inject()"
|
||||||
a, err = ParseInject(a2)
|
// a, err = ParseInject(a2)
|
||||||
if nil != err {
|
// if nil != err {
|
||||||
log.Printf("%v", err)
|
// log.Printf("%v", err)
|
||||||
}
|
// }
|
||||||
log.Printf("%v", a)
|
// log.Printf("%v", a)
|
||||||
|
|
||||||
a3 := "@Inject(name=string)"
|
// a3 := "@Inject(name=string)"
|
||||||
a, err = ParseInject(a3)
|
// a, err = ParseInject(a3)
|
||||||
if nil != err {
|
// if nil != err {
|
||||||
log.Printf("%v", err)
|
// log.Printf("%v", err)
|
||||||
}
|
// }
|
||||||
log.Printf("%v", a)
|
// log.Printf("%v", a)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,29 @@
|
||||||
package annotation
|
package annotation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseResource(t *testing.T) {
|
func TestParseResource(t *testing.T) {
|
||||||
a1 := "@Resource"
|
// a1 := "@Resource"
|
||||||
a, err := ParseResource(a1)
|
// a, err := ParseResource(a1)
|
||||||
if nil != err {
|
// if nil != err {
|
||||||
log.Printf("%v", err)
|
// log.Printf("%v", err)
|
||||||
}
|
// }
|
||||||
log.Printf("%v", a)
|
// log.Printf("%v", a)
|
||||||
|
|
||||||
a2 := "@Resource( )"
|
// a2 := "@Resource( )"
|
||||||
a, err = ParseResource(a2)
|
// a, err = ParseResource(a2)
|
||||||
if nil != err {
|
// if nil != err {
|
||||||
log.Printf("%v", err)
|
// log.Printf("%v", err)
|
||||||
}
|
// }
|
||||||
log.Printf("%v", a)
|
// log.Printf("%v", a)
|
||||||
|
|
||||||
a3 := "@Resource(name=string1232)"
|
// a3 := "@Resource(name=string1232)"
|
||||||
a, err = ParseResource(a3)
|
// a, err = ParseResource(a3)
|
||||||
if nil != err {
|
// if nil != err {
|
||||||
log.Printf("%v", err)
|
// log.Printf("%v", err)
|
||||||
}
|
// }
|
||||||
log.Printf("%v", a)
|
// log.Printf("%v", a)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,8 +22,8 @@ type ComponentRegistry interface {
|
||||||
RegisterType(t reflect.Type, ca *cda.ComponentAnnotation)
|
RegisterType(t reflect.Type, ca *cda.ComponentAnnotation)
|
||||||
RegisterResource(name string, resource interface{}) error
|
RegisterResource(name string, resource interface{}) error
|
||||||
|
|
||||||
GetInstance(t reflect.Type) (interface{}, error)
|
GetInstance(t reflect.Type) interface{}
|
||||||
GetInstanceByName(name string) (interface{}, error)
|
GetInstanceByName(name string) interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRegistry() ComponentRegistry {
|
func newRegistry() ComponentRegistry {
|
||||||
|
@ -97,20 +97,20 @@ func (cr *defaultComponentRegistry) RegisterResource(name string, resource inter
|
||||||
|
|
||||||
// GetInstance returns instance of type
|
// GetInstance returns instance of type
|
||||||
// t must be pointer of struct
|
// t must be pointer of struct
|
||||||
func GetInstance(t reflect.Type) (interface{}, error) {
|
func GetInstance(t reflect.Type) interface{} {
|
||||||
return registry.GetInstance(t)
|
return registry.GetInstance(t)
|
||||||
}
|
}
|
||||||
func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, error) {
|
func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) interface{} {
|
||||||
if nil == t {
|
if nil == t {
|
||||||
return nil, fmt.Errorf("DI: t[reflect.Type] is nil")
|
logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] is nil"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if reflect.Ptr != t.Kind() {
|
if reflect.Ptr != t.Kind() {
|
||||||
return nil, fmt.Errorf("DI: t[reflect.Type] must be pointer of struct")
|
logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be pointer of struct"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if reflect.Struct != t.Elem().Kind() {
|
if reflect.Struct != t.Elem().Kind() {
|
||||||
return nil, fmt.Errorf("DI: t[reflect.Type] must be pointer of struct")
|
logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be pointer of struct"))
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
@ -120,13 +120,13 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er
|
||||||
if !ok {
|
if !ok {
|
||||||
td, err = cr.buildDefinition(t)
|
td, err = cr.buildDefinition(t)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return nil, err
|
logging.Logger().Panic(fmt.Sprintf("DI: %v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
comV, ok := cr.instanceByType[td.RealType]
|
comV, ok := cr.instanceByType[td.RealType]
|
||||||
if ok {
|
if ok {
|
||||||
return comV, nil
|
return comV
|
||||||
}
|
}
|
||||||
|
|
||||||
v := reflect.New(rt)
|
v := reflect.New(rt)
|
||||||
|
@ -139,15 +139,15 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er
|
||||||
f := rv.FieldByName(fd.FieldName)
|
f := rv.FieldByName(fd.FieldName)
|
||||||
|
|
||||||
if !f.IsValid() {
|
if !f.IsValid() {
|
||||||
return nil, fmt.Errorf("DI: Field[%s] is not valid", fd.FieldName)
|
logging.Logger().Panic(fmt.Sprintf("DI: Field[%s] is not valid", fd.FieldName))
|
||||||
}
|
}
|
||||||
if !f.CanSet() {
|
if !f.CanSet() {
|
||||||
return nil, fmt.Errorf("DI: Field[%s] can not set", fd.FieldName)
|
logging.Logger().Panic(fmt.Sprintf("DI: Field[%s] can not set", fd.FieldName))
|
||||||
}
|
}
|
||||||
|
|
||||||
annotation = fd.GetAnnotation(cda.InjectTag)
|
annotation = fd.GetAnnotation(cda.InjectTag)
|
||||||
if nil != annotation {
|
if nil != annotation {
|
||||||
fV, err = cr.GetInstance(fd.Type)
|
fV = cr.GetInstance(fd.Type)
|
||||||
}
|
}
|
||||||
annotation = fd.GetAnnotation(cda.ResourceTag)
|
annotation = fd.GetAnnotation(cda.ResourceTag)
|
||||||
if nil != annotation {
|
if nil != annotation {
|
||||||
|
@ -155,29 +155,30 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er
|
||||||
if "" == n {
|
if "" == n {
|
||||||
n = fd.FieldName
|
n = fd.FieldName
|
||||||
}
|
}
|
||||||
fV, err = cr.GetInstanceByName(n)
|
fV = cr.GetInstanceByName(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return nil, err
|
logging.Logger().Panic(fmt.Sprintf("DI: %v", err))
|
||||||
}
|
}
|
||||||
f.Set(reflect.ValueOf(fV))
|
f.Set(reflect.ValueOf(fV))
|
||||||
}
|
}
|
||||||
|
|
||||||
cr.instanceByType[td.RealType] = v.Interface()
|
cr.instanceByType[td.RealType] = v.Interface()
|
||||||
|
|
||||||
return v.Interface(), nil
|
return v.Interface()
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetInstanceByName(name string) (interface{}, error) {
|
func GetInstanceByName(name string) interface{} {
|
||||||
return registry.GetInstanceByName(name)
|
return registry.GetInstanceByName(name)
|
||||||
}
|
}
|
||||||
func (cr *defaultComponentRegistry) GetInstanceByName(name string) (interface{}, error) {
|
func (cr *defaultComponentRegistry) GetInstanceByName(name string) interface{} {
|
||||||
v, ok := cr.resourceByName[name]
|
v, ok := cr.resourceByName[name]
|
||||||
if ok {
|
if ok {
|
||||||
return v, nil
|
return v
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("DI: Resource[%s] is not exist", name)
|
logging.Logger().Panic(fmt.Sprintf("DI: Resource[%s] is not exist", name))
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefinition, error) {
|
func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefinition, error) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user