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"
|
|
|
|
)
|
|
|
|
|
2021-09-22 02:22:22 +03:00
|
|
|
type protoCodec struct {
|
|
|
|
opts codec.Options
|
|
|
|
}
|
2020-11-24 16:25:09 +03:00
|
|
|
|
2021-09-24 00:24:58 +03:00
|
|
|
var _ codec.Codec = &protoCodec{}
|
|
|
|
|
2021-05-25 23:18:06 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
2021-09-22 02:22:22 +03:00
|
|
|
func (c *protoCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
2021-09-24 00:24:58 +03:00
|
|
|
if v == nil {
|
2020-11-29 21:06:14 +03:00
|
|
|
return nil, nil
|
2021-09-24 00:24:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
|
|
|
if m, ok := v.(*codec.Frame); ok {
|
2020-11-24 16:25:09 +03:00
|
|
|
return m.Data, nil
|
|
|
|
}
|
2021-09-24 00:24:58 +03:00
|
|
|
|
|
|
|
if _, ok := v.(proto.Message); !ok {
|
|
|
|
return nil, codec.ErrInvalidMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto.Marshal(v.(proto.Message))
|
2020-11-24 16:25:09 +03:00
|
|
|
}
|
|
|
|
|
2021-09-22 02:22:22 +03:00
|
|
|
func (c *protoCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
2021-09-24 00:24:58 +03:00
|
|
|
if v == nil || len(d) == 0 {
|
2020-11-24 16:25:09 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-24 00:24:58 +03:00
|
|
|
|
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
|
|
|
if m, ok := v.(*codec.Frame); ok {
|
2020-11-24 16:25:09 +03:00
|
|
|
m.Data = d
|
2021-09-24 00:24:58 +03:00
|
|
|
return nil
|
2020-11-24 16:25:09 +03:00
|
|
|
}
|
2021-09-24 00:24:58 +03:00
|
|
|
|
|
|
|
if _, ok := v.(proto.Message); !ok {
|
|
|
|
return codec.ErrInvalidMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto.Unmarshal(d, v.(proto.Message))
|
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 {
|
2021-05-26 00:47:41 +03:00
|
|
|
if v == nil {
|
2020-11-29 21:06:14 +03:00
|
|
|
return nil
|
2021-05-26 00:47:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := io.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(buf) == 0 {
|
2020-11-24 16:25:09 +03:00
|
|
|
return nil
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|
2021-05-26 00:47:41 +03:00
|
|
|
return c.Unmarshal(buf, v)
|
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 {
|
2021-05-26 00:47:41 +03:00
|
|
|
if v == nil {
|
2020-11-29 21:06:14 +03:00
|
|
|
return nil
|
2021-05-26 00:47:41 +03:00
|
|
|
}
|
2021-05-25 23:18:06 +03:00
|
|
|
|
2021-05-26 00:47:41 +03:00
|
|
|
buf, err := c.Marshal(v)
|
|
|
|
if err != nil {
|
2020-11-03 01:22:02 +03:00
|
|
|
return err
|
2021-05-26 00:47:41 +03:00
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return codec.ErrInvalidMessage
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|
2021-05-26 00:47:41 +03:00
|
|
|
_, err = conn.Write(buf)
|
|
|
|
return err
|
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"
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:22:22 +03:00
|
|
|
func NewCodec(opts ...codec.Option) codec.Codec {
|
|
|
|
return &protoCodec{opts: codec.NewOptions(opts...)}
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|