2020-11-03 01:22:02 +03:00
|
|
|
// Package proto provides a proto codec
|
2024-09-16 23:44:26 +03:00
|
|
|
package proto
|
2020-11-03 01:22:02 +03:00
|
|
|
|
|
|
|
import (
|
2023-03-05 22:20:00 +03:00
|
|
|
pb "go.unistack.org/micro-proto/v3/codec"
|
2021-10-25 20:22:23 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2020-11-03 01:22:02 +03:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
var (
|
|
|
|
DefaultMarshalOptions = proto.MarshalOptions{
|
|
|
|
AllowPartial: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultUnmarshalOptions = proto.UnmarshalOptions{
|
|
|
|
DiscardUnknown: false,
|
|
|
|
AllowPartial: false,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-09-22 02:22:22 +03:00
|
|
|
type protoCodec struct {
|
|
|
|
opts codec.Options
|
|
|
|
}
|
2020-11-24 16:25:09 +03:00
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
type protoCodecV2 struct {
|
|
|
|
opts codec.Options
|
|
|
|
}
|
2021-09-24 00:24:58 +03:00
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
var (
|
|
|
|
_ codec.Codec = (*protoCodec)(nil)
|
|
|
|
_ codec.CodecV2 = (*protoCodecV2)(nil)
|
2021-05-25 23:18:06 +03:00
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
if options.Flatten {
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2021-09-24 00:24:58 +03:00
|
|
|
}
|
|
|
|
|
2023-03-05 22:20:00 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case *pb.Frame:
|
2020-11-24 16:25:09 +03:00
|
|
|
return m.Data, nil
|
2024-09-16 23:44:26 +03:00
|
|
|
case proto.Message:
|
|
|
|
marshalOptions := DefaultMarshalOptions
|
|
|
|
if options.Context != nil {
|
|
|
|
if f, ok := options.Context.Value(marshalOptionsKey{}).(proto.MarshalOptions); ok {
|
|
|
|
marshalOptions = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return marshalOptions.Marshal(m)
|
|
|
|
default:
|
2021-09-24 00:24:58 +03:00
|
|
|
return nil, codec.ErrInvalidMessage
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
if options.Flatten {
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2021-09-24 00:24:58 +03:00
|
|
|
}
|
|
|
|
|
2023-03-05 22:20:00 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = d
|
|
|
|
return nil
|
|
|
|
case *pb.Frame:
|
2020-11-24 16:25:09 +03:00
|
|
|
m.Data = d
|
2021-09-24 00:24:58 +03:00
|
|
|
return nil
|
2024-09-16 23:44:26 +03:00
|
|
|
case proto.Message:
|
|
|
|
unmarshalOptions := DefaultUnmarshalOptions
|
|
|
|
if options.Context != nil {
|
|
|
|
if f, ok := options.Context.Value(marshalOptionsKey{}).(proto.UnmarshalOptions); ok {
|
|
|
|
unmarshalOptions = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unmarshalOptions.Unmarshal(d, m)
|
|
|
|
default:
|
2021-09-24 00:24:58 +03:00
|
|
|
return codec.ErrInvalidMessage
|
|
|
|
}
|
2024-09-16 23:44:26 +03:00
|
|
|
}
|
2021-09-24 00:24:58 +03:00
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
func (c *protoCodec) String() string {
|
|
|
|
return "proto"
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
func NewCodec(opts ...codec.Option) codec.Codec {
|
|
|
|
return &protoCodec{opts: codec.NewOptions(opts...)}
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
func (c *protoCodecV2) Marshal(d []byte, v interface{}, opts ...codec.Option) ([]byte, error) {
|
2021-05-26 00:47:41 +03:00
|
|
|
if v == nil {
|
2024-09-16 23:44:26 +03:00
|
|
|
return nil, nil
|
2021-05-26 00:47:41 +03:00
|
|
|
}
|
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.Flatten {
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case *pb.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case proto.Message:
|
|
|
|
marshalOptions := DefaultMarshalOptions
|
|
|
|
if options.Context != nil {
|
|
|
|
if f, ok := options.Context.Value(marshalOptionsKey{}).(proto.MarshalOptions); ok {
|
|
|
|
marshalOptions = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return marshalOptions.MarshalAppend(d, m)
|
|
|
|
default:
|
|
|
|
return nil, codec.ErrInvalidMessage
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
func (c *protoCodecV2) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
|
|
|
if v == nil || len(d) == 0 {
|
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
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.Flatten {
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = d
|
|
|
|
return nil
|
|
|
|
case *pb.Frame:
|
|
|
|
m.Data = d
|
|
|
|
return nil
|
|
|
|
case proto.Message:
|
|
|
|
unmarshalOptions := DefaultUnmarshalOptions
|
|
|
|
if options.Context != nil {
|
|
|
|
if f, ok := options.Context.Value(marshalOptionsKey{}).(proto.UnmarshalOptions); ok {
|
|
|
|
unmarshalOptions = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unmarshalOptions.Unmarshal(d, m)
|
|
|
|
default:
|
2021-05-26 00:47:41 +03:00
|
|
|
return codec.ErrInvalidMessage
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
func (c *protoCodecV2) String() string {
|
2020-11-03 01:22:02 +03:00
|
|
|
return "proto"
|
|
|
|
}
|
|
|
|
|
2024-09-16 23:44:26 +03:00
|
|
|
func NewCodecV2(opts ...codec.Option) codec.CodecV2 {
|
|
|
|
return &protoCodecV2{opts: codec.NewOptions(opts...)}
|
2020-11-03 01:22:02 +03:00
|
|
|
}
|