This commit is contained in:
crusader 2018-08-23 18:28:10 +09:00
parent 9d80d778c6
commit ef45ff4ad6

View File

@ -129,21 +129,7 @@ func (r *AnnotationRegistry) getAnnotation(f *reflect.StructField) (map[reflect.
}
name := fmt.Sprintf("@%s", strings.TrimSpace(rs[1]))
body := rs[2]
if !AnnotationBodyRGX.MatchString(body) {
return nil, fmt.Errorf("Body[%s] of annotation[%s] is not valid", body, name)
}
body = AnnotationBodyRGX.ReplaceAllStringFunc(body, func(token string) string {
switch len(token) {
case 0, 1, 2:
return "\"\""
default:
return strings.Replace(fmt.Sprintf("\"%s\"", token[1:len(token)-1]), "\\'", "'", -1)
}
})
body = fmt.Sprintf("{%s}", strings.TrimSpace(body))
body := strings.TrimSpace(rs[2])
def, ok := r.definitions[name]
if !ok {
@ -153,9 +139,25 @@ func (r *AnnotationRegistry) getAnnotation(f *reflect.StructField) (map[reflect.
v := reflect.New(def.rt)
i := v.Interface()
err := json.Unmarshal([]byte(body), i)
if nil != err {
return nil, fmt.Errorf("Unmarshal failed %v", err)
if "" != body {
if !AnnotationBodyRGX.MatchString(body) {
return nil, fmt.Errorf("Body[%s] of annotation[%s] is not valid", body, name)
}
body = AnnotationBodyRGX.ReplaceAllStringFunc(body, func(token string) string {
switch len(token) {
case 0, 1, 2:
return "\"\""
default:
return strings.Replace(fmt.Sprintf("\"%s\"", token[1:len(token)-1]), "\\'", "'", -1)
}
})
body = fmt.Sprintf("{%s}", strings.TrimSpace(body))
err := json.Unmarshal([]byte(body), i)
if nil != err {
return nil, fmt.Errorf("Unmarshal failed %v", err)
}
}
annotations[def.t] = i