Compare commits

...

2 Commits
v3.10.0 ... v3

Author SHA1 Message Date
31b7e7182e change yaml package to support yaml tags
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-09-30 20:21:09 +03:00
560c8d9704 update to latest micro, optimize flatten
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-09-17 12:16:07 +03:00
4 changed files with 1576 additions and 70 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)
}
@ -37,14 +37,18 @@ func TestFrameFlatten(t *testing.T) {
}
}
func TestReadBody(t *testing.T) {
func TestNativeYamlTags(t *testing.T) {
s := &struct {
Name string
}{}
c := NewCodec()
b := bytes.NewReader(nil)
err := c.ReadBody(b, s)
One string `yaml:"first"`
}{
One: "",
}
err := NewCodec().Unmarshal([]byte(`first: "val"`), s)
if err != nil {
t.Fatal(err)
}
if s.One != "val" {
t.Fatalf("XXX %#+v\n", s)
}
}

9
go.mod
View File

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

1563
go.sum

File diff suppressed because it is too large Load Diff

56
yaml.go
View File

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