Compare commits

..

4 Commits
v3 ... master

Author SHA1 Message Date
551d522616 Merge pull request 'Update workflows' (#96) from atolstikhin/micro-codec-yaml:master into master
All checks were successful
test / test (push) Successful in 12m44s
Reviewed-on: #96
2024-12-13 12:23:52 +03:00
Aleksandr Tolstikhin
8b2b346af9 Update workflows 2024-12-13 11:17:55 +07:00
19f0e4b27e Merge pull request 'move to micro v4' (#95) from v4 into master
Reviewed-on: #95
2023-04-30 10:56:15 +03:00
e48795153d move to micro v4
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-04-30 10:55:48 +03:00
4 changed files with 85 additions and 1580 deletions

View File

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

19
go.mod
View File

@ -1,10 +1,17 @@
module go.unistack.org/micro-codec-yaml/v3
module go.unistack.org/micro-codec-yaml/v4
go 1.16
go 1.19
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
go.unistack.org/micro-proto/v4 v4.0.0
go.unistack.org/micro/v4 v4.0.1
sigs.k8s.io/yaml v1.3.0
)
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,11 +1,13 @@
// Package yaml provides a yaml codec
package yaml // import "go.unistack.org/micro-codec-yaml/v3"
package yaml // import "go.unistack.org/micro-codec-yaml/v4"
import (
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"
"io"
pb "go.unistack.org/micro-proto/v4/codec"
"go.unistack.org/micro/v4/codec"
rutil "go.unistack.org/micro/v4/util/reflect"
"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"
}