2020-11-03 01:22:02 +03:00
|
|
|
// Package proto provides a proto codec
|
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
2020-11-24 16:25:09 +03:00
|
|
|
type protoCodec struct{}
|
|
|
|
|
|
|
|
func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
switch m := v.(type) {
|
2020-11-29 21:06:14 +03:00
|
|
|
case nil:
|
|
|
|
return nil, nil
|
2020-11-24 16:25:09 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case proto.Message:
|
|
|
|
return proto.Marshal(m)
|
|
|
|
}
|
|
|
|
return nil, codec.ErrInvalidMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
|
2021-04-12 19:14:45 +03:00
|
|
|
if len(d) == 0 {
|
2020-11-24 16:25:09 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch m := v.(type) {
|
2020-11-29 21:06:14 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2020-11-24 16:25:09 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = d
|
|
|
|
case proto.Message:
|
|
|
|
return proto.Unmarshal(d, m)
|
|
|
|
}
|
|
|
|
return codec.ErrInvalidMessage
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|
|
|
|
|
2020-12-20 23:57:18 +03:00
|
|
|
func (c *protoCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
2020-11-03 01:22:02 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-20 23:57:18 +03:00
|
|
|
func (c *protoCodec) ReadBody(conn io.Reader, b interface{}) error {
|
2020-11-24 16:25:09 +03:00
|
|
|
switch m := b.(type) {
|
2020-11-29 21:06:14 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2020-11-24 16:25:09 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-12 19:14:45 +03:00
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return nil
|
2020-11-24 16:25:09 +03:00
|
|
|
}
|
|
|
|
m.Data = buf
|
|
|
|
return nil
|
|
|
|
case proto.Message:
|
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-12 19:14:45 +03:00
|
|
|
} else if len(buf) == 0 {
|
2021-04-12 16:42:06 +03:00
|
|
|
return nil
|
2020-11-24 16:25:09 +03:00
|
|
|
}
|
|
|
|
return proto.Unmarshal(buf, m)
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|
2020-11-24 16:25:09 +03:00
|
|
|
return codec.ErrInvalidMessage
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|
|
|
|
|
2020-12-20 23:57:18 +03:00
|
|
|
func (c *protoCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
2020-11-24 16:25:09 +03:00
|
|
|
switch m := b.(type) {
|
2020-11-29 21:06:14 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2020-11-24 16:25:09 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
_, err := conn.Write(m.Data)
|
|
|
|
return err
|
|
|
|
case proto.Message:
|
|
|
|
buf, err := proto.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = conn.Write(buf)
|
2020-11-03 01:22:02 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-11-24 16:25:09 +03:00
|
|
|
return codec.ErrInvalidMessage
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|
|
|
|
|
2020-11-24 16:25:09 +03:00
|
|
|
func (c *protoCodec) String() string {
|
2020-11-03 01:22:02 +03:00
|
|
|
return "proto"
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:25:09 +03:00
|
|
|
func NewCodec() codec.Codec {
|
|
|
|
return &protoCodec{}
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|