This commit is contained in:
crusader 2017-12-08 19:32:59 +09:00
parent 5de5aa30e3
commit c6d6690cee
5 changed files with 135 additions and 57 deletions

View File

@ -4,11 +4,10 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"strings" "strings"
"git.loafle.net/commons_go/di"
) )
const ( const (
AnnotationTag = "annotation"
AnnotationChar = "@" AnnotationChar = "@"
AnnotationStartChar = "(" AnnotationStartChar = "("
AnnotationEndChar = ")" AnnotationEndChar = ")"
@ -33,8 +32,8 @@ type Annotation interface {
// @Inject(name? string) // @Inject(name? string)
// @Resource(name? string) // @Resource(name? string)
func ParseAnnotation(tag reflect.StructTag) ([]Annotation, error) { func ParseAnnotation(tag reflect.StructTag) (map[string]Annotation, error) {
s := strings.Trim(tag.Get(di.AnnotationTag), " ") s := strings.Trim(tag.Get(AnnotationTag), " ")
if "" == s { if "" == s {
return nil, nil return nil, nil
} }
@ -44,7 +43,7 @@ func ParseAnnotation(tag reflect.StructTag) ([]Annotation, error) {
return nil, nil return nil, nil
} }
rKVs := make([]Annotation, 0) rKVs := make(map[string]Annotation, 0)
for _, a := range annotations { for _, a := range annotations {
a = strings.Trim(a, " ") a = strings.Trim(a, " ")
if "" == a { if "" == a {
@ -61,7 +60,7 @@ func ParseAnnotation(tag reflect.StructTag) ([]Annotation, error) {
if nil != err { if nil != err {
return nil, err return nil, err
} }
rKVs = append(rKVs, annotation) rKVs[aName] = annotation
} }
return rKVs, nil return rKVs, nil

View File

@ -23,7 +23,3 @@ const (
ScopeTypeSingleton ScopeTypeSingleton
ScopeTypeTransiant ScopeTypeTransiant
) )
const (
AnnotationTag = "annotation"
)

View File

@ -14,16 +14,24 @@ type TypeDefinition struct {
Type reflect.Type Type reflect.Type
RealType reflect.Type RealType reflect.Type
Fields map[string]*FieldDefinition Fields []*FieldDefinition
} }
type FieldDefinition struct { type FieldDefinition struct {
FieldName string
PkgName string PkgName string
TypeName string TypeName string
Type reflect.Type Type reflect.Type
RealType reflect.Type RealType reflect.Type
Annotations []cda.Annotation Annotations map[string]cda.Annotation
}
func (fd *FieldDefinition) GetAnnotation(name string) cda.Annotation {
if nil == fd.Annotations {
return nil
}
return fd.Annotations[name]
} }
func FullName(pkgName, typeName string) string { func FullName(pkgName, typeName string) string {

View File

@ -30,6 +30,7 @@ func newRegistry() ComponentRegistry {
r := &defaultComponentRegistry{} r := &defaultComponentRegistry{}
r.definitionByType = make(map[reflect.Type]*TypeDefinition, 0) r.definitionByType = make(map[reflect.Type]*TypeDefinition, 0)
r.definitionByName = make(map[string]*TypeDefinition, 0) r.definitionByName = make(map[string]*TypeDefinition, 0)
r.instanceByType = make(map[reflect.Type]interface{}, 0)
r.resourceByName = make(map[string]interface{}, 0) r.resourceByName = make(map[string]interface{}, 0)
return r return r
@ -38,6 +39,7 @@ func newRegistry() ComponentRegistry {
type defaultComponentRegistry struct { type defaultComponentRegistry struct {
definitionByType map[reflect.Type]*TypeDefinition definitionByType map[reflect.Type]*TypeDefinition
definitionByName map[string]*TypeDefinition definitionByName map[string]*TypeDefinition
instanceByType map[reflect.Type]interface{}
resourceByName map[string]interface{} resourceByName map[string]interface{}
} }
@ -93,6 +95,8 @@ func (cr *defaultComponentRegistry) RegisterResource(name string, resource inter
return nil return nil
} }
// GetInstance returns instance of type
// t must be pointer of struct
func GetInstance(t reflect.Type) (interface{}, error) { func GetInstance(t reflect.Type) (interface{}, error) {
return registry.GetInstance(t) return registry.GetInstance(t)
} }
@ -101,6 +105,14 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er
return nil, fmt.Errorf("DI: t[reflect.Type] is nil") return nil, fmt.Errorf("DI: t[reflect.Type] is nil")
} }
if reflect.Ptr != t.Kind() {
return nil, fmt.Errorf("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")
}
var err error var err error
rt, _, _ := cdur.GetTypeInfo(t) rt, _, _ := cdur.GetTypeInfo(t)
@ -112,22 +124,60 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, er
} }
} }
v := reflect.New(rt) comV, ok := cr.instanceByType[td.RealType]
i := v.Interface() if ok {
return comV, nil
for f, fd := range td.Fields {
} }
return nil, nil v := reflect.New(rt)
rv := v.Elem()
var annotation cda.Annotation
var fV interface{}
for _, fd := range td.Fields {
f := rv.FieldByName(fd.FieldName)
if !f.IsValid() {
return nil, fmt.Errorf("DI: Field[%s] is not valid", fd.FieldName)
}
if !f.CanSet() {
return nil, fmt.Errorf("DI: Field[%s] can not set", fd.FieldName)
}
annotation = fd.GetAnnotation(cda.InjectTag)
if nil != annotation {
fV, err = cr.GetInstance(fd.Type)
}
annotation = fd.GetAnnotation(cda.ResourceTag)
if nil != annotation {
n := annotation.(*cda.Resource).Name
if "" == n {
n = fd.FieldName
}
fV, err = cr.GetInstanceByName(n)
}
if nil != err {
return nil, err
}
f.Set(reflect.ValueOf(fV))
}
cr.instanceByType[td.RealType] = v.Interface()
return v.Interface(), nil
} }
func GetInstanceByName(name string) (interface{}, error) { func GetInstanceByName(name string) (interface{}, error) {
return registry.GetInstanceByName(name) return registry.GetInstanceByName(name)
} }
func (cr *defaultComponentRegistry) GetInstanceByName(name string) (interface{}, error) { func (cr *defaultComponentRegistry) GetInstanceByName(name string) (interface{}, error) {
v, ok := cr.resourceByName[name]
return nil, nil if ok {
return v, nil
}
return nil, fmt.Errorf("DI: Resource[%s] is not exist", name)
} }
func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefinition, error) { func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefinition, error) {
@ -143,33 +193,47 @@ func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefini
td.Type = t td.Type = t
td.RealType = rt td.RealType = rt
nf := rt.NumField() fields := getFields(rt)
if 0 < nf {
fields := make(map[string]*FieldDefinition) if 0 < len(fields) {
for i := 0; i < nf; i++ { td.Fields = fields
}
return td, nil
}
func getFields(t reflect.Type) []*FieldDefinition {
fields := make([]*FieldDefinition, 0)
rt, _, _ := cdur.GetTypeInfo(t)
if reflect.Struct != rt.Kind() {
return fields
}
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i) f := rt.Field(i)
if f.Anonymous {
fields = append(fields, getFields(f.Type)...)
} else {
as, err := cda.ParseAnnotation(f.Tag) as, err := cda.ParseAnnotation(f.Tag)
if nil != err { if nil != err {
return nil, err return fields
} }
if nil != as && 0 < len(as) { if nil != as && 0 < len(as) {
fRT, fPkgName, fTName := cdur.GetTypeInfo(f.Type) fRT, fPkgName, fTName := cdur.GetTypeInfo(f.Type)
fd := &FieldDefinition{ fd := &FieldDefinition{
FieldName: f.Name,
PkgName: fPkgName, PkgName: fPkgName,
TypeName: fTName, TypeName: fTName,
Type: f.Type, Type: f.Type,
RealType: fRT, RealType: fRT,
Annotations: as, Annotations: as,
} }
fields[f.Name] = fd fields = append(fields, fd)
}
} }
} }
if 0 < len(fields) { return fields
td.Fields = fields
}
}
return td, nil
} }

