micro-server-grpc/codec.go

234 lines
4.6 KiB
Go
Raw Permalink Normal View History

2019-06-03 20:44:43 +03:00
package grpc
import (
b "bytes"
2019-06-03 20:44:43 +03:00
"encoding/json"
2019-06-18 20:51:52 +03:00
"strings"
2019-06-03 20:44:43 +03:00
oldjsonpb "github.com/golang/protobuf/jsonpb"
oldproto "github.com/golang/protobuf/proto"
bytes "github.com/unistack-org/micro-codec-bytes"
"github.com/unistack-org/micro/v3/codec"
2019-06-18 20:51:52 +03:00
"google.golang.org/grpc"
2019-06-03 20:44:43 +03:00
"google.golang.org/grpc/encoding"
2019-06-18 20:51:52 +03:00
"google.golang.org/grpc/metadata"
jsonpb "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
2019-06-03 20:44:43 +03:00
)
type jsonCodec struct{}
type bytesCodec struct{}
type protoCodec struct{}
2019-06-18 20:51:52 +03:00
type wrapCodec struct{ encoding.Codec }
2019-06-03 20:44:43 +03:00
var (
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,
}
)
2019-06-03 20:44:43 +03:00
var (
defaultGRPCCodecs = map[string]encoding.Codec{
"application/json": jsonCodec{},
"application/proto": protoCodec{},
"application/protobuf": protoCodec{},
"application/octet-stream": protoCodec{},
"application/grpc": protoCodec{},
"application/grpc+json": jsonCodec{},
"application/grpc+proto": protoCodec{},
"application/grpc+bytes": bytesCodec{},
}
)
2019-06-17 22:05:58 +03:00
func (w wrapCodec) String() string {
2019-06-18 20:51:52 +03:00
return w.Codec.Name()
2019-06-17 22:05:58 +03:00
}
func (w wrapCodec) Marshal(v interface{}) ([]byte, error) {
switch m := v.(type) {
case *bytes.Frame:
return m.Data, nil
2019-06-18 20:51:52 +03:00
}
return w.Codec.Marshal(v)
2019-06-17 22:05:58 +03:00
}
func (w wrapCodec) Unmarshal(data []byte, v interface{}) error {
if len(data) == 0 {
2019-06-18 20:51:52 +03:00
return nil
}
2020-01-02 00:56:29 +03:00
if v == nil {
return nil
}
switch m := v.(type) {
case *bytes.Frame:
m.Data = data
return nil
}
2019-06-18 20:51:52 +03:00
return w.Codec.Unmarshal(data, v)
2019-06-17 22:05:58 +03:00
}
2019-06-03 20:44:43 +03:00
func (protoCodec) Marshal(v interface{}) ([]byte, error) {
switch m := v.(type) {
case proto.Message:
return proto.Marshal(m)
case oldproto.Message:
return oldproto.Marshal(m)
}
return nil, codec.ErrInvalidMessage
2019-06-03 20:44:43 +03:00
}
func (protoCodec) Unmarshal(data []byte, v interface{}) error {
if len(data) == 0 {
return nil
}
if v == nil {
return nil
}
switch m := v.(type) {
case proto.Message:
return proto.Unmarshal(data, m)
case oldproto.Message:
return oldproto.Unmarshal(data, m)
}
return codec.ErrInvalidMessage
2019-06-03 20:44:43 +03:00
}
func (protoCodec) Name() string {
return "proto"
}
func (jsonCodec) Marshal(v interface{}) ([]byte, error) {
switch m := v.(type) {
case proto.Message:
return jsonpbMarshaler.Marshal(m)
case oldproto.Message:
buf := b.NewBuffer(nil)
err := oldjsonpbMarshaler.Marshal(buf, m)
return buf.Bytes(), err
}
2019-06-03 20:44:43 +03:00
return json.Marshal(v)
}
func (jsonCodec) Unmarshal(data []byte, v interface{}) error {
if len(data) == 0 {
return nil
}
if v == nil {
return nil
}
switch m := v.(type) {
case proto.Message:
return jsonpbUnmarshaler.Unmarshal(data, m)
case oldproto.Message:
return oldjsonpbUnmarshaler.Unmarshal(b.NewReader(data), m)
}
2019-06-03 20:44:43 +03:00
return json.Unmarshal(data, v)
}
func (jsonCodec) Name() string {
return "json"
}
func (bytesCodec) Marshal(v interface{}) ([]byte, error) {
switch m := v.(type) {
case *[]byte:
return *m, nil
2019-06-03 20:44:43 +03:00
}
return nil, codec.ErrInvalidMessage
2019-06-03 20:44:43 +03:00
}
func (bytesCodec) Unmarshal(data []byte, v interface{}) error {
if len(data) == 0 {
return nil
2019-06-03 20:44:43 +03:00
}
if v == nil {
return nil
}
switch m := v.(type) {
case *[]byte:
*m = data
return nil
}
return codec.ErrInvalidMessage
2019-06-03 20:44:43 +03:00
}
func (bytesCodec) Name() string {
return "bytes"
}
2019-06-18 20:51:52 +03:00
type grpcCodec struct {
grpc.ServerStream
2019-06-18 20:51:52 +03:00
// headers
id string
target string
method string
endpoint string
c encoding.Codec
}
func (g *grpcCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
md, _ := metadata.FromIncomingContext(g.ServerStream.Context())
2019-06-18 20:51:52 +03:00
if m == nil {
m = new(codec.Message)
}
if m.Header == nil {
m.Header = make(map[string]string, len(md))
2019-06-18 20:51:52 +03:00
}
for k, v := range md {
m.Header[k] = strings.Join(v, ",")
}
m.Id = g.id
m.Target = g.target
m.Method = g.method
m.Endpoint = g.endpoint
return nil
}
func (g *grpcCodec) ReadBody(v interface{}) error {
// caller has requested a frame
switch m := v.(type) {
case *bytes.Frame:
return g.ServerStream.RecvMsg(m)
2019-06-18 20:51:52 +03:00
}
return g.ServerStream.RecvMsg(v)
2019-06-18 20:51:52 +03:00
}
func (g *grpcCodec) Write(m *codec.Message, v interface{}) error {
// if we don't have a body
if v != nil {
b, err := g.c.Marshal(v)
if err != nil {
return err
}
m.Body = b
}
// write the body using the framing codec
return g.ServerStream.SendMsg(&bytes.Frame{Data: m.Body})
2019-06-18 20:51:52 +03:00
}
func (g *grpcCodec) Close() error {
return nil
}
func (g *grpcCodec) String() string {
2020-01-02 21:23:43 +03:00
return "grpc"
2019-06-18 20:51:52 +03:00
}