2 Commits

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 1580 additions and 85 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)
} }
@@ -37,14 +37,18 @@ func TestFrameFlatten(t *testing.T) {
} }
} }
func TestReadBody(t *testing.T) { func TestNativeYamlTags(t *testing.T) {
s := &struct { s := &struct {
Name string One string `yaml:"first"`
}{} }{
c := NewCodec() One: "",
b := bytes.NewReader(nil) }
err := c.ReadBody(b, s)
err := NewCodec().Unmarshal([]byte(`first: "val"`), s)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if s.One != "val" {
t.Fatalf("XXX %#+v\n", s)
}
} }

19
go.mod
View File

@@ -1,17 +1,10 @@
module go.unistack.org/micro-codec-yaml/v4 module go.unistack.org/micro-codec-yaml/v3
go 1.19 go 1.16
require ( require (
go.unistack.org/micro-proto/v4 v4.0.0 go.unistack.org/micro-proto/v3 v3.4.1
go.unistack.org/micro/v4 v4.0.1 go.unistack.org/micro/v3 v3.10.94
sigs.k8s.io/yaml v1.3.0 google.golang.org/protobuf v1.34.2 // indirect
) gopkg.in/yaml.v3 v3.0.1
require (
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.2.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
) )

1562
go.sum

File diff suppressed because it is too large Load Diff

64
yaml.go
View File

@@ -1,13 +1,11 @@
// Package yaml provides a yaml codec // Package yaml provides a yaml codec
package yaml // import "go.unistack.org/micro-codec-yaml/v4" package yaml // import "go.unistack.org/micro-codec-yaml/v3"
import ( import (
"io" pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v3/codec"
pb "go.unistack.org/micro-proto/v4/codec" rutil "go.unistack.org/micro/v3/util/reflect"
"go.unistack.org/micro/v4/codec" yaml "gopkg.in/yaml.v3"
rutil "go.unistack.org/micro/v4/util/reflect"
"sigs.k8s.io/yaml"
) )
type yamlCodec struct { type yamlCodec struct {
@@ -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"
} }