micro-codec-jsonpb/jsonpb.go

110 lines
2.1 KiB
Go
Raw Normal View History

// 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 (
JsonpbMarshaler = jsonpb.MarshalOptions{
UseEnumNumbers: false,
EmitUnpopulated: false,
UseProtoNames: true,
AllowPartial: false,
}
JsonpbUnmarshaler = jsonpb.UnmarshalOptions{
DiscardUnknown: false,
AllowPartial: false,
}
)
type jsonpbCodec struct{}
func (c *jsonpbCodec) Marshal(v interface{}) ([]byte, error) {
switch m := v.(type) {
case nil:
return nil, nil
case *codec.Frame:
return m.Data, nil
case proto.Message:
return JsonpbMarshaler.Marshal(m)
}
return nil, codec.ErrInvalidMessage
}
func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}) error {
if len(d) == 0 {
return nil
}
switch m := v.(type) {
case nil:
return nil
case *codec.Frame:
m.Data = d
return nil
case proto.Message:
return JsonpbUnmarshaler.Unmarshal(d, m)
}
return codec.ErrInvalidMessage
}
func (c *jsonpbCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
return nil
}
func (c *jsonpbCodec) ReadBody(conn io.Reader, b interface{}) error {
switch m := b.(type) {
case nil:
return nil
case *codec.Frame:
buf, err := ioutil.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
m.Data = buf
return nil
case proto.Message:
buf, err := ioutil.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
return JsonpbUnmarshaler.Unmarshal(buf, m)
}
return codec.ErrInvalidMessage
}
func (c *jsonpbCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
switch m := b.(type) {
case nil:
return nil
case *codec.Frame:
_, err := conn.Write(m.Data)
return err
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{}
}