2019-06-03 20:44:43 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2020-09-20 16:08:45 +03:00
|
|
|
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
|
|
|
|
2020-09-20 16:08:45 +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"
|
2020-09-20 16:08:45 +03:00
|
|
|
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
|
|
|
|
2020-09-20 16:08:45 +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-07-08 05:32:10 +03:00
|
|
|
|
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) {
|
2020-09-20 16:08:45 +03:00
|
|
|
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 {
|
2020-09-20 16:08:45 +03:00
|
|
|
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
|
|
|
|
}
|
2020-09-20 16:08:45 +03:00
|
|
|
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) {
|
2020-09-20 16:08:45 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case proto.Message:
|
|
|
|
return proto.Marshal(m)
|
|
|
|
case oldproto.Message:
|
|
|
|
return oldproto.Marshal(m)
|
2020-02-19 02:05:38 +03:00
|
|
|
}
|
2020-09-20 16:08:45 +03:00
|
|
|
return nil, codec.ErrInvalidMessage
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (protoCodec) Unmarshal(data []byte, v interface{}) error {
|
2020-09-20 16:08:45 +03:00
|
|
|
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)
|
2020-02-19 02:05:38 +03:00
|
|
|
}
|
2020-09-20 16:08:45 +03:00
|
|
|
return codec.ErrInvalidMessage
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (protoCodec) Name() string {
|
|
|
|
return "proto"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
2020-09-20 16:08:45 +03:00
|
|
|
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-07-08 05:32:10 +03:00
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
return json.Marshal(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jsonCodec) Unmarshal(data []byte, v interface{}) error {
|
2020-01-07 21:37:34 +03:00
|
|
|
if len(data) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-20 16:08:45 +03:00
|
|
|
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-07-04 11:43:36 +03:00
|
|
|
}
|
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) {
|
2020-09-20 16:08:45 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *[]byte:
|
|
|
|
return *m, nil
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
2020-09-20 16:08:45 +03:00
|
|
|
return nil, codec.ErrInvalidMessage
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bytesCodec) Unmarshal(data []byte, v interface{}) error {
|
2020-09-20 16:08:45 +03:00
|
|
|
if len(data) == 0 {
|
|
|
|
return nil
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
2020-09-20 16:08:45 +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 {
|
2020-08-09 17:43:41 +03:00
|
|
|
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 {
|
2020-08-09 17:43:41 +03:00
|
|
|
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 {
|
2020-02-24 17:15:20 +03:00
|
|
|
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
|
2020-09-20 16:08:45 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *bytes.Frame:
|
|
|
|
return g.ServerStream.RecvMsg(m)
|
2019-06-18 20:51:52 +03:00
|
|
|
}
|
2020-08-09 17:43:41 +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
|
2020-08-09 17:43:41 +03:00
|
|
|
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
|
|
|
}
|