update to latest micro, optimize flatten

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-09-17 12:16:07 +03:00
parent a1f8ab7806
commit 560c8d9704
4 changed files with 1562 additions and 62 deletions

View File

@ -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)
}
}

10
go.mod
View File

@ -3,9 +3,11 @@ module go.unistack.org/micro-codec-yaml/v3
go 1.16 go 1.16
require ( require (
github.com/kr/pretty v0.2.1 // indirect github.com/imdario/mergo v0.3.13 // indirect
github.com/kr/text v0.2.0 // indirect github.com/kr/text v0.2.0 // indirect
go.unistack.org/micro/v3 v3.10.14 go.unistack.org/micro-proto/v3 v3.4.1
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect go.unistack.org/micro/v3 v3.10.88
sigs.k8s.io/yaml v1.3.0 google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
sigs.k8s.io/yaml v1.4.0
) )

1546
go.sum

File diff suppressed because it is too large Load Diff

54
yaml.go
View File

@ -2,8 +2,6 @@
package yaml // import "go.unistack.org/micro-codec-yaml/v3" package yaml // import "go.unistack.org/micro-codec-yaml/v3"
import ( import (
"io"
pb "go.unistack.org/micro-proto/v3/codec" pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v3/codec" "go.unistack.org/micro/v3/codec"
rutil "go.unistack.org/micro/v3/util/reflect" rutil "go.unistack.org/micro/v3/util/reflect"
@ -16,10 +14,6 @@ type yamlCodec struct {
var _ codec.Codec = &yamlCodec{} var _ codec.Codec = &yamlCodec{}
const (
flattenTag = "flatten"
)
func (c *yamlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) { func (c *yamlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
if v == nil { if v == nil {
return nil, nil return nil, nil
@ -29,8 +23,11 @@ func (c *yamlCodec) 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, "flatten"); nerr == nil {
v = nv
}
} }
switch m := v.(type) { switch m := v.(type) {
@ -53,8 +50,10 @@ func (c *yamlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) err
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, "flatten"); nerr == nil {
v = nv
}
} }
switch m := v.(type) { switch m := v.(type) {
@ -69,41 +68,6 @@ func (c *yamlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) err
return yaml.Unmarshal(b, v) return yaml.Unmarshal(b, v)
} }
func (c *yamlCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
return nil
}
func (c *yamlCodec) 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 *yamlCodec) 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 *yamlCodec) String() string { func (c *yamlCodec) String() string {
return "yaml" return "yaml"
} }