2020-12-01 00:26:20 +03:00
|
|
|
// Package toml provides a toml codec
|
|
|
|
package toml
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tomlCodec struct{}
|
|
|
|
|
|
|
|
func (c *tomlCodec) Marshal(b interface{}) ([]byte, error) {
|
|
|
|
switch m := b.(type) {
|
|
|
|
case nil:
|
|
|
|
return nil, nil
|
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
defer buf.Reset()
|
|
|
|
err := toml.NewEncoder(buf).Encode(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tomlCodec) Unmarshal(b []byte, v interface{}) error {
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch m := v.(type) {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return toml.Unmarshal(b, v)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:01:10 +03:00
|
|
|
func (c *tomlCodec) ReadBody(conn io.Reader, b interface{}) error {
|
2020-12-01 00:26:20 +03:00
|
|
|
switch m := b.(type) {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
case *codec.Frame:
|
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Data = buf
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return toml.Unmarshal(buf, b)
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:01:10 +03:00
|
|
|
func (c *tomlCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
2020-12-01 00:26:20 +03:00
|
|
|
switch m := b.(type) {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
case *codec.Frame:
|
|
|
|
_, err := conn.Write(m.Data)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return toml.NewEncoder(conn).Encode(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tomlCodec) String() string {
|
|
|
|
return "toml"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCodec() codec.Codec {
|
|
|
|
return &tomlCodec{}
|
|
|
|
}
|