diff --git a/parser/annotator.go b/parser/annotator.go index 30ca09e..4ea8382 100644 --- a/parser/annotator.go +++ b/parser/annotator.go @@ -57,7 +57,13 @@ func (a *Annotator) Annotate(comment string) []map[string]interface{} { //parse annotation to json func (a *Annotator) AnnotateToJson(comment string) (string, error) { + if comment == "" { + return "", nil + } annotate := a.Annotate(comment) + if len(annotate) == 0 { + return "", nil + } result, err := json.MarshalIndent(annotate, "", " ") return string(result), err } diff --git a/parser/annotator_test.go b/parser/annotator_test.go index d5f9974..6ef10fa 100644 --- a/parser/annotator_test.go +++ b/parser/annotator_test.go @@ -50,7 +50,7 @@ func TestAnnotate(t *testing.T) { } func TestAnnotateToJson(t *testing.T) { - expect := `[ + expect1 := `[ { "Name": [ "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) {