2020-11-23 19:17:52 +03:00
|
|
|
// Package jsonpb provides a json codec
|
|
|
|
package jsonpb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2020-11-29 21:04:41 +03:00
|
|
|
// nolint: staticcheck
|
2020-11-23 19:17:52 +03:00
|
|
|
oldjsonpb "github.com/golang/protobuf/jsonpb"
|
2020-11-29 21:04:41 +03:00
|
|
|
// nolint: staticcheck
|
2020-11-23 19:17:52 +03:00
|
|
|
oldproto "github.com/golang/protobuf/proto"
|
2021-03-07 19:20:40 +03:00
|
|
|
mproto "github.com/unistack-org/micro-proto/proto"
|
2020-11-23 19:17:52 +03:00
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-11-24 15:21:36 +03:00
|
|
|
JsonpbMarshaler = jsonpb.MarshalOptions{
|
|
|
|
UseEnumNumbers: false,
|
|
|
|
EmitUnpopulated: false,
|
|
|
|
UseProtoNames: true,
|
|
|
|
AllowPartial: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonpbUnmarshaler = jsonpb.UnmarshalOptions{
|
|
|
|
DiscardUnknown: false,
|
|
|
|
AllowPartial: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
OldJsonpbMarshaler = oldjsonpb.Marshaler{
|
|
|
|
OrigName: true,
|
|
|
|
EmitDefaults: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
OldJsonpbUnmarshaler = oldjsonpb.Unmarshaler{
|
|
|
|
AllowUnknownFields: false,
|
|
|
|
}
|
2020-11-23 19:17:52 +03:00
|
|
|
)
|
|
|
|
|
2020-11-24 16:30:31 +03:00
|
|
|
type jsonpbCodec struct{}
|
|
|
|
|
|
|
|
func (c *jsonpbCodec) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
switch m := v.(type) {
|
2020-11-29 21:04:41 +03:00
|
|
|
case nil:
|
|
|
|
return nil, nil
|
2021-03-07 19:20:40 +03:00
|
|
|
case *mproto.Frame:
|
|
|
|
return m.Data, nil
|
2020-11-24 16:30:31 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case proto.Message:
|
|
|
|
return JsonpbMarshaler.Marshal(m)
|
|
|
|
case oldproto.Message:
|
|
|
|
buf, err := OldJsonpbMarshaler.MarshalToString(m)
|
|
|
|
return []byte(buf), err
|
|
|
|
}
|
|
|
|
return nil, codec.ErrInvalidMessage
|
2020-11-23 19:17:52 +03:00
|
|
|
}
|
|
|
|
|
2020-11-24 16:30:31 +03:00
|
|
|
func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}) error {
|
2020-11-29 21:04:41 +03:00
|
|
|
if d == nil {
|
2020-11-24 16:30:31 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch m := v.(type) {
|
2020-11-29 21:04:41 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2021-03-07 19:20:40 +03:00
|
|
|
case *mproto.Frame:
|
|
|
|
m.Data = d
|
|
|
|
return nil
|
2020-11-24 16:30:31 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = d
|
|
|
|
return nil
|
|
|
|
case proto.Message:
|
|
|
|
return JsonpbUnmarshaler.Unmarshal(d, m)
|
|
|
|
case oldproto.Message:
|
|
|
|
return OldJsonpbUnmarshaler.Unmarshal(bytes.NewReader(d), m)
|
|
|
|
}
|
|
|
|
return codec.ErrInvalidMessage
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-20 23:56:44 +03:00
|
|
|
func (c *jsonpbCodec) ReadBody(conn io.Reader, b interface{}) error {
|
2020-11-23 19:17:52 +03:00
|
|
|
switch m := b.(type) {
|
2020-11-29 21:04:41 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2021-03-07 19:20:40 +03:00
|
|
|
case *mproto.Frame:
|
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Data = buf
|
|
|
|
return nil
|
2020-11-24 16:30:31 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Data = buf
|
|
|
|
return nil
|
2020-11-23 19:17:52 +03:00
|
|
|
case oldproto.Message:
|
|
|
|
return OldJsonpbUnmarshaler.Unmarshal(conn, m)
|
|
|
|
case proto.Message:
|
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return JsonpbUnmarshaler.Unmarshal(buf, m)
|
|
|
|
}
|
|
|
|
return codec.ErrInvalidMessage
|
|
|
|
}
|
|
|
|
|
2020-12-20 23:56:44 +03:00
|
|
|
func (c *jsonpbCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
2020-11-23 19:17:52 +03:00
|
|
|
switch m := b.(type) {
|
2020-11-29 21:04:41 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2021-03-07 19:20:40 +03:00
|
|
|
case *mproto.Frame:
|
|
|
|
_, err := conn.Write(m.Data)
|
|
|
|
return err
|
2020-11-24 16:30:31 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
_, err := conn.Write(m.Data)
|
|
|
|
return err
|
2020-11-23 19:17:52 +03:00
|
|
|
case oldproto.Message:
|
|
|
|
return OldJsonpbMarshaler.Marshal(conn, m)
|
|
|
|
case proto.Message:
|
|
|
|
buf, err := JsonpbMarshaler.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = conn.Write(buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return codec.ErrInvalidMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *jsonpbCodec) String() string {
|
|
|
|
return "jsonpb"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCodec() codec.Codec {
|
|
|
|
return &jsonpbCodec{}
|
|
|
|
}
|