2020-11-27 17:43:13 +03:00
|
|
|
// Package proto provides a proto codec
|
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/segmentio/encoding/proto"
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2021-05-26 00:58:47 +03:00
|
|
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
2020-11-27 17:43:13 +03:00
|
|
|
newproto "google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
type protoCodec struct {
|
|
|
|
opts codec.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ codec.Codec = &protoCodec{}
|
2020-11-27 17:43:13 +03:00
|
|
|
|
2021-05-26 00:58:47 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
func (c *protoCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
|
|
|
if v == nil {
|
2020-11-29 21:08:49 +03:00
|
|
|
return nil, nil
|
2021-09-24 00:45:52 +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-27 17:43:13 +03:00
|
|
|
return m.Data, nil
|
|
|
|
}
|
2021-09-24 00:45:52 +03:00
|
|
|
|
2021-09-27 15:50:02 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case proto.Message:
|
|
|
|
return proto.Marshal(m)
|
|
|
|
case newproto.Message:
|
|
|
|
return proto.Marshal(m)
|
2021-09-24 00:45:52 +03:00
|
|
|
}
|
|
|
|
|
2021-09-27 15:50:02 +03:00
|
|
|
return nil, codec.ErrInvalidMessage
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
func (c *protoCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
|
|
|
if v == nil || len(d) == 0 {
|
2020-11-27 17:43:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-24 00:45:52 +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-27 17:43:13 +03:00
|
|
|
m.Data = d
|
2021-05-26 00:58:47 +03:00
|
|
|
return nil
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
2021-09-24 00:45:52 +03:00
|
|
|
|
2021-09-27 15:50:02 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case proto.Message:
|
|
|
|
return proto.Unmarshal(d, m)
|
|
|
|
case newproto.Message:
|
|
|
|
return proto.Unmarshal(d, m)
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
}
|
|
|
|
|
2021-09-27 15:50:02 +03:00
|
|
|
return codec.ErrInvalidMessage
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:58:47 +03:00
|
|
|
func (c *protoCodec) ReadBody(conn io.Reader, v interface{}) error {
|
|
|
|
if v == nil {
|
2020-11-29 21:08:49 +03:00
|
|
|
return nil
|
2021-05-26 00:58:47 +03:00
|
|
|
}
|
|
|
|
buf, err := io.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(buf) == 0 {
|
2020-11-27 17:43:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 00:58:47 +03:00
|
|
|
return c.Unmarshal(buf, v)
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:58:47 +03:00
|
|
|
func (c *protoCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
|
|
|
if v == nil {
|
2020-11-29 21:08:49 +03:00
|
|
|
return nil
|
2021-05-26 00:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := c.Marshal(v)
|
|
|
|
if err != nil {
|
2020-11-27 17:43:13 +03:00
|
|
|
return err
|
2021-05-26 00:58:47 +03:00
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return codec.ErrInvalidMessage
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
2021-05-26 00:58:47 +03:00
|
|
|
|
|
|
|
_, err = conn.Write(buf)
|
|
|
|
return err
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *protoCodec) String() string {
|
|
|
|
return "proto"
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
func NewCodec(opts ...codec.Option) *protoCodec {
|
|
|
|
return &protoCodec{opts: codec.NewOptions(opts...)}
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|