2020-11-30 23:40:08 +03:00
|
|
|
// Package yaml provides a yaml codec
|
2023-04-30 10:55:48 +03:00
|
|
|
package yaml // import "go.unistack.org/micro-codec-yaml/v4"
|
2020-11-30 23:40:08 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2023-04-30 10:55:48 +03:00
|
|
|
pb "go.unistack.org/micro-proto/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/codec"
|
|
|
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
2023-03-05 23:48:42 +03:00
|
|
|
"sigs.k8s.io/yaml"
|
2020-11-30 23:40:08 +03:00
|
|
|
)
|
|
|
|
|
2021-09-24 00:16:06 +03:00
|
|
|
type yamlCodec struct {
|
|
|
|
opts codec.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ codec.Codec = &yamlCodec{}
|
2020-11-30 23:40:08 +03:00
|
|
|
|
2021-05-26 01:20:27 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
2021-09-24 00:16:06 +03:00
|
|
|
func (c *yamlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
|
|
|
if v == nil {
|
2020-11-30 23:40:08 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:16:06 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
2021-05-26 01:20:27 +03:00
|
|
|
v = nv
|
|
|
|
}
|
2021-09-24 00:16:06 +03:00
|
|
|
|
2023-03-05 23:48:42 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case *pb.Frame:
|
2021-09-24 00:16:06 +03:00
|
|
|
return m.Data, nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:20:27 +03:00
|
|
|
return yaml.Marshal(v)
|
2020-11-30 23:40:08 +03:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:16:06 +03:00
|
|
|
func (c *yamlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
|
2021-05-26 01:20:27 +03:00
|
|
|
if len(b) == 0 || v == nil {
|
2020-11-30 23:40:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 01:20:27 +03:00
|
|
|
|
2021-09-24 00:16:06 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2020-11-30 23:40:08 +03:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:16:06 +03:00
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
2021-05-26 01:20:27 +03:00
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
2023-03-05 23:48:42 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
case *pb.Frame:
|
2021-09-24 00:16:06 +03:00
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:40:08 +03:00
|
|
|
return yaml.Unmarshal(b, v)
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:02:24 +03:00
|
|
|
func (c *yamlCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
2020-11-30 23:40:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:20:27 +03:00
|
|
|
func (c *yamlCodec) ReadBody(conn io.Reader, v interface{}) error {
|
|
|
|
if v == nil {
|
2020-11-30 23:40:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:20:27 +03:00
|
|
|
buf, err := io.ReadAll(conn)
|
2020-11-30 23:40:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-12 19:20:17 +03:00
|
|
|
} else if len(buf) == 0 {
|
2021-04-12 16:52:10 +03:00
|
|
|
return nil
|
2020-11-30 23:40:08 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:20:27 +03:00
|
|
|
return c.Unmarshal(buf, v)
|
2020-11-30 23:40:08 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:20:27 +03:00
|
|
|
func (c *yamlCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
|
|
|
if v == nil {
|
2020-11-30 23:40:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:20:27 +03:00
|
|
|
buf, err := c.Marshal(v)
|
2020-11-30 23:40:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-05-26 01:20:27 +03:00
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return codec.ErrInvalidMessage
|
2020-11-30 23:40:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Write(buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *yamlCodec) String() string {
|
|
|
|
return "yaml"
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:16:06 +03:00
|
|
|
func NewCodec(opts ...codec.Option) *yamlCodec {
|
|
|
|
return &yamlCodec{opts: codec.NewOptions(opts...)}
|
2020-11-30 23:40:08 +03:00
|
|
|
}
|