1 Commits

Author SHA1 Message Date
31ea6d6989 update to latest micro, optimize flatten
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-09-17 12:18:41 +03:00
4 changed files with 1575 additions and 71 deletions

View File

@@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"testing" "testing"
"go.unistack.org/micro/v4/codec" "go.unistack.org/micro/v3/codec"
) )
func TestFrame(t *testing.T) { func TestFrame(t *testing.T) {
@@ -28,7 +28,7 @@ func TestFrameFlatten(t *testing.T) {
Name: &codec.Frame{Data: []byte("test")}, Name: &codec.Frame{Data: []byte("test")},
} }
buf, err := NewCodec().Marshal(s) buf, err := NewCodec(codec.Flatten(true)).Marshal(s)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -36,15 +36,3 @@ func TestFrameFlatten(t *testing.T) {
t.Fatalf("bytes not equal %s != %s", buf, `test`) 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)
}
}

12
go.mod
View File

@@ -1,10 +1,10 @@
module go.unistack.org/micro-codec-xml/v4 module go.unistack.org/micro-codec-xml/v3
go 1.19 go 1.16
require ( require (
go.unistack.org/micro-proto/v4 v4.0.0 github.com/imdario/mergo v0.3.13 // indirect
go.unistack.org/micro/v4 v4.0.1 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
) )
require google.golang.org/protobuf v1.28.1 // indirect

1561
go.sum

File diff suppressed because it is too large Load Diff

57
xml.go
View File

@@ -1,13 +1,12 @@
// Package xml provides a xml codec // Package xml provides a xml codec
package xml // import "go.unistack.org/micro-codec-xml/v4" package xml // import "go.unistack.org/micro-codec-xml/v3"
import ( import (
"encoding/xml" "encoding/xml"
"io"
pb "go.unistack.org/micro-proto/v4/codec" pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v4/codec" "go.unistack.org/micro/v3/codec"
rutil "go.unistack.org/micro/v4/util/reflect" rutil "go.unistack.org/micro/v3/util/reflect"
) )
var _ codec.Codec = &xmlCodec{} var _ codec.Codec = &xmlCodec{}
@@ -29,8 +28,11 @@ func (c *xmlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error)
for _, o := range opts { for _, o := range opts {
o(&options) 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) { switch m := v.(type) {
@@ -53,8 +55,10 @@ func (c *xmlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) erro
o(&options) o(&options)
} }
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil { if options.Flatten {
v = nv if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
}
} }
switch m := v.(type) { 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) 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 { func (c *xmlCodec) String() string {
return "xml" return "xml"
} }