diff --git a/registry.go b/registry.go index 9c72355..39cfa9a 100644 --- a/registry.go +++ b/registry.go @@ -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