bug fixed
This commit is contained in:
parent
997e367d4f
commit
bef304a864
|
@ -1,8 +1,6 @@
|
|||
package annotation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
|
@ -14,8 +12,6 @@ type AnnotationFieldMeta struct {
|
|||
options anotationMetaOptions
|
||||
}
|
||||
|
||||
|
||||
|
||||
type anotationMetaOptions string
|
||||
|
||||
func (o anotationMetaOptions) Contains(optionName string) bool {
|
||||
|
@ -80,96 +76,96 @@ func parseAnnotationMeta(tag reflect.StructTag) (string, 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)
|
||||
// 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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
|
||||
// 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]
|
||||
// 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
|
||||
}
|
||||
// is := strings.Split(ts, AnnotationAttributeArrayItemSpliter)
|
||||
// if nil == is || 0 == len(is) {
|
||||
// return nil, nil
|
||||
// }
|
||||
|
||||
for i, v := range is {
|
||||
is[i] = strings.TrimSpace(v)
|
||||
}
|
||||
// for i, v := range is {
|
||||
// is[i] = strings.TrimSpace(v)
|
||||
// }
|
||||
|
||||
return cur.ConvertToType(is, t)
|
||||
}
|
||||
// return cur.ConvertToType(is, t)
|
||||
// }
|
||||
|
||||
func convertMap(s string, t reflect.Type) (interface{}, error) {
|
||||
ts := s
|
||||
// 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]
|
||||
// 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
|
||||
}
|
||||
// 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
|
||||
}
|
||||
// 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)
|
||||
}
|
||||
// return cur.ConvertToType(ms, t)
|
||||
// }
|
||||
|
|
Loading…
Reference in New Issue
Block a user