This commit is contained in:
crusader 2018-03-15 16:32:04 +09:00
parent c6d6690cee
commit 70ddbc5e5d
3 changed files with 56 additions and 57 deletions

View File

@ -1,30 +1,29 @@
package annotation
import (
"log"
"testing"
)
func TestParseInject(t *testing.T) {
a1 := "@Inject"
a, err := ParseInject(a1)
if nil != err {
log.Printf("%v", err)
}
log.Printf("%v", a)
// a1 := "@Inject"
// a, err := ParseInject(a1)
// if nil != err {
// log.Printf("%v", err)
// }
// log.Printf("%v", a)
a2 := "@Inject()"
a, err = ParseInject(a2)
if nil != err {
log.Printf("%v", err)
}
log.Printf("%v", a)
// a2 := "@Inject()"
// a, err = ParseInject(a2)
// if nil != err {
// log.Printf("%v", err)
// }
// log.Printf("%v", a)
a3 := "@Inject(name=string)"
a, err = ParseInject(a3)
if nil != err {
log.Printf("%v", err)
}
log.Printf("%v", a)
// a3 := "@Inject(name=string)"
// a, err = ParseInject(a3)
// if nil != err {
// log.Printf("%v", err)
// }
// log.Printf("%v", a)
}

View File

@ -1,30 +1,29 @@
package annotation
import (
"log"
"testing"
)
func TestParseResource(t *testing.T) {
a1 := "@Resource"
a, err := ParseResource(a1)
if nil != err {
log.Printf("%v", err)
}
log.Printf("%v", a)
// a1 := "@Resource"
// a, err := ParseResource(a1)
// if nil != err {
// log.Printf("%v", err)
// }
// log.Printf("%v", a)
a2 := "@Resource( )"
a, err = ParseResource(a2)
if nil != err {
log.Printf("%v", err)
}
log.Printf("%v", a)
// a2 := "@Resource( )"
// a, err = ParseResource(a2)
// if nil != err {
// log.Printf("%v", err)
// }
// log.Printf("%v", a)
a3 := "@Resource(name=string1232)"
a, err = ParseResource(a3)
if nil != err {
log.Printf("%v", err)
}
log.Printf("%v", a)
// a3 := "@Resource(name=string1232)"
// a, err = ParseResource(a3)
// if nil != err {
// log.Printf("%v", err)
// }
// log.Printf("%v", a)
}

View File

@ -22,8 +22,8 @@ type ComponentRegistry interface {
RegisterType(t reflect.Type, ca *cda.ComponentAnnotation)
RegisterResource(name string, resource interface{}) error
GetInstance(t reflect.Type) (interface{}, error)
GetInstanceByName(name string) (interface{}, error)
GetInstance(t reflect.Type) interface{}
GetInstanceByName(name string) interface{}
}
func newRegistry() ComponentRegistry {
@ -97,20 +97,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{}, error) {
func GetInstance(t reflect.Type) interface{} {
return registry.GetInstance(t)
}
func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, error) {
func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) interface{} {
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() {
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() {
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
@ -120,13 +120,13 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er
if !ok {
td, err = cr.buildDefinition(t)
if nil != err {
return nil, err
logging.Logger().Panic(fmt.Sprintf("DI: %v", err))
}
}
comV, ok := cr.instanceByType[td.RealType]
if ok {
return comV, nil
return comV
}
v := reflect.New(rt)
@ -139,15 +139,15 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er
f := rv.FieldByName(fd.FieldName)
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() {
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)
if nil != annotation {
fV, err = cr.GetInstance(fd.Type)
fV = cr.GetInstance(fd.Type)
}
annotation = fd.GetAnnotation(cda.ResourceTag)
if nil != annotation {
@ -155,29 +155,30 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er
if "" == n {
n = fd.FieldName
}
fV, err = cr.GetInstanceByName(n)
fV = cr.GetInstanceByName(n)
}
if nil != err {
return nil, err
logging.Logger().Panic(fmt.Sprintf("DI: %v", err))
}
f.Set(reflect.ValueOf(fV))
}
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)
}
func (cr *defaultComponentRegistry) GetInstanceByName(name string) (interface{}, error) {
func (cr *defaultComponentRegistry) GetInstanceByName(name string) interface{} {
v, ok := cr.resourceByName[name]
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) {