Compare commits

..

4 Commits
master ... v3

Author SHA1 Message Date
efb75b8357 add codec.RawMessage support
All checks were successful
test / test (push) Successful in 3m25s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-12-23 20:57:22 +03:00
3dac202602 Merge pull request 'Update workflows' (#96) from atolstikhin/micro-codec-xml:v3 into v3
All checks were successful
test / test (push) Successful in 13m31s
Reviewed-on: #96
2024-12-14 16:25:49 +03:00
Aleksandr Tolstikhin
cf29bc78a2 Update workflows 2024-12-14 01:06:05 +07:00
31ea6d6989 update to latest micro, optimize flatten
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-09-17 12:18:41 +03:00
6 changed files with 45 additions and 77 deletions

View File

@ -4,7 +4,7 @@ import (
"bytes"
"testing"
"go.unistack.org/micro/v4/codec"
"go.unistack.org/micro/v3/codec"
)
func TestFrame(t *testing.T) {
@ -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)
}
}

15
go.mod
View File

@ -1,10 +1,15 @@
module go.unistack.org/micro-codec-xml/v4
module go.unistack.org/micro-codec-xml/v3
go 1.19
go 1.21
toolchain go1.23.4
require (
go.unistack.org/micro-proto/v4 v4.0.0
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
)
require google.golang.org/protobuf v1.28.1 // indirect
require (
github.com/google/go-cmp v0.6.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect
)

20
go.sum
View File

@ -1,12 +1,8 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
go.unistack.org/micro-proto/v4 v4.0.0 h1:+TAwKSlx48eLsNO2E2cO0JLE8we1KU2AuWe9Q310FuU=
go.unistack.org/micro-proto/v4 v4.0.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo=
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
go.unistack.org/micro/v3 v3.10.88 h1:MxlzP+77Y6Kphb3lzHxROL4XfE/WdCQMQpnPv4D9Z8U=
go.unistack.org/micro/v3 v3.10.88/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=

59
xml.go
View File

@ -1,13 +1,12 @@
// 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 (
"encoding/xml"
"io"
pb "go.unistack.org/micro-proto/v4/codec"
"go.unistack.org/micro/v4/codec"
rutil "go.unistack.org/micro/v4/util/reflect"
pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v3/codec"
rutil "go.unistack.org/micro/v3/util/reflect"
)
var _ codec.Codec = &xmlCodec{}
@ -29,15 +28,22 @@ 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
}
}
switch m := v.(type) {
case *codec.Frame:
return m.Data, nil
case *pb.Frame:
return m.Data, nil
case codec.RawMessage:
return []byte(m), nil
case *codec.RawMessage:
return []byte(*m), nil
}
return xml.Marshal(v)
@ -53,9 +59,11 @@ 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
}
}
switch m := v.(type) {
case *codec.Frame:
@ -64,46 +72,17 @@ func (c *xmlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) erro
case *pb.Frame:
m.Data = b
return nil
case *codec.RawMessage:
*m = append((*m)[0:0], b...)
return nil
case codec.RawMessage:
copy(m, b)
return nil
}
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"
}