2022-01-13 00:34:00 +03:00
|
|
|
package msgpack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFrame(t *testing.T) {
|
|
|
|
s := &codec.Frame{Data: []byte("test")}
|
|
|
|
|
|
|
|
buf, err := NewCodec().Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(buf, []byte(`test`)) {
|
|
|
|
t.Fatalf("bytes not equal %s != %s", buf, `test`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFrameFlatten(t *testing.T) {
|
|
|
|
s := &struct {
|
|
|
|
One string
|
|
|
|
Name *codec.Frame `json:"name" codec:"flatten"`
|
|
|
|
}{
|
|
|
|
One: "xx",
|
|
|
|
Name: &codec.Frame{Data: []byte("test")},
|
|
|
|
}
|
|
|
|
|
2024-09-17 12:42:07 +03:00
|
|
|
buf, err := NewCodec(codec.Flatten(true)).Marshal(s)
|
2022-01-13 00:34:00 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(buf, []byte(`test`)) {
|
|
|
|
t.Fatalf("bytes not equal %s != %s", buf, `test`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructByTag(t *testing.T) {
|
|
|
|
type Str struct {
|
|
|
|
Name []string `msgpack:"name" codec:"flatten"`
|
|
|
|
}
|
|
|
|
|
|
|
|
val := &Str{Name: []string{"first", "second"}}
|
|
|
|
|
2024-09-17 12:42:07 +03:00
|
|
|
c := NewCodec(codec.Flatten(true))
|
2022-01-13 00:34:00 +03:00
|
|
|
buf, err := c.Marshal(val)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nval := &Str{}
|
|
|
|
err = c.Unmarshal(buf, nval)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(nval.Name) != 2 {
|
|
|
|
t.Fatalf("invalid unmarshal: %v", nval)
|
|
|
|
}
|
|
|
|
}
|