update to latest micro, optimize flatten

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-09-17 12:18:41 +03:00
parent e09c864070
commit 31ea6d6989
4 changed files with 1567 additions and 54 deletions

View File

@ -28,7 +28,7 @@ func TestFrameFlatten(t *testing.T) {
Name: &codec.Frame{Data: []byte("test")},
}
buf, err := NewCodec().Marshal(s)
buf, err := NewCodec(codec.Flatten(true)).Marshal(s)
if err != nil {
t.Fatal(err)
}
@ -36,15 +36,3 @@ func TestFrameFlatten(t *testing.T) {
t.Fatalf("bytes not equal %s != %s", buf, `test`)
}
}
func TestReadBody(t *testing.T) {
s := &struct {
Name string
}{}
c := NewCodec()
b := bytes.NewReader(nil)
err := c.ReadBody(b, s)
if err != nil {
t.Fatal(err)
}
}

7
go.mod
View File

@ -2,4 +2,9 @@ module go.unistack.org/micro-codec-xml/v3
go 1.16
require go.unistack.org/micro/v3 v3.10.14
require (
github.com/imdario/mergo v0.3.13 // indirect
go.unistack.org/micro-proto/v3 v3.4.1
go.unistack.org/micro/v3 v3.10.88
google.golang.org/protobuf v1.34.2 // indirect
)

1551
go.sum

File diff suppressed because it is too large Load Diff

49
xml.go
View File

@ -3,7 +3,6 @@ package xml // import "go.unistack.org/micro-codec-xml/v3"
import (
"encoding/xml"
"io"
pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v3/codec"
@ -29,8 +28,11 @@ func (c *xmlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error)
for _, o := range opts {
o(&options)
}
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
if options.Flatten {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
}
}
switch m := v.(type) {
@ -53,8 +55,10 @@ func (c *xmlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) erro
o(&options)
}
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
if options.Flatten {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
}
}
switch m := v.(type) {
@ -69,41 +73,6 @@ func (c *xmlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) erro
return xml.Unmarshal(b, v)
}
func (c *xmlCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
return nil
}
func (c *xmlCodec) ReadBody(conn io.Reader, v interface{}) error {
if v == nil {
return nil
}
buf, err := io.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
return c.Unmarshal(buf, v)
}
func (c *xmlCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
if v == nil {
return nil
}
buf, err := c.Marshal(v)
if err != nil {
return err
} else if len(buf) == 0 {
return codec.ErrInvalidMessage
}
_, err = conn.Write(buf)
return err
}
func (c *xmlCodec) String() string {
return "xml"
}