-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support marshal slice type if's not proto message (#7)
* feat: support marshal slice type if's not proto message * test: add marshal a nil slice type case
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package gateway | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestMarshalNonProtoSliceField(t *testing.T) { | ||
type testData struct { | ||
List []string | ||
} | ||
|
||
data := testData{} | ||
j := &JSONPb{} | ||
|
||
t.Run("disable EmitDefaults", func(t *testing.T) { | ||
j.EmitDefaults = false | ||
|
||
r, err := j.Marshal(data.List) | ||
if err != nil { | ||
t.Errorf("Marshal failed with %v", err) | ||
} | ||
if want := "null"; want != string(r) { | ||
t.Errorf("want (%v), got (%v)", want, string(r)) | ||
} | ||
}) | ||
|
||
t.Run("enable EmitDefaults", func(t *testing.T) { | ||
j.EmitDefaults = true | ||
|
||
r, err := j.Marshal(data.List) | ||
if err != nil { | ||
t.Errorf("Marshal failed with %v", err) | ||
} | ||
if want := "[]"; want != string(r) { | ||
t.Errorf("want (%v), got (%v)", want, string(r)) | ||
} | ||
}) | ||
} |