This commit is contained in:
crusader 2018-03-15 22:19:44 +09:00
parent 70ddbc5e5d
commit 1a82266014
16 changed files with 272 additions and 130 deletions

View File

@ -8,6 +8,7 @@ import (
const ( const (
AnnotationTag = "annotation" AnnotationTag = "annotation"
AnnotationMetaTag = "annotation-meta"
AnnotationChar = "@" AnnotationChar = "@"
AnnotationStartChar = "(" AnnotationStartChar = "("
AnnotationEndChar = ")" AnnotationEndChar = ")"
@ -16,18 +17,7 @@ const (
AnnotationKeyValueSpliter = "=" AnnotationKeyValueSpliter = "="
) )
var annotationRegistry map[string]reflect.Type
func init() {
annotationRegistry = make(map[string]reflect.Type, 0)
}
func registerAnnotation(name string, t reflect.Type) {
annotationRegistry[name] = t
}
type Annotation interface { type Annotation interface {
parseAttribute(attributes map[string]string) error
} }
// @Inject(name? string) // @Inject(name? string)
@ -49,14 +39,14 @@ func ParseAnnotation(tag reflect.StructTag) (map[string]Annotation, error) {
if "" == a { if "" == a {
continue continue
} }
aName, attributes, err := ParseAnnotationItem(a) aName, attributes, err := parseAnnotationItem(a)
if nil != err { if nil != err {
return nil, err return nil, err
} }
if "" == aName { if "" == aName {
continue continue
} }
annotation, err := NewAnnotation(aName, attributes) annotation, err := newAnnotation(aName, attributes)
if nil != err { if nil != err {
return nil, err return nil, err
} }
@ -66,22 +56,22 @@ func ParseAnnotation(tag reflect.StructTag) (map[string]Annotation, error) {
return rKVs, nil return rKVs, nil
} }
func NewAnnotation(name string, attributes map[string]string) (Annotation, error) { func newAnnotation(name string, attributes map[string]string) (Annotation, error) {
t, ok := annotationRegistry[name] def, ok := annotationRegistry[name]
if !ok { if !ok {
return nil, fmt.Errorf("There is no annotation[%s]", name) return nil, fmt.Errorf("There is no annotation[%s]", name)
} }
v := reflect.New(t.Elem()) v := reflect.New(def.rt)
i := v.Interface().(Annotation) i := v.Interface().(Annotation)
if nil != attributes { if nil != attributes {
i.parseAttribute(attributes) setMetaAttributes(def, v.Elem(), attributes)
} }
return i, nil return i, nil
} }
func ParseAnnotationItem(a string) (name string, attributes map[string]string, err error) { func parseAnnotationItem(a string) (name string, attributes map[string]string, err error) {
s := strings.Trim(a, " ") s := strings.Trim(a, " ")
if "" == s { if "" == s {
return return
@ -120,7 +110,7 @@ func ParseAnnotationItem(a string) (name string, attributes map[string]string, e
return return
} }
attrs, pErr := ParseAttribute(s[aStart+1 : aEnd]) attrs, pErr := parseAttributeString(s[aStart+1 : aEnd])
if nil != pErr { if nil != pErr {
err = pErr err = pErr
return return
@ -131,7 +121,7 @@ func ParseAnnotationItem(a string) (name string, attributes map[string]string, e
return return
} }
func ParseAttribute(s string) (map[string]string, error) { func parseAttributeString(s string) (map[string]string, error) {
attr := strings.Trim(s, " ") attr := strings.Trim(s, " ")
if "" == attr { if "" == attr {
return nil, nil return nil, nil

View File

@ -1,13 +0,0 @@
package annotation
import (
"git.loafle.net/commons_go/di"
)
type ComponentAnnotation struct {
Annotation
Name string
InitMethod string // func (receiver interface{}, cr ComponentRegistry) error
DestroyMethod string // func (receiver interface{}, cr ComponentRegistry) error
Scope di.ScopeType
}

33
annotation/definition.go Normal file
View File

@ -0,0 +1,33 @@
package annotation
import (
"reflect"
cdur "git.loafle.net/commons_go/di/util/reflect"
)
var annotationRegistry map[string]*AnnotationDefinition
func init() {
annotationRegistry = make(map[string]*AnnotationDefinition, 0)
}
func RegisterAnnotation(name string, t reflect.Type) {
meta := getMetaFields(t)
fRT, _, _ := cdur.GetTypeInfo(t)
def := &AnnotationDefinition{
t: t,
rt: fRT,
fields: meta,
}
annotationRegistry[name] = def
}
type AnnotationDefinition struct {
t reflect.Type
rt reflect.Type
fields map[string]*AnnotationFieldMeta
}

View File

@ -1,33 +0,0 @@
package annotation
// @Inject(name? string)
import (
"fmt"
"reflect"
)
const (
InjectTag = "@Inject"
)
func init() {
registerAnnotation(InjectTag, reflect.TypeOf((*Inject)(nil)))
}
type Inject struct {
Annotation
Name string
}
func (a *Inject) parseAttribute(attributes map[string]string) error {
for k, v := range attributes {
switch k {
case "name":
a.Name = v
default:
return fmt.Errorf("Syntax error: not supported attribute[%s]", k)
}
}
return nil
}

87
annotation/meta.go Normal file
View File

@ -0,0 +1,87 @@
package annotation
import (
"reflect"
"strings"
cdur "git.loafle.net/commons_go/di/util/reflect"
)
type AnnotationFieldMeta struct {
fieldName string
options anotationMetaOptions
}
type anotationMetaOptions string
func (o anotationMetaOptions) Contains(optionName string) bool {
if len(o) == 0 {
return false
}
s := string(o)
for s != "" {
var next string
i := strings.Index(s, AnnotationAttributeSpliter)
if i >= 0 {
s, next = s[:i], s[i+1:]
}
if s == optionName {
return true
}
s = next
}
return false
}
func getMetaFields(t reflect.Type) map[string]*AnnotationFieldMeta {
fields := make(map[string]*AnnotationFieldMeta, 0)
rt, _, _ := cdur.GetTypeInfo(t)
if reflect.Struct != rt.Kind() {
return fields
}
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
if f.Anonymous {
pFields := getMetaFields(f.Type)
for k, v := range pFields {
fields[k] = v
}
} else {
name, metaOptions := parseAnnotationMeta(f.Tag)
if "" == name {
continue
}
fields[name] = &AnnotationFieldMeta{
fieldName: f.Name,
options: metaOptions,
}
}
}
return fields
}
func parseAnnotationMeta(tag reflect.StructTag) (string, anotationMetaOptions) {
s := strings.Trim(tag.Get(AnnotationMetaTag), " ")
if "" == s {
return "", ""
}
if idx := strings.Index(s, AnnotationAttributeSpliter); idx != -1 {
return s[:idx], anotationMetaOptions(s[idx+1:])
}
return s, anotationMetaOptions("")
}
func setMetaAttributes(def *AnnotationDefinition, rv reflect.Value, attributes map[string]string) {
for k, v := range attributes {
meta := def.fields[k]
f := rv.FieldByName(meta.fieldName)
f.Set(reflect.ValueOf(v))
}
}

View File

@ -1,31 +0,0 @@
package annotation
import (
"fmt"
"reflect"
)
const (
ResourceTag = "@Resource"
)
func init() {
registerAnnotation(ResourceTag, reflect.TypeOf((*Resource)(nil)))
}
type Resource struct {
Annotation
Name string
}
func (a *Resource) parseAttribute(attributes map[string]string) error {
for k, v := range attributes {
switch k {
case "name":
a.Name = v
default:
return fmt.Errorf("Syntax error: not supported attribute[%s]", k)
}
}
return nil
}

5
annotation/type.go Normal file
View File

@ -0,0 +1,5 @@
package annotation
type TypeAnnotation interface {
Annotation
}

View File

@ -16,10 +16,10 @@ package di
// destroyMethod string // destroyMethod string
// scope enum singleton, transiant // scope enum singleton, transiant
type ScopeType int type ScopeType string
const ( const (
ScopeTypeDefault ScopeType = iota // == ScopeTypeSingleton ScopeTypeDefault ScopeType = "Default" // == ScopeTypeSingleton
ScopeTypeSingleton ScopeTypeSingleton ScopeType = "Singleton"
ScopeTypeTransiant ScopeTypeTransiant ScopeType = "Transiant"
) )

View File

@ -0,0 +1,26 @@
package annotation
// @Inject(name? string)
import (
"reflect"
"git.loafle.net/commons_go/di"
cda "git.loafle.net/commons_go/di/annotation"
)
const (
ComponentTag = "@Component"
)
func init() {
cda.RegisterAnnotation(ComponentTag, reflect.TypeOf((*Component)(nil)))
}
type Component struct {
cda.Annotation
Name string `annotation-meta:"name"`
InitMethod string `annotation-meta:"initMethod"` // func (receiver interface{}, cr ComponentRegistry) error
DestroyMethod string `annotation-meta:"destroyMethod"` // func (receiver interface{}, cr ComponentRegistry) error
Scope di.ScopeType `annotation-meta:"scope"`
}

View File

@ -0,0 +1,22 @@
package annotation
// @Inject(name? string)
import (
"reflect"
cda "git.loafle.net/commons_go/di/annotation"
)
const (
InjectTag = "@Inject"
)
func init() {
cda.RegisterAnnotation(InjectTag, reflect.TypeOf((*Inject)(nil)))
}
type Inject struct {
cda.Annotation
Name string `annotation-meta:"name"`
}

View File

@ -0,0 +1,20 @@
package annotation
import (
"reflect"
cda "git.loafle.net/commons_go/di/annotation"
)
const (
ResourceTag = "@Resource"
)
func init() {
cda.RegisterAnnotation(ResourceTag, reflect.TypeOf((*Resource)(nil)))
}
type Resource struct {
cda.Annotation
Name string `annotation-meta:"name"`
}

View File

@ -14,9 +14,17 @@ type TypeDefinition struct {
Type reflect.Type Type reflect.Type
RealType reflect.Type RealType reflect.Type
TypeAnnotations map[string]cda.Annotation
Fields []*FieldDefinition Fields []*FieldDefinition
} }
func (td *TypeDefinition) GetAnnotation(name string) cda.Annotation {
if nil == td.TypeAnnotations {
return nil
}
return td.TypeAnnotations[name]
}
type FieldDefinition struct { type FieldDefinition struct {
FieldName string FieldName string
PkgName string PkgName string

View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
cda "git.loafle.net/commons_go/di/annotation" cda "git.loafle.net/commons_go/di/annotation"
cdia "git.loafle.net/commons_go/di/injection/annotation"
cdur "git.loafle.net/commons_go/di/util/reflect" cdur "git.loafle.net/commons_go/di/util/reflect"
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
) )
@ -19,7 +20,7 @@ func init() {
var registry ComponentRegistry var registry ComponentRegistry
type ComponentRegistry interface { type ComponentRegistry interface {
RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) RegisterType(t reflect.Type)
RegisterResource(name string, resource interface{}) error RegisterResource(name string, resource interface{}) error
GetInstance(t reflect.Type) interface{} GetInstance(t reflect.Type) interface{}
@ -43,16 +44,16 @@ type defaultComponentRegistry struct {
resourceByName map[string]interface{} resourceByName map[string]interface{}
} }
func RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) { func RegisterType(t reflect.Type) {
pc, _, _, ok := runtime.Caller(1) pc, _, _, ok := runtime.Caller(1)
details := runtime.FuncForPC(pc) details := runtime.FuncForPC(pc)
if ok && details != nil { if ok && details != nil {
log.Printf("called from %s\n", details.Name()) log.Printf("called from %s\n", details.Name())
} }
registry.RegisterType(t, ca) registry.RegisterType(t)
} }
func (cr *defaultComponentRegistry) RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) { func (cr *defaultComponentRegistry) RegisterType(t reflect.Type) {
if nil == t { if nil == t {
logging.Logger().Panic("DI: t[reflect.Type] is nil") logging.Logger().Panic("DI: t[reflect.Type] is nil")
} }
@ -70,6 +71,9 @@ func (cr *defaultComponentRegistry) RegisterType(t reflect.Type, ca *cda.Compone
} }
cr.definitionByType[td.RealType] = td cr.definitionByType[td.RealType] = td
// _ca := ca.(*cdia.ComponentAnnotation)
ca := td.GetAnnotation(cdia.ComponentTag).(*cdia.Component)
name := "" name := ""
if nil != ca && "" != strings.Trim(ca.Name, " ") { if nil != ca && "" != strings.Trim(ca.Name, " ") {
name = ca.Name name = ca.Name
@ -145,13 +149,13 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) interface{} {
logging.Logger().Panic(fmt.Sprintf("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(cdia.InjectTag)
if nil != annotation { if nil != annotation {
fV = cr.GetInstance(fd.Type) fV = cr.GetInstance(fd.Type)
} }
annotation = fd.GetAnnotation(cda.ResourceTag) annotation = fd.GetAnnotation(cdia.ResourceTag)
if nil != annotation { if nil != annotation {
n := annotation.(*cda.Resource).Name n := annotation.(*cdia.Resource).Name
if "" == n { if "" == n {
n = fd.FieldName n = fd.FieldName
} }
@ -193,32 +197,32 @@ func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefini
td.TypeName = tName td.TypeName = tName
td.Type = t td.Type = t
td.RealType = rt td.RealType = rt
td.Fields = make([]*FieldDefinition, 0)
fields := getFields(rt) parseFields(rt, td)
if 0 < len(fields) {
td.Fields = fields
}
return td, nil return td, nil
} }
func getFields(t reflect.Type) []*FieldDefinition { func parseFields(t reflect.Type, td *TypeDefinition) {
fields := make([]*FieldDefinition, 0) // fields := make([]*FieldDefinition, 0)
rt, _, _ := cdur.GetTypeInfo(t) rt, _, _ := cdur.GetTypeInfo(t)
if reflect.Struct != rt.Kind() { if reflect.Struct != rt.Kind() {
return fields return
} }
for i := 0; i < rt.NumField(); i++ { for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i) f := rt.Field(i)
if f.Anonymous { if f.Anonymous {
fields = append(fields, getFields(f.Type)...) if parseAnonymousField(f, td) {
continue
}
parseFields(f.Type, td)
} else { } else {
as, err := cda.ParseAnnotation(f.Tag) as, err := cda.ParseAnnotation(f.Tag)
if nil != err { if nil != err {
return fields return
} }
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)
@ -231,10 +235,31 @@ func getFields(t reflect.Type) []*FieldDefinition {
RealType: fRT, RealType: fRT,
Annotations: as, Annotations: as,
} }
fields = append(fields, fd) td.Fields = append(td.Fields, fd)
}
} }
} }
} }
return fields 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
}
as, err := cda.ParseAnnotation(f.Tag)
if nil != err {
return false
}
if nil != as && 0 < len(as) {
td.TypeAnnotations = as
return true
}
return false
} }

View File

@ -4,10 +4,13 @@ import (
"log" "log"
"reflect" "reflect"
"testing" "testing"
"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((*AService)(nil)))
RegisterType(reflect.TypeOf((*BService)(nil)))
// RegisterType(reflect.TypeOf((*TestStruct2)(nil)), &cda.ComponentAnnotation{ // RegisterType(reflect.TypeOf((*TestStruct2)(nil)), &cda.ComponentAnnotation{
// Name: "test1", // Name: "test1",
// }) // })
@ -20,21 +23,21 @@ func TestRegisterType(t *testing.T) {
RegisterResource("List", []string{"dfdkf", "skgkfg"}) RegisterResource("List", []string{"dfdkf", "skgkfg"})
i, err := GetInstance(reflect.TypeOf((*CService)(nil))) cs := GetInstance(reflect.TypeOf((*CService)(nil))).(*CService)
if nil != err {
log.Printf("%v", err)
} else {
cs := i.(*CService)
log.Printf("%v", cs) log.Printf("%v", cs)
}
} }
type AService struct { type AService struct {
annotation.TypeAnnotation `annotation:"@Component(name=dkdkdf)"`
NameA string NameA string
} }
type BService struct { type BService struct {
annotation.TypeAnnotation `annotation:"@Component()"`
NameB string NameB string
} }