Compare commits

..

4 Commits
v3 ... master

Author SHA1 Message Date
5bd30c4e88 Merge pull request 'Update workflows' (#97) from atolstikhin/micro-codec-xml:master into master
All checks were successful
test / test (push) Successful in 13m6s
Reviewed-on: #97
2024-12-14 16:25:28 +03:00
Aleksandr Tolstikhin
3e94d1ee73 Update workflows 2024-12-14 01:04:48 +07:00
6e982b87b3 Merge pull request 'move to micro v4' (#95) from v4 into master
Reviewed-on: #95
2023-04-30 10:53:42 +03:00
68b84a5c3d move to micro v4
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-04-30 10:53:24 +03:00
6 changed files with 73 additions and 1577 deletions

View File

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

12
go.mod
View File

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