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
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
}

View File

@ -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) {