2020-11-27 17:43:13 +03:00
|
|
|
// Package proto provides a proto codec
|
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/segmentio/encoding/proto"
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
newproto "google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type protoCodec struct{}
|
|
|
|
|
|
|
|
func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
switch m := v.(type) {
|
2020-11-29 21:08:49 +03:00
|
|
|
case nil:
|
|
|
|
return nil, nil
|
2020-11-27 17:43:13 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
2021-03-22 10:30:43 +03:00
|
|
|
case newproto.Message, proto.Message:
|
2020-11-27 17:43:13 +03:00
|
|
|
return proto.Marshal(m)
|
|
|
|
}
|
|
|
|
return nil, codec.ErrInvalidMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
|
2021-03-07 19:28:12 +03:00
|
|
|
var err error
|
2020-11-29 21:08:49 +03:00
|
|
|
if d == nil {
|
2020-11-27 17:43:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch m := v.(type) {
|
2020-11-29 21:08:49 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2020-11-27 17:43:13 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = d
|
2021-03-22 10:30:43 +03:00
|
|
|
case newproto.Message, proto.Message:
|
2021-03-07 19:28:12 +03:00
|
|
|
err = proto.Unmarshal(d, m)
|
|
|
|
default:
|
|
|
|
err = codec.ErrInvalidMessage
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
2021-03-07 19:28:12 +03:00
|
|
|
return err
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
2020-12-21 00:00:27 +03:00
|
|
|
func (c *protoCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
2020-11-27 17:43:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:00:27 +03:00
|
|
|
func (c *protoCodec) ReadBody(conn io.Reader, b interface{}) error {
|
2020-11-27 17:43:13 +03:00
|
|
|
switch m := b.(type) {
|
2020-11-29 21:08:49 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2020-11-27 17:43:13 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Data = buf
|
|
|
|
return nil
|
2021-03-22 10:30:43 +03:00
|
|
|
case newproto.Message, proto.Message:
|
2020-11-27 17:43:13 +03:00
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return proto.Unmarshal(buf, m)
|
|
|
|
}
|
|
|
|
return codec.ErrInvalidMessage
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:00:27 +03:00
|
|
|
func (c *protoCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
2020-11-27 17:43:13 +03:00
|
|
|
switch m := b.(type) {
|
2020-11-29 21:08:49 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2020-11-27 17:43:13 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
_, err := conn.Write(m.Data)
|
|
|
|
return err
|
2021-03-22 10:30:43 +03:00
|
|
|
case newproto.Message, proto.Message:
|
2020-11-27 17:43:13 +03:00
|
|
|
buf, err := proto.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = conn.Write(buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return codec.ErrInvalidMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *protoCodec) String() string {
|
|
|
|
return "proto"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCodec() codec.Codec {
|
|
|
|
return &protoCodec{}
|
|
|
|
}
|