2024-09-16 23:59:08 +03:00
|
|
|
// Package jsonpb provides a json protobuf codec
|
|
|
|
package jsonpb
|
2020-11-23 19:17:52 +03:00
|
|
|
|
|
|
|
import (
|
2023-03-05 22:02:25 +03:00
|
|
|
pb "go.unistack.org/micro-proto/v3/codec"
|
2021-10-25 20:21:10 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2020-11-23 19:17:52 +03:00
|
|
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-09-22 02:17:04 +03:00
|
|
|
DefaultMarshalOptions = jsonpb.MarshalOptions{
|
2020-11-24 15:21:36 +03:00
|
|
|
UseEnumNumbers: false,
|
|
|
|
EmitUnpopulated: false,
|
2023-12-25 16:16:57 +03:00
|
|
|
UseProtoNames: false,
|
2020-11-24 15:21:36 +03:00
|
|
|
AllowPartial: false,
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:17:04 +03:00
|
|
|
DefaultUnmarshalOptions = jsonpb.UnmarshalOptions{
|
2020-11-24 15:21:36 +03:00
|
|
|
DiscardUnknown: false,
|
|
|
|
AllowPartial: false,
|
|
|
|
}
|
2020-11-23 19:17:52 +03:00
|
|
|
)
|
|
|
|
|
2021-09-22 02:17:04 +03:00
|
|
|
type jsonpbCodec struct {
|
|
|
|
opts codec.Options
|
|
|
|
}
|
2020-11-24 16:30:31 +03:00
|
|
|
|
2024-09-16 23:59:08 +03:00
|
|
|
type jsonpbCodecV2 struct {
|
|
|
|
opts codec.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ codec.Codec = (*jsonpbCodec)(nil)
|
|
|
|
_ codec.CodecV2 = (*jsonpbCodecV2)(nil)
|
2021-05-25 23:12:33 +03:00
|
|
|
)
|
|
|
|
|
2021-09-22 02:17:04 +03:00
|
|
|
func (c *jsonpbCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
2021-09-24 00:10:13 +03:00
|
|
|
if v == nil {
|
2020-11-29 21:04:41 +03:00
|
|
|
return nil, nil
|
2021-09-24 00:10:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2024-09-16 23:59:08 +03:00
|
|
|
if options.Flatten {
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2021-09-24 00:10:13 +03:00
|
|
|
}
|
|
|
|
|
2023-03-05 22:02:25 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case *pb.Frame:
|
2020-11-24 16:30:31 +03:00
|
|
|
return m.Data, nil
|
2024-09-16 23:59:08 +03:00
|
|
|
case proto.Message:
|
|
|
|
marshalOptions := DefaultMarshalOptions
|
|
|
|
if options.Context != nil {
|
|
|
|
if f, ok := options.Context.Value(marshalOptionsKey{}).(jsonpb.MarshalOptions); ok {
|
|
|
|
marshalOptions = f
|
|
|
|
}
|
2021-09-22 02:17:04 +03:00
|
|
|
}
|
2024-09-16 23:59:08 +03:00
|
|
|
return marshalOptions.Marshal(m)
|
|
|
|
default:
|
|
|
|
return nil, codec.ErrInvalidMessage
|
2020-11-24 16:30:31 +03:00
|
|
|
}
|
2020-11-23 19:17:52 +03:00
|
|
|
}
|
|
|
|
|
2021-09-22 02:17:04 +03:00
|
|
|
func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
2021-09-24 00:10:13 +03:00
|
|
|
if v == nil || len(d) == 0 {
|
2020-11-24 16:30:31 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-24 00:10:13 +03:00
|
|
|
|
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2024-09-16 23:59:08 +03:00
|
|
|
if options.Flatten {
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2021-09-24 00:10:13 +03:00
|
|
|
}
|
|
|
|
|
2023-03-05 22:02:25 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = d
|
|
|
|
return nil
|
|
|
|
case *pb.Frame:
|
2020-11-24 16:30:31 +03:00
|
|
|
m.Data = d
|
|
|
|
return nil
|
2024-09-16 23:59:08 +03:00
|
|
|
case proto.Message:
|
|
|
|
unmarshalOptions := DefaultUnmarshalOptions
|
|
|
|
if options.Context != nil {
|
|
|
|
if f, ok := options.Context.Value(unmarshalOptionsKey{}).(jsonpb.UnmarshalOptions); ok {
|
|
|
|
unmarshalOptions = f
|
|
|
|
}
|
2021-09-22 02:17:04 +03:00
|
|
|
}
|
2024-09-16 23:59:08 +03:00
|
|
|
return unmarshalOptions.Unmarshal(d, m)
|
|
|
|
default:
|
|
|
|
return codec.ErrInvalidMessage
|
2020-11-24 16:30:31 +03:00
|
|
|
}
|
2024-09-16 23:59:08 +03:00
|
|
|
}
|
2021-09-24 00:10:13 +03:00
|
|
|
|
2024-09-16 23:59:08 +03:00
|
|
|
func (c *jsonpbCodec) String() string {
|
|
|
|
return "jsonpb"
|
2020-11-24 16:30:31 +03:00
|
|
|
}
|
2021-05-26 00:32:11 +03:00
|
|
|
|
2024-09-16 23:59:08 +03:00
|
|
|
func NewCodec(opts ...codec.Option) codec.Codec {
|
|
|
|
return &jsonpbCodec{opts: codec.NewOptions(opts...)}
|
2020-11-23 19:17:52 +03:00
|
|
|
}
|
|
|
|
|
2024-09-16 23:59:08 +03:00
|
|
|
func (c *jsonpbCodecV2) Marshal(d []byte, v interface{}, opts ...codec.Option) ([]byte, error) {
|
2021-05-26 00:46:17 +03:00
|
|
|
if v == nil {
|
2024-09-16 23:59:08 +03:00
|
|
|
return nil, nil
|
2021-05-26 00:46:17 +03:00
|
|
|
}
|
2024-09-16 23:59:08 +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{}).(jsonpb.MarshalOptions); ok {
|
|
|
|
marshalOptions = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return marshalOptions.MarshalAppend(d, m)
|
|
|
|
default:
|
|
|
|
return nil, codec.ErrInvalidMessage
|
2020-11-23 19:17:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 23:59:08 +03:00
|
|
|
func (c *jsonpbCodecV2) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
|
|
|
if v == nil || len(d) == 0 {
|
2020-11-29 21:04:41 +03:00
|
|
|
return nil
|
2021-05-26 00:46:17 +03:00
|
|
|
}
|
2021-05-26 00:32:11 +03:00
|
|
|
|
2024-09-16 23:59:08 +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(unmarshalOptionsKey{}).(jsonpb.UnmarshalOptions); ok {
|
|
|
|
unmarshalOptions = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unmarshalOptions.Unmarshal(d, m)
|
|
|
|
default:
|
2021-05-26 00:46:17 +03:00
|
|
|
return codec.ErrInvalidMessage
|
2020-11-23 19:17:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 23:59:08 +03:00
|
|
|
func (c *jsonpbCodecV2) String() string {
|
2020-11-23 19:17:52 +03:00
|
|
|
return "jsonpb"
|
|
|
|
}
|
|
|
|
|
2024-09-16 23:59:08 +03:00
|
|
|
func NewCodecV2(opts ...codec.Option) codec.CodecV2 {
|
|
|
|
return &jsonpbCodecV2{opts: codec.NewOptions(opts...)}
|
2020-11-23 19:17:52 +03:00
|
|
|
}
|