View File

@ -1,34 +1,45 @@
package registry package registry
import ( import (
"log"
"reflect" "reflect"
"testing" "testing"
cda "git.loafle.net/commons_go/di/annotation"
) )
func TestRegisterType(t *testing.T) { func TestRegisterType(t *testing.T) {
RegisterType(reflect.TypeOf((*TestStruct1)(nil)), &cda.ComponentAnnotation{}) // RegisterType(reflect.TypeOf((*TestStruct1)(nil)), &cda.ComponentAnnotation{})
RegisterType(reflect.TypeOf((*TestStruct2)(nil)), &cda.ComponentAnnotation{ // RegisterType(reflect.TypeOf((*TestStruct2)(nil)), &cda.ComponentAnnotation{
Name: "test1", // Name: "test1",
}) // })
RegisterType(reflect.TypeOf((*TestStruct3)(nil)), &cda.ComponentAnnotation{ // RegisterType(reflect.TypeOf((*TestStruct3)(nil)), &cda.ComponentAnnotation{
Name: "test2", // Name: "test2",
}) // })
// fs := getFields(reflect.TypeOf((*TestStruct3)(nil)))
// log.Printf("%v", fs)
RegisterResource("List", []string{"dfdkf", "skgkfg"})
i, err := GetInstance(reflect.TypeOf((*CService)(nil)))
if nil != err {
log.Printf("%v", err)
} else {
cs := i.(*CService)
log.Printf("%v", cs)
}
} }
type TestStruct1 struct { type AService struct {
Name1 string `annotation:"@Inject"` NameA string
Name2 string `annotation:"@Inject()"`
Name3 string `annotation:"@Inject(name=test1)"`
} }
type TestStruct2 struct {
Name1 string `annotation:"@Inject"` type BService struct {
Name2 string `annotation:"@Inject()"` NameB string
Name3 string `annotation:"@Inject(name=test2)"`
} }
type TestStruct3 struct {
Name1 string `annotation:"@Inject"` type CService struct {
Name2 string `annotation:"@Inject()"` AService *AService `annotation:"@Inject()"`
Name3 string `annotation:"@Inject(name=test3)"` BService *BService `annotation:"@Inject()"`
List []string `annotation:"@Resource()"`
} }