2020-11-03 01:22:02 +03:00
|
|
|
// Package proto provides a proto codec
|
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2021-05-25 23:18:06 +03:00
|
|
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
2020-11-03 01:22:02 +03:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
2020-11-24 16:25:09 +03:00
|
|
|
type protoCodec struct{}
|
|
|
|
|
2021-05-25 23:18:06 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
2020-11-24 16:25:09 +03:00
|
|
|
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:
|
2021-05-25 23:18:06 +03:00
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
|
|
|
if nm, ok := nv.(proto.Message); ok {
|
|
|
|
return proto.Marshal(nm)
|
|
|
|
}
|
|
|
|
}
|
2020-11-24 16:25:09 +03:00
|
|
|
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:
|
2021-05-25 23:18:06 +03:00
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
|
|
|
if nm, ok := nv.(proto.Message); ok {
|
|
|
|
return proto.Unmarshal(d, nm)
|
|
|
|
}
|
|
|
|
}
|
2020-11-24 16:25:09 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-25 23:18:06 +03:00
|
|
|
func (c *protoCodec) ReadBody(conn io.Reader, v interface{}) error {
|
|
|
|
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:
|
2021-05-25 23:18:06 +03:00
|
|
|
buf, err := io.ReadAll(conn)
|
2020-11-24 16:25:09 +03:00
|
|
|
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:
|
2021-05-25 23:18:06 +03:00
|
|
|
buf, err := io.ReadAll(conn)
|
2020-11-24 16:25:09 +03:00
|
|
|
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
|
|
|
}
|
2021-05-25 23:18:06 +03:00
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
|
|
|
if nm, ok := nv.(proto.Message); ok {
|
|
|
|
return proto.Unmarshal(buf, nm)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-05-25 23:18:06 +03:00
|
|
|
func (c *protoCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
|
|
|
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:
|
|
|
|
_, err := conn.Write(m.Data)
|
|
|
|
return err
|
|
|
|
case proto.Message:
|
2021-05-25 23:18:06 +03:00
|
|
|
var buf []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
|
|
|
if nm, ok := nv.(proto.Message); ok {
|
|
|
|
buf, err = proto.Marshal(nm)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf, err = proto.Marshal(m)
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:25:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-05-25 23:18:06 +03:00
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return codec.ErrInvalidMessage
|
2020-11-24 16:25:09 +03:00
|
|
|
}
|
|
|
|
_, 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
|
|
|
}
|