bug fixed

This commit is contained in:
병준 박 2019-11-11 23:41:01 +09:00
parent bef304a864
commit 27dd1f6347
3 changed files with 60 additions and 156 deletions

View File

@ -88,39 +88,39 @@ func splitAnnotation(s string) (map[string]string, error) {
return ss, nil return ss, nil
} }
func splitAnnotationAttribute(s string) (map[string]string, error) { // func splitAnnotationAttribute(s string) (map[string]string, error) {
ss := make(map[string]string, 0) // ss := make(map[string]string, 0)
ts := s // ts := s
for { // for {
as := strings.Index(ts, AnnotationKeyValueSpliter) // as := strings.Index(ts, AnnotationKeyValueSpliter)
if -1 == as { // if -1 == as {
break // break
} // }
aName := ts[:as] // aName := ts[:as]
aas := strings.Index(ts[as:], AnnotationAttributeStartChar) // aas := strings.Index(ts[as:], AnnotationAttributeStartChar)
aae := strings.Index(ts[as+aas+1:], AnnotationAttributeEndChar) // aae := strings.Index(ts[as+aas+1:], AnnotationAttributeEndChar)
if -1 == aas && -1 == aae { // if -1 == aas && -1 == aae {
ss[aName] = ts[as+1 : len(ts)] // ss[aName] = ts[as+1 : len(ts)]
} else if -1 != aas && -1 != aae { // } else if -1 != aas && -1 != aae {
ss[aName] = ts[as+aas+1 : as+aas+aae+1] // ss[aName] = ts[as+aas+1 : as+aas+aae+1]
} else { // } else {
return nil, fmt.Errorf("not valid string %s", ts) // return nil, fmt.Errorf("not valid string %s", ts)
} // }
asi := strings.Index(ts[as+aae:], AnnotationAttributeSpliter) // asi := strings.Index(ts[as+aae:], AnnotationAttributeSpliter)
if -1 == asi { // if -1 == asi {
break // break
} // }
if len(ts) <= (as + aae + asi + 1) { // if len(ts) <= (as + aae + asi + 1) {
break // break
} // }
ts = strings.TrimSpace(ts[as+aae+asi+1:]) // ts = strings.TrimSpace(ts[as+aae+asi+1:])
} // }
return ss, nil // return ss, nil
} // }
func newAnnotation(name string, attributes string) (reflect.Type, Annotation, error) { func newAnnotation(name string, attributes string) (reflect.Type, Annotation, error) {
def, ok := annotationRegistry[name] def, ok := annotationRegistry[name]

View File

@ -12,9 +12,9 @@ func TestSplitAnnotation(t *testing.T) {
log.Printf("%v", ss) log.Printf("%v", ss)
} }
func TestSplitAnnotationAttribute(t *testing.T) { // func TestSplitAnnotationAttribute(t *testing.T) {
s := "method='POST', entry='/account/signin', params='[signinID, signinPW]'" // s := "method='POST', entry='/account/signin', params='[signinID, signinPW]'"
ss, _ := splitAnnotationAttribute(s) // ss, _ := splitAnnotationAttribute(s)
log.Printf("%v", ss) // log.Printf("%v", ss)
} // }

View File

@ -3,8 +3,6 @@ package annotation
import ( import (
"reflect" "reflect"
"strings" "strings"
cur "git.loafle.net/commons/util-go/reflect"
) )
type AnnotationFieldMeta struct { type AnnotationFieldMeta struct {
@ -33,35 +31,35 @@ func (o anotationMetaOptions) Contains(optionName string) bool {
return false return false
} }
func getMetaFields(t reflect.Type) map[string]*AnnotationFieldMeta { // func getMetaFields(t reflect.Type) map[string]*AnnotationFieldMeta {
fields := make(map[string]*AnnotationFieldMeta, 0) // fields := make(map[string]*AnnotationFieldMeta, 0)
rt, _, _ := cur.GetTypeInfo(t) // rt, _, _ := cur.GetTypeInfo(t)
if reflect.Struct != rt.Kind() { // if reflect.Struct != rt.Kind() {
return fields // return fields
} // }
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 {
pFields := getMetaFields(f.Type) // pFields := getMetaFields(f.Type)
for k, v := range pFields { // for k, v := range pFields {
fields[k] = v // fields[k] = v
} // }
} else { // } else {
name, metaOptions := parseAnnotationMeta(f.Tag) // name, metaOptions := parseAnnotationMeta(f.Tag)
if "" == name { // if "" == name {
continue // continue
} // }
fields[name] = &AnnotationFieldMeta{ // fields[name] = &AnnotationFieldMeta{
fieldName: f.Name, // fieldName: f.Name,
options: metaOptions, // options: metaOptions,
} // }
} // }
} // }
return fields // return fields
} // }
func parseAnnotationMeta(tag reflect.StructTag) (string, anotationMetaOptions) { func parseAnnotationMeta(tag reflect.StructTag) (string, anotationMetaOptions) {
s := strings.Trim(tag.Get(AnnotationMetaTag), " ") s := strings.Trim(tag.Get(AnnotationMetaTag), " ")
@ -75,97 +73,3 @@ func parseAnnotationMeta(tag reflect.StructTag) (string, anotationMetaOptions) {
return s, anotationMetaOptions("") return s, anotationMetaOptions("")
} }
// func setMetaAttributes(def *AnnotationDefinition, rv reflect.Value, attributes map[string]string) {
// META_LOOP:
// for k, v := range attributes {
// meta := def.fields[k]
// if nil == meta {
// log.Printf("Attribute[%s] of Type[%s] is not exist", k, def.rt.Name())
// continue META_LOOP
// }
// f := rv.FieldByName(meta.fieldName)
// switch f.Type().Kind() {
// case reflect.Array:
// log.Printf("Array not support")
// case reflect.Slice:
// s, err := convertSlice(v, f.Type())
// if nil != err {
// continue META_LOOP
// }
// f.Set(reflect.ValueOf(s))
// case reflect.Map:
// s, err := convertMap(v, f.Type())
// if nil != err {
// continue META_LOOP
// }
// f.Set(reflect.ValueOf(s))
// case reflect.Struct:
// log.Printf("Struct not support")
// case reflect.Ptr:
// log.Printf("Pointer not support")
// default:
// f.Set(reflect.ValueOf(v))
// }
// }
// }
// func convertSlice(s string, t reflect.Type) (interface{}, error) {
// ts := s
// as := strings.Index(ts, AnnotationAttributeArrayStartChar)
// if -1 == as {
// return nil, fmt.Errorf("Value is not contain %s", AnnotationAttributeArrayStartChar)
// }
// ts = ts[as+1:]
// ae := strings.Index(ts, AnnotationAttributeArrayEndChar)
// if -1 == ae {
// return nil, fmt.Errorf("Value is not contain %s", AnnotationAttributeArrayEndChar)
// }
// ts = ts[:ae]
// is := strings.Split(ts, AnnotationAttributeArrayItemSpliter)
// if nil == is || 0 == len(is) {
// return nil, nil
// }
// for i, v := range is {
// is[i] = strings.TrimSpace(v)
// }
// return cur.ConvertToType(is, t)
// }
// func convertMap(s string, t reflect.Type) (interface{}, error) {
// ts := s
// as := strings.Index(ts, AnnotationAttributeMapStartChar)
// if -1 == as {
// return nil, fmt.Errorf("Value is not contain %s", AnnotationAttributeMapStartChar)
// }
// ts = ts[as+1:]
// ae := strings.Index(ts, AnnotationAttributeMapEndChar)
// if -1 == ae {
// return nil, fmt.Errorf("Value is not contain %s", AnnotationAttributeMapEndChar)
// }
// ts = ts[:ae]
// is := strings.Split(ts, AnnotationAttributeMapItemSpliter)
// if nil == is || 0 == len(is) {
// return nil, nil
// }
// ms := make(map[string]string)
// for _, s := range is {
// kvs := strings.Split(s, AnnotationAttributeMapKeyValueSpliter)
// if 2 != len(kvs) {
// return nil, fmt.Errorf("Map Value format k:v but %s", s)
// }
// k := strings.TrimSpace(kvs[0])
// v := strings.TrimSpace(kvs[1])
// ms[k] = v
// }
// return cur.ConvertToType(ms, t)
// }