2020-11-23 19:17:52 +03:00
|
|
|
// Package jsonpb provides a json codec
|
|
|
|
package jsonpb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"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,
|
|
|
|
}
|
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
|
2020-11-24 16:30:31 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case proto.Message:
|
|
|
|
return JsonpbMarshaler.Marshal(m)
|
|
|
|
}
|
|
|
|
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 {
|
2021-04-12 19:13:29 +03:00
|
|
|
if len(d) == 0 {
|
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
|
2020-11-24 16:30:31 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = d
|
|
|
|
return nil
|
|
|
|
case proto.Message:
|
|
|
|
return JsonpbUnmarshaler.Unmarshal(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
|
2020-11-24 16:30:31 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-12 19:13:29 +03:00
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return nil
|
2020-11-24 16:30:31 +03:00
|
|
|
}
|
|
|
|
m.Data = buf
|
|
|
|
return nil
|
2020-11-23 19:17:52 +03:00
|
|
|
case proto.Message:
|
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-12 19:13:29 +03:00
|
|
|
} else if len(buf) == 0 {
|
2021-04-12 16:40:45 +03:00
|
|
|
return nil
|
2020-11-23 19:17:52 +03:00
|
|
|
}
|
|
|
|
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
|
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 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{}
|
|
|
|
}
|