2020-11-27 17:43:13 +03:00
|
|
|
// Package proto provides a proto codec
|
2021-10-25 20:39:17 +03:00
|
|
|
package proto // import "go.unistack.org/micro-codec-segmentio/v3/proto"
|
2020-11-27 17:43:13 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/segmentio/encoding/proto"
|
2023-03-05 22:25:51 +03:00
|
|
|
pb "go.unistack.org/micro-proto/v3/codec"
|
2021-10-25 20:24:40 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
rutil "go.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-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)
|
|
|
|
}
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
if options.Flatten {
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2021-09-24 00:45:52 +03:00
|
|
|
}
|
|
|
|
|
2023-03-05 22:25:51 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case *pb.Frame:
|
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)
|
2024-09-17 12:28:35 +03:00
|
|
|
default:
|
|
|
|
return nil, codec.ErrInvalidMessage
|
2021-09-24 00:45:52 +03:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
if options.Flatten {
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2021-09-24 00:45:52 +03:00
|
|
|
}
|
|
|
|
|
2023-03-05 22:25:51 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = d
|
|
|
|
return nil
|
|
|
|
case *pb.Frame:
|
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)
|
2024-09-17 12:28:35 +03:00
|
|
|
default:
|
2021-05-26 00:58:47 +03:00
|
|
|
return codec.ErrInvalidMessage
|
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
|
|
|
}
|