micro/server/grpc/codec.go

174 lines
3.7 KiB
Go
Raw Normal View History

2019-06-03 18:44:43 +01:00
package grpc
import (
"encoding/json"
"fmt"
2019-06-18 18:51:52 +01:00
"strings"
2019-06-03 18:44:43 +01:00
b "bytes"
"github.com/golang/protobuf/jsonpb"
2019-06-03 18:44:43 +01:00
"github.com/golang/protobuf/proto"
"github.com/micro/go-micro/codec"
2019-06-17 20:05:58 +01:00
"github.com/micro/go-micro/codec/bytes"
2019-06-03 18:44:43 +01:00
"github.com/micro/go-micro/codec/jsonrpc"
"github.com/micro/go-micro/codec/protorpc"
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
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{},
}
defaultRPCCodecs = map[string]codec.NewCodec{
"application/json": jsonrpc.NewCodec,
"application/json-rpc": jsonrpc.NewCodec,
"application/protobuf": protorpc.NewCodec,
"application/proto-rpc": protorpc.NewCodec,
"application/octet-stream": protorpc.NewCodec,
}
)
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
}
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) {
return proto.Marshal(v.(proto.Message))
}
func (protoCodec) Unmarshal(data []byte, v interface{}) error {
return proto.Unmarshal(data, v.(proto.Message))
}
func (protoCodec) Name() string {
return "proto"
}
func (jsonCodec) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func (jsonCodec) Unmarshal(data []byte, v interface{}) error {
if pb, ok := v.(proto.Message); ok {
return jsonpb.Unmarshal(b.NewReader(data), pb)
}
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 {
return nil, fmt.Errorf("failed to marshal: %v is not type of *[]byte", v)
}
return *b, nil
}
func (bytesCodec) Unmarshal(data []byte, v interface{}) error {
b, ok := v.(*[]byte)
if !ok {
return fmt.Errorf("failed to unmarshal: %v is not type of *[]byte", v)
}
*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 {
m.Header = make(map[string]string)
}
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
return g.s.SendMsg(&bytes.Frame{m.Body})
}
func (g *grpcCodec) Close() error {
return nil
}
func (g *grpcCodec) String() string {
return g.c.Name()
}