bug fixed

This commit is contained in:
병준 박 2019-11-11 23:22:27 +09:00
parent 880b90003d
commit 997e367d4f
2 changed files with 14 additions and 130 deletions

View File

@ -1,6 +1,7 @@
package annotation package annotation
import ( import (
"encoding/json"
"fmt" "fmt"
"reflect" "reflect"
"strings" "strings"
@ -29,42 +30,6 @@ const (
type Annotation interface { type Annotation interface {
} }
// // @Inject(name? string)
// // @Resource(name? string)
// func ParseAnnotation(tag reflect.StructTag) (map[string]Annotation, error) {
// s := strings.Trim(tag.Get(AnnotationTag), " ")
// if "" == s {
// return nil, nil
// }
// annotations := strings.Split(s, AnnotationSpliter)
// if nil == annotations || 0 == len(annotations) {
// return nil, nil
// }
// rKVs := make(map[string]Annotation, 0)
// for _, a := range annotations {
// a = strings.Trim(a, " ")
// if "" == a {
// continue
// }
// aName, attributes, err := parseAnnotationItem(a)
// if nil != err {
// return nil, err
// }
// if "" == aName {
// continue
// }
// annotation, err := newAnnotation(aName, attributes)
// if nil != err {
// return nil, err
// }
// rKVs[aName] = annotation
// }
// return rKVs, nil
// }
// @Inject(name? string) // @Inject(name? string)
// @Resource(name? string) // @Resource(name? string)
func ParseAnnotation(tag reflect.StructTag) (map[reflect.Type]Annotation, error) { func ParseAnnotation(tag reflect.StructTag) (map[reflect.Type]Annotation, error) {
@ -94,8 +59,8 @@ func ParseAnnotation(tag reflect.StructTag) (map[reflect.Type]Annotation, error)
return rKVs, nil return rKVs, nil
} }
func splitAnnotation(s string) (map[string]map[string]string, error) { func splitAnnotation(s string) (map[string]string, error) {
ss := make(map[string]map[string]string, 0) ss := make(map[string]string, 0)
ts := s ts := s
for { for {
as := strings.Index(ts, AnnotationChar) as := strings.Index(ts, AnnotationChar)
@ -109,13 +74,9 @@ func splitAnnotation(s string) (map[string]map[string]string, error) {
aAttributes := ts[aas+1 : aae] aAttributes := ts[aas+1 : aae]
if 0 < len(aAttributes) { if 0 < len(aAttributes) {
attributes, err := splitAnnotationAttribute(aAttributes) ss[aName] = aAttributes
if nil != err {
return nil, err
}
ss[aName] = attributes
} else { } else {
ss[aName] = nil ss[aName] = ""
} }
if len(ts) <= (aae + 1) { if len(ts) <= (aae + 1) {
@ -161,7 +122,7 @@ func splitAnnotationAttribute(s string) (map[string]string, error) {
return ss, nil return ss, nil
} }
func newAnnotation(name string, attributes map[string]string) (reflect.Type, Annotation, error) { func newAnnotation(name string, attributes string) (reflect.Type, Annotation, error) {
def, ok := annotationRegistry[name] def, ok := annotationRegistry[name]
if !ok { if !ok {
return nil, nil, fmt.Errorf("There is no annotation[%s]", name) return nil, nil, fmt.Errorf("There is no annotation[%s]", name)
@ -170,84 +131,12 @@ func newAnnotation(name string, attributes map[string]string) (reflect.Type, Ann
v := reflect.New(def.rt) v := reflect.New(def.rt)
i := v.Interface().(Annotation) i := v.Interface().(Annotation)
if nil != attributes { if "" != attributes {
setMetaAttributes(def, v.Elem(), attributes) _json := fmt.Sprintf("{%s}", attributes)
if err := json.Unmarshal([]byte(_json), i); nil != err {
return nil, nil, err
}
} }
return def.t, i, nil return def.t, i, nil
} }
// func parseAnnotationItem(a string) (name string, attributes map[string]string, err error) {
// s := strings.Trim(a, " ")
// if "" == s {
// return
// }
// i := strings.Index(s, AnnotationChar)
// if -1 == i {
// err = fmt.Errorf("Syntax error: annotation must be started %s", AnnotationChar)
// return
// }
// aStart := strings.Index(s, AnnotationStartChar)
// if -1 == aStart {
// // This is pure annotation ex)@Resource
// name = s
// return
// }
// name = s[:aStart]
// aEnd := strings.Index(s, AnnotationEndChar)
// if -1 == aEnd {
// // This is syntax error ex)@Resource(
// err = fmt.Errorf("Syntax error: annotation must be ended %s", AnnotationEndChar)
// return
// }
// if 1 >= aEnd-aStart {
// return
// }
// attr := s[aStart+1 : aEnd]
// attr = strings.Trim(attr, " ")
// if "" == attr {
// return
// }
// attrs, pErr := parseAttributeString(s[aStart+1 : aEnd])
// if nil != pErr {
// err = pErr
// return
// }
// attributes = attrs
// return
// }
// func parseAttributeString(s string) (map[string]string, error) {
// attr := strings.Trim(s, " ")
// if "" == attr {
// return nil, nil
// }
// kvs := strings.Split(attr, AnnotationAttributeSpliter)
// if nil == kvs || 0 == len(kvs) {
// return nil, nil
// }
// rKVs := make(map[string]string)
// for i := 0; i < len(kvs); i++ {
// s := strings.Trim(kvs[i], " ")
// if "" == s {
// continue
// }
// kv := strings.Split(s, AnnotationKeyValueSpliter)
// k := strings.Trim(kv[0], " ")
// v := strings.Trim(kv[1], " ")
// rKVs[k] = v
// }
// return rKVs, nil
// }

View File

@ -27,12 +27,9 @@ func RegisterAnnotation(t reflect.Type) error {
return fmt.Errorf("DI: name[%s] of annotation exist already", name) return fmt.Errorf("DI: name[%s] of annotation exist already", name)
} }
meta := getMetaFields(t)
def := &AnnotationDefinition{ def := &AnnotationDefinition{
t: t, t: t,
rt: rt, rt: rt,
fields: meta,
} }
annotationRegistry[name] = def annotationRegistry[name] = def
@ -43,8 +40,6 @@ func RegisterAnnotation(t reflect.Type) error {
type AnnotationDefinition struct { type AnnotationDefinition struct {
t reflect.Type t reflect.Type
rt reflect.Type rt reflect.Type
fields map[string]*AnnotationFieldMeta
} }
func getTypeAnnotationField(t reflect.Type) *reflect.StructField { func getTypeAnnotationField(t reflect.Type) *reflect.StructField {