2019-06-03 18:44:43 +01:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-06-18 18:51:52 +01:00
|
|
|
"strings"
|
2019-06-03 18:44:43 +01:00
|
|
|
|
2019-07-04 14:06:29 +03:00
|
|
|
b "bytes"
|
|
|
|
|
2019-07-04 16:43:36 +08:00
|
|
|
"github.com/golang/protobuf/jsonpb"
|
2019-06-03 18:44:43 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/codec"
|
|
|
|
"github.com/micro/go-micro/v2/codec/bytes"
|
2019-06-18 18:51:52 +01:00
|
|
|
"google.golang.org/grpc"
|
2019-06-03 18:44:43 +01:00
|
|
|
"google.golang.org/grpc/encoding"
|
2019-06-18 18:51:52 +01:00
|
|
|
"google.golang.org/grpc/metadata"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type jsonCodec struct{}
|
|
|
|
type bytesCodec struct{}
|
|
|
|
type protoCodec struct{}
|
2019-06-18 18:51:52 +01:00
|
|
|
type wrapCodec struct{ encoding.Codec }
|
2019-06-03 18:44:43 +01:00
|
|
|
|
2020-03-30 11:04:59 +03:00
|
|
|
var jsonpbMarshaler = &jsonpb.Marshaler{
|
|
|
|
EnumsAsInts: false,
|
|
|
|
EmitDefaults: false,
|
|
|
|
OrigName: true,
|
|
|
|
}
|
2019-07-08 10:32:10 +08:00
|
|
|
|
2019-06-03 18:44:43 +01: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 20:05:58 +01:00
|
|
|
func (w wrapCodec) String() string {
|
2019-06-18 18:51:52 +01:00
|
|
|
return w.Codec.Name()
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w wrapCodec) Marshal(v interface{}) ([]byte, error) {
|
2019-06-18 18:51:52 +01:00
|
|
|
b, ok := v.(*bytes.Frame)
|
|
|
|
if ok {
|
|
|
|
return b.Data, nil
|
|
|
|
}
|
|
|
|
return w.Codec.Marshal(v)
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w wrapCodec) Unmarshal(data []byte, v interface{}) error {
|
2019-06-18 18:51:52 +01:00
|
|
|
b, ok := v.(*bytes.Frame)
|
|
|
|
if ok {
|
|
|
|
b.Data = data
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-01 21:56:29 +00:00
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-18 18:51:52 +01:00
|
|
|
return w.Codec.Unmarshal(data, v)
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
func (protoCodec) Marshal(v interface{}) ([]byte, error) {
|
2020-02-19 02:05:38 +03:00
|
|
|
m, ok := v.(proto.Message)
|
|
|
|
if !ok {
|
|
|
|
return nil, codec.ErrInvalidMessage
|
|
|
|
}
|
|
|
|
return proto.Marshal(m)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (protoCodec) Unmarshal(data []byte, v interface{}) error {
|
2020-02-19 02:05:38 +03:00
|
|
|
m, ok := v.(proto.Message)
|
|
|
|
if !ok {
|
|
|
|
return codec.ErrInvalidMessage
|
|
|
|
}
|
|
|
|
return proto.Unmarshal(data, m)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (protoCodec) Name() string {
|
|
|
|
return "proto"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
2019-07-08 10:32:10 +08:00
|
|
|
if pb, ok := v.(proto.Message); ok {
|
|
|
|
s, err := jsonpbMarshaler.MarshalToString(pb)
|
|
|
|
return []byte(s), err
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
return json.Marshal(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jsonCodec) Unmarshal(data []byte, v interface{}) error {
|
2020-01-07 18:37:34 +00:00
|
|
|
if len(data) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-04 16:43:36 +08:00
|
|
|
if pb, ok := v.(proto.Message); ok {
|
2019-07-04 14:06:29 +03:00
|
|
|
return jsonpb.Unmarshal(b.NewReader(data), pb)
|
2019-07-04 16:43:36 +08:00
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
return json.Unmarshal(data, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jsonCodec) Name() string {
|
|
|
|
return "json"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bytesCodec) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
b, ok := v.(*[]byte)
|
|
|
|
if !ok {
|
2020-02-19 02:05:38 +03:00
|
|
|
return nil, codec.ErrInvalidMessage
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
return *b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bytesCodec) Unmarshal(data []byte, v interface{}) error {
|
|
|
|
b, ok := v.(*[]byte)
|
|
|
|
if !ok {
|
2020-02-19 02:05:38 +03:00
|
|
|
return codec.ErrInvalidMessage
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
*b = data
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bytesCodec) Name() string {
|
|
|
|
return "bytes"
|
|
|
|
}
|
2019-06-18 18:51:52 +01:00
|
|
|
|
|
|
|
type grpcCodec struct {
|
|
|
|
// headers
|
|
|
|
id string
|
|
|
|
target string
|
|
|
|
method string
|
|
|
|
endpoint string
|
|
|
|
|
|
|
|
s grpc.ServerStream
|
|
|
|
c encoding.Codec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
|
|
|
|
md, _ := metadata.FromIncomingContext(g.s.Context())
|
|
|
|
if m == nil {
|
|
|
|
m = new(codec.Message)
|
|
|
|
}
|
|
|
|
if m.Header == nil {
|
2020-02-24 17:15:20 +03:00
|
|
|
m.Header = make(map[string]string, len(md))
|
2019-06-18 18:51:52 +01: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
|
|
|
|
if f, ok := v.(*bytes.Frame); ok {
|
|
|
|
return g.s.RecvMsg(f)
|
|
|
|
}
|
|
|
|
return g.s.RecvMsg(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-12-05 16:10:49 +00:00
|
|
|
return g.s.SendMsg(&bytes.Frame{Data: m.Body})
|
2019-06-18 18:51:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcCodec) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcCodec) String() string {
|
2020-01-02 18:23:43 +00:00
|
|
|
return "grpc"
|
2019-06-18 18:51:52 +01:00
|
|
|
}
|