allow to flatten struct

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-05-25 22:46:40 +03:00
parent fb7611c3a9
commit 2dd2644b84
4 changed files with 49 additions and 494 deletions

View File

@@ -5,6 +5,33 @@ import (
"testing"
)
func TestStructByTag(t *testing.T) {
type Str struct {
Name []string `json:"name" codec:"flatten"`
}
val := &Str{Name: []string{"first", "second"}}
c := NewCodec()
buf, err := c.Marshal(val)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf, []byte(`["first","second"]`)) {
t.Fatalf("invalid marshal: %s", buf)
}
err = c.Unmarshal([]byte(`["1","2"]`), val)
if err != nil {
t.Fatal(err)
}
if len(val.Name) != 2 {
t.Fatalf("invalid unmarshal: %v", val)
}
}
func TestReadBody(t *testing.T) {
s := &struct {
Name string