diff --git a/codec_test.go b/codec_test.go index 4cffe8e..4a8c576 100644 --- a/codec_test.go +++ b/codec_test.go @@ -3,8 +3,40 @@ package xml 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 `xml:"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) { s := &struct { Name string diff --git a/xml.go b/xml.go index dde53fc..4f59702 100644 --- a/xml.go +++ b/xml.go @@ -20,22 +20,22 @@ const ( ) func (c *xmlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) { - switch m := v.(type) { - case nil: + if v == nil { return nil, nil - case *codec.Frame: - return m.Data, nil } 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 + } + return xml.Marshal(v) } @@ -44,11 +44,6 @@ func (c *xmlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) erro return nil } - if m, ok := v.(*codec.Frame); ok { - m.Data = b - return nil - } - options := c.opts for _, o := range opts { o(&options) @@ -58,6 +53,11 @@ func (c *xmlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) erro v = nv } + if m, ok := v.(*codec.Frame); ok { + m.Data = b + return nil + } + return xml.Unmarshal(b, v) }