1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 05:00:54 +00:00

fix annotate to json with empty comment

This commit is contained in:
LinXiaoYi 2021-05-24 22:50:09 +08:00
parent 8401b2200b
commit 1658822bf3
2 changed files with 11 additions and 3 deletions

View File

@ -57,7 +57,13 @@ func (a *Annotator) Annotate(comment string) []map[string]interface{} {
//parse annotation to json //parse annotation to json
func (a *Annotator) AnnotateToJson(comment string) (string, error) { func (a *Annotator) AnnotateToJson(comment string) (string, error) {
if comment == "" {
return "", nil
}
annotate := a.Annotate(comment) annotate := a.Annotate(comment)
if len(annotate) == 0 {
return "", nil
}
result, err := json.MarshalIndent(annotate, "", " ") result, err := json.MarshalIndent(annotate, "", " ")
return string(result), err return string(result), err
} }

View File

@ -50,7 +50,7 @@ func TestAnnotate(t *testing.T) {
} }
func TestAnnotateToJson(t *testing.T) { func TestAnnotateToJson(t *testing.T) {
expect := `[ expect1 := `[
{ {
"Name": [ "Name": [
"Field1" "Field1"
@ -69,9 +69,11 @@ func TestAnnotateToJson(t *testing.T) {
} }
]` ]`
actual, _ := BeeAnnotator.AnnotateToJson(Annotation1) actual1, _ := BeeAnnotator.AnnotateToJson(Annotation1)
actual2, _ := BeeAnnotator.AnnotateToJson("")
assert.Equal(t, expect, actual) assert.Equal(t, expect1, actual1)
assert.Equal(t, "", actual2)
} }
func TestHandleWhitespaceValues(t *testing.T) { func TestHandleWhitespaceValues(t *testing.T) {