Skip to content

Commit

Permalink
fix codec.Frame in case of flatten struct tag
Browse files Browse the repository at this point in the history
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
  • Loading branch information
vtolstov committed Sep 23, 2021
1 parent 6673e7d commit 016b3aa
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 34 deletions.
32 changes: 32 additions & 0 deletions codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,40 @@ package proto
import (
"bytes"
"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) {
t.Skip("skip as no proto")
s := &struct {
Expand Down
74 changes: 40 additions & 34 deletions proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,61 @@ type protoCodec struct {
opts codec.Options
}

var _ codec.Codec = &protoCodec{}

const (
flattenTag = "flatten"
)

func (c *protoCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
switch m := v.(type) {
case nil:
if v == nil {
return nil, nil
case *codec.Frame:
}

options := c.opts
for _, o := range opts {
o(&options)
}

if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
}

if m, ok := v.(*codec.Frame); ok {
return m.Data, nil
case proto.Message:
options := c.opts
for _, o := range opts {
o(&options)
}

if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
m = nm
}
}
return proto.Marshal(m)
}
return nil, codec.ErrInvalidMessage

if _, ok := v.(proto.Message); !ok {
return nil, codec.ErrInvalidMessage
}

return proto.Marshal(v.(proto.Message))
}

func (c *protoCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
if len(d) == 0 {
if v == nil || len(d) == 0 {
return nil
}
switch m := v.(type) {
case nil:
return nil
case *codec.Frame:

options := c.opts
for _, o := range opts {
o(&options)
}

if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
}

if m, ok := v.(*codec.Frame); ok {
m.Data = d
case proto.Message:
options := c.opts
for _, o := range opts {
o(&options)
}

if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
m = nm
}
}
return proto.Unmarshal(d, m)
return nil
}
return codec.ErrInvalidMessage

if _, ok := v.(proto.Message); !ok {
return codec.ErrInvalidMessage
}

return proto.Unmarshal(d, v.(proto.Message))
}

func (c *protoCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
Expand Down

0 comments on commit 016b3aa

Please sign in to comment.