fix codec.Frame in case of flatten struct tag

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-09-24 00:10:13 +03:00
parent 6ab7ec4b8f
commit dd93a0da3b
2 changed files with 84 additions and 50 deletions

View File

@ -3,8 +3,40 @@ package jsonpb
import ( import (
"bytes" "bytes"
"testing" "testing"
"github.com/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")},
}
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 TestReadBody(t *testing.T) { func TestReadBody(t *testing.T) {
t.Skip("skip without proto message") t.Skip("skip without proto message")
s := &struct { s := &struct {

View File

@ -33,21 +33,25 @@ const (
) )
func (c *jsonpbCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) { func (c *jsonpbCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
switch m := v.(type) { if v == nil {
case nil:
return nil, nil return nil, nil
case *codec.Frame: }
return m.Data, nil
case proto.Message:
options := c.opts options := c.opts
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)
} }
if nv, nerr := rutil.StructFieldByTag(m, options.TagName, flattenTag); nerr == nil { if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok { v = nv
m = nm
} }
if m, ok := v.(*codec.Frame); ok {
return m.Data, nil
}
if _, ok := v.(proto.Message); !ok {
return nil, codec.ErrInvalidMessage
} }
marshalOptions := DefaultMarshalOptions marshalOptions := DefaultMarshalOptions
@ -57,31 +61,30 @@ func (c *jsonpbCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, erro
} }
} }
return marshalOptions.Marshal(m) return marshalOptions.Marshal(v.(proto.Message))
}
return nil, codec.ErrInvalidMessage
} }
func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error { func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
if len(d) == 0 { if v == nil || len(d) == 0 {
return nil return nil
} }
switch m := v.(type) {
case nil:
return nil
case *codec.Frame:
m.Data = d
return nil
case proto.Message:
options := c.opts options := c.opts
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)
} }
if nv, nerr := rutil.StructFieldByTag(m, options.TagName, flattenTag); nerr == nil { if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok { v = nv
m = nm
} }
if m, ok := v.(*codec.Frame); ok {
m.Data = d
return nil
}
if _, ok := v.(proto.Message); !ok {
return codec.ErrInvalidMessage
} }
unmarshalOptions := DefaultUnmarshalOptions unmarshalOptions := DefaultUnmarshalOptions
@ -90,9 +93,8 @@ func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) e
unmarshalOptions = f unmarshalOptions = f
} }
} }
return unmarshalOptions.Unmarshal(d, m)
} return unmarshalOptions.Unmarshal(d, v.(proto.Message))
return codec.ErrInvalidMessage
} }
func (c *jsonpbCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error { func (c *jsonpbCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {