Compare commits

..

No commits in common. "v3" and "v3.10.0" have entirely different histories.
v3 ... v3.10.0

4 changed files with 70 additions and 1576 deletions

View File

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

9
go.mod
View File

@ -3,8 +3,9 @@ module go.unistack.org/micro-codec-yaml/v3
go 1.16
require (
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
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
)

1563
go.sum

File diff suppressed because it is too large Load Diff

56
yaml.go
View File

@ -2,10 +2,12 @@
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"
yaml "gopkg.in/yaml.v3"
"sigs.k8s.io/yaml"
)
type yamlCodec struct {
@ -14,6 +16,10 @@ 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
@ -23,11 +29,8 @@ func (c *yamlCodec) 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, "flatten"); nerr == nil {
v = nv
}
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
}
switch m := v.(type) {
@ -50,10 +53,8 @@ func (c *yamlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) err
o(&options)
}
if options.Flatten {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
v = nv
}
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
}
switch m := v.(type) {
@ -68,6 +69,41 @@ 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"
}