bug fixed

This commit is contained in:
병준 박 2019-11-11 23:24:24 +09:00
parent 997e367d4f
commit bef304a864

View File

@ -1,8 +1,6 @@
package annotation package annotation
import ( import (
"fmt"
"log"
"reflect" "reflect"
"strings" "strings"
@ -14,8 +12,6 @@ type AnnotationFieldMeta struct {
options anotationMetaOptions options anotationMetaOptions
} }
type anotationMetaOptions string type anotationMetaOptions string
func (o anotationMetaOptions) Contains(optionName string) bool { func (o anotationMetaOptions) Contains(optionName string) bool {
@ -80,96 +76,96 @@ func parseAnnotationMeta(tag reflect.StructTag) (string, anotationMetaOptions) {
return s, anotationMetaOptions("") return s, anotationMetaOptions("")
} }
func setMetaAttributes(def *AnnotationDefinition, rv reflect.Value, attributes map[string]string) { // func setMetaAttributes(def *AnnotationDefinition, rv reflect.Value, attributes map[string]string) {
META_LOOP: // META_LOOP:
for k, v := range attributes { // for k, v := range attributes {
meta := def.fields[k] // meta := def.fields[k]
if nil == meta { // if nil == meta {
log.Printf("Attribute[%s] of Type[%s] is not exist", k, def.rt.Name()) // log.Printf("Attribute[%s] of Type[%s] is not exist", k, def.rt.Name())
continue META_LOOP // continue META_LOOP
} // }
f := rv.FieldByName(meta.fieldName) // f := rv.FieldByName(meta.fieldName)
switch f.Type().Kind() { // switch f.Type().Kind() {
case reflect.Array: // case reflect.Array:
log.Printf("Array not support") // log.Printf("Array not support")
case reflect.Slice: // case reflect.Slice:
s, err := convertSlice(v, f.Type()) // s, err := convertSlice(v, f.Type())
if nil != err { // if nil != err {
continue META_LOOP // continue META_LOOP
} // }
f.Set(reflect.ValueOf(s)) // f.Set(reflect.ValueOf(s))
case reflect.Map: // case reflect.Map:
s, err := convertMap(v, f.Type()) // s, err := convertMap(v, f.Type())
if nil != err { // if nil != err {
continue META_LOOP // continue META_LOOP
} // }
f.Set(reflect.ValueOf(s)) // f.Set(reflect.ValueOf(s))
case reflect.Struct: // case reflect.Struct:
log.Printf("Struct not support") // log.Printf("Struct not support")
case reflect.Ptr: // case reflect.Ptr:
log.Printf("Pointer not support") // log.Printf("Pointer not support")
default: // default:
f.Set(reflect.ValueOf(v)) // f.Set(reflect.ValueOf(v))
} // }
} // }
} // }
func convertSlice(s string, t reflect.Type) (interface{}, error) { // func convertSlice(s string, t reflect.Type) (interface{}, error) {
ts := s // ts := s
as := strings.Index(ts, AnnotationAttributeArrayStartChar) // as := strings.Index(ts, AnnotationAttributeArrayStartChar)
if -1 == as { // if -1 == as {
return nil, fmt.Errorf("Value is not contain %s", AnnotationAttributeArrayStartChar) // return nil, fmt.Errorf("Value is not contain %s", AnnotationAttributeArrayStartChar)
} // }
ts = ts[as+1:] // ts = ts[as+1:]
ae := strings.Index(ts, AnnotationAttributeArrayEndChar) // ae := strings.Index(ts, AnnotationAttributeArrayEndChar)
if -1 == ae { // if -1 == ae {
return nil, fmt.Errorf("Value is not contain %s", AnnotationAttributeArrayEndChar) // return nil, fmt.Errorf("Value is not contain %s", AnnotationAttributeArrayEndChar)
} // }
ts = ts[:ae] // ts = ts[:ae]
is := strings.Split(ts, AnnotationAttributeArrayItemSpliter) // is := strings.Split(ts, AnnotationAttributeArrayItemSpliter)
if nil == is || 0 == len(is) { // if nil == is || 0 == len(is) {
return nil, nil // return nil, nil
} // }
for i, v := range is { // for i, v := range is {
is[i] = strings.TrimSpace(v) // is[i] = strings.TrimSpace(v)
} // }
return cur.ConvertToType(is, t) // return cur.ConvertToType(is, t)
} // }
func convertMap(s string, t reflect.Type) (interface{}, error) { // func convertMap(s string, t reflect.Type) (interface{}, error) {
ts := s // ts := s
as := strings.Index(ts, AnnotationAttributeMapStartChar) // as := strings.Index(ts, AnnotationAttributeMapStartChar)
if -1 == as { // if -1 == as {
return nil, fmt.Errorf("Value is not contain %s", AnnotationAttributeMapStartChar) // return nil, fmt.Errorf("Value is not contain %s", AnnotationAttributeMapStartChar)
} // }
ts = ts[as+1:] // ts = ts[as+1:]
ae := strings.Index(ts, AnnotationAttributeMapEndChar) // ae := strings.Index(ts, AnnotationAttributeMapEndChar)
if -1 == ae { // if -1 == ae {
return nil, fmt.Errorf("Value is not contain %s", AnnotationAttributeMapEndChar) // return nil, fmt.Errorf("Value is not contain %s", AnnotationAttributeMapEndChar)
} // }
ts = ts[:ae] // ts = ts[:ae]
is := strings.Split(ts, AnnotationAttributeMapItemSpliter) // is := strings.Split(ts, AnnotationAttributeMapItemSpliter)
if nil == is || 0 == len(is) { // if nil == is || 0 == len(is) {
return nil, nil // return nil, nil
} // }
ms := make(map[string]string) // ms := make(map[string]string)
for _, s := range is { // for _, s := range is {
kvs := strings.Split(s, AnnotationAttributeMapKeyValueSpliter) // kvs := strings.Split(s, AnnotationAttributeMapKeyValueSpliter)
if 2 != len(kvs) { // if 2 != len(kvs) {
return nil, fmt.Errorf("Map Value format k:v but %s", s) // return nil, fmt.Errorf("Map Value format k:v but %s", s)
} // }
k := strings.TrimSpace(kvs[0]) // k := strings.TrimSpace(kvs[0])
v := strings.TrimSpace(kvs[1]) // v := strings.TrimSpace(kvs[1])
ms[k] = v // ms[k] = v
} // }
return cur.ConvertToType(ms, t) // return cur.ConvertToType(ms, t)
} // }