2020-11-23 19:17:52 +03:00
|
|
|
// Package jsonpb provides a json codec
|
2021-10-25 20:36:58 +03:00
|
|
|
package jsonpb // import "go.unistack.org/micro-codec-jsonpb/v3"
|
2020-11-23 19:17:52 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
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
|
|
|
|
2021-05-25 23:12:33 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
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
|
2021-09-24 00:10:13 +03:00
|
|
|
}
|
2021-09-22 02:17:04 +03:00
|
|
|
|
2021-09-24 00:10:13 +03:00
|
|
|
if _, ok := v.(proto.Message); !ok {
|
|
|
|
return nil, codec.ErrInvalidMessage
|
|
|
|
}
|
2021-09-22 02:19:49 +03:00
|
|
|
|
2021-09-24 00:10:13 +03:00
|
|
|
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
|
|
|
}
|
2020-11-24 16:30:31 +03:00
|
|
|
}
|
2021-09-24 00:10:13 +03:00
|
|
|
|
|
|
|
return marshalOptions.Marshal(v.(proto.Message))
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
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
|
2021-09-24 00:10:13 +03:00
|
|
|
}
|
2021-09-22 02:17:04 +03:00
|
|
|
|
2021-09-24 00:10:13 +03:00
|
|
|
if _, ok := v.(proto.Message); !ok {
|
|
|
|
return codec.ErrInvalidMessage
|
|
|
|
}
|
2021-09-22 02:19:49 +03:00
|
|
|
|
2021-09-24 00:10:13 +03:00
|
|
|
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
|
|
|
}
|
2020-11-24 16:30:31 +03:00
|
|
|
}
|
2021-09-24 00:10:13 +03:00
|
|
|
|
|
|
|
return unmarshalOptions.Unmarshal(d, v.(proto.Message))
|
2020-11-24 16:30:31 +03:00
|
|
|
}
|
2021-05-26 00:32:11 +03:00
|
|
|
|
2020-12-20 23:56:44 +03:00
|
|
|
func (c *jsonpbCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
2020-11-23 19:17:52 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 23:12:33 +03:00
|
|
|
func (c *jsonpbCodec) ReadBody(conn io.Reader, v interface{}) error {
|
2021-05-26 00:46:17 +03:00
|
|
|
if v == nil {
|
2020-11-29 21:04:41 +03:00
|
|
|
return nil
|
2021-05-26 00:46:17 +03:00
|
|
|
}
|
|
|
|
buf, err := io.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(buf) == 0 {
|
2020-11-24 16:30:31 +03:00
|
|
|
return nil
|
2020-11-23 19:17:52 +03:00
|
|
|
}
|
2021-05-26 00:46:17 +03:00
|
|
|
return c.Unmarshal(buf, v)
|
2020-11-23 19:17:52 +03:00
|
|
|
}
|
|
|
|
|
2021-05-25 23:12:33 +03:00
|
|
|
func (c *jsonpbCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
2021-05-26 00:46:17 +03:00
|
|
|
if v == nil {
|
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
|
|
|
|
2021-05-26 00:46:17 +03:00
|
|
|
buf, err := c.Marshal(v)
|
|
|
|
if err != nil {
|
2020-11-23 19:17:52 +03:00
|
|
|
return err
|
2021-05-26 00:46:17 +03:00
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return codec.ErrInvalidMessage
|
2020-11-23 19:17:52 +03:00
|
|
|
}
|
2021-05-26 00:46:17 +03:00
|
|
|
_, err = conn.Write(buf)
|
|
|
|
return err
|
2020-11-23 19:17:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *jsonpbCodec) String() string {
|
|
|
|
return "jsonpb"
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:17:04 +03:00
|
|
|
func NewCodec(opts ...codec.Option) codec.Codec {
|
|
|
|
return &jsonpbCodec{opts: codec.NewOptions(opts...)}
|
2020-11-23 19:17:52 +03:00
|
|
|
}
|