2020-12-01 00:26:20 +03:00
|
|
|
// Package toml provides a toml codec
|
2021-10-25 20:40:06 +03:00
|
|
|
package toml // import "go.unistack.org/micro-codec-toml/v3"
|
2020-12-01 00:26:20 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2023-03-05 23:34:31 +03:00
|
|
|
pb "go.unistack.org/micro-proto/v3/codec"
|
2021-10-25 20:26:12 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2020-12-01 00:26:20 +03:00
|
|
|
)
|
|
|
|
|
2021-09-24 00:29:28 +03:00
|
|
|
type tomlCodec struct {
|
|
|
|
opts codec.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ codec.Codec = &tomlCodec{}
|
2020-12-01 00:26:20 +03:00
|
|
|
|
2021-05-26 01:06:10 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
2021-09-24 00:29:28 +03:00
|
|
|
func (c *tomlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
|
|
|
if v == nil {
|
2020-12-01 00:26:20 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:29:28 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2021-05-26 01:06:10 +03:00
|
|
|
|
2021-09-24 00:29:28 +03:00
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
2021-05-26 01:06:10 +03:00
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
2023-03-05 23:34:31 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case *pb.Frame:
|
2021-09-24 00:29:28 +03:00
|
|
|
return m.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2021-05-26 01:06:10 +03:00
|
|
|
err := toml.NewEncoder(buf).Encode(v)
|
2020-12-01 00:26:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-24 00:29:28 +03:00
|
|
|
|
2020-12-01 00:26:20 +03:00
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:29:28 +03:00
|
|
|
func (c *tomlCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
|
|
|
if v == nil || len(d) == 0 {
|
2020-12-01 00:26:20 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 01:06:10 +03:00
|
|
|
|
2021-09-24 00:29:28 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2020-12-01 00:26:20 +03:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:29:28 +03:00
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
2021-05-26 01:06:10 +03:00
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
2023-03-05 23:34:31 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = d
|
|
|
|
return nil
|
|
|
|
case *pb.Frame:
|
2021-09-24 00:29:28 +03:00
|
|
|
m.Data = d
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return toml.Unmarshal(d, v)
|
2020-12-01 00:26:20 +03:00
|
|
|
}
|
|
|
|
|
2020-12-21 00:01:10 +03:00
|
|
|
func (c *tomlCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
2020-12-01 00:26:20 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:06:10 +03:00
|
|
|
func (c *tomlCodec) ReadBody(conn io.Reader, v interface{}) error {
|
|
|
|
if v == nil {
|
2020-12-01 00:26:20 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:06:10 +03:00
|
|
|
buf, err := io.ReadAll(conn)
|
2020-12-01 00:26:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-12 19:18:10 +03:00
|
|
|
} else if len(buf) == 0 {
|
2021-04-12 16:44:47 +03:00
|
|
|
return nil
|
2020-12-01 00:26:20 +03:00
|
|
|
}
|
2021-05-26 01:06:10 +03:00
|
|
|
return c.Unmarshal(buf, v)
|
2020-12-01 00:26:20 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:06:10 +03:00
|
|
|
func (c *tomlCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
|
|
|
if v == nil {
|
2020-12-01 00:26:20 +03:00
|
|
|
return nil
|
2021-05-26 01:06:10 +03:00
|
|
|
}
|
|
|
|
buf, err := c.Marshal(v)
|
|
|
|
if err != nil {
|
2020-12-01 00:26:20 +03:00
|
|
|
return err
|
2021-05-26 01:06:10 +03:00
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return codec.ErrInvalidMessage
|
2020-12-01 00:26:20 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:06:10 +03:00
|
|
|
_, err = conn.Write(buf)
|
|
|
|
return err
|
2020-12-01 00:26:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tomlCodec) String() string {
|
|
|
|
return "toml"
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:29:28 +03:00
|
|
|
func NewCodec(opts ...codec.Option) *tomlCodec {
|
|
|
|
return &tomlCodec{opts: codec.NewOptions(opts...)}
|
2020-12-01 00:26:20 +03:00
|
|
|
}
|