diff --git a/codec.go b/codec.go index 50a96ef..681a104 100644 --- a/codec.go +++ b/codec.go @@ -6,6 +6,7 @@ import ( "github.com/golang/protobuf/proto" "github.com/micro/go-micro/codec" + "github.com/micro/go-micro/codec/bytes" "github.com/micro/go-micro/codec/jsonrpc" "github.com/micro/go-micro/codec/protorpc" "google.golang.org/grpc/encoding" @@ -14,6 +15,7 @@ import ( type jsonCodec struct{} type bytesCodec struct{} type protoCodec struct{} +type wrapCodec struct { encoding.Codec } var ( defaultGRPCCodecs = map[string]encoding.Codec{ @@ -36,6 +38,27 @@ var ( } ) +func (w wrapCodec) String() string { + return w.Codec.Name() +} + +func (w wrapCodec) Marshal(v interface{}) ([]byte, error) { + b, ok := v.(*bytes.Frame) + if ok { + return b.Data, nil + } + return w.Codec.Marshal(v) +} + +func (w wrapCodec) Unmarshal(data []byte, v interface{}) error { + b, ok := v.(*bytes.Frame) + if ok { + b.Data = data + return nil + } + return w.Codec.Unmarshal(data, v) +} + func (protoCodec) Marshal(v interface{}) ([]byte, error) { return proto.Marshal(v.(proto.Message)) } diff --git a/grpc.go b/grpc.go index bde2e4c..1b33081 100644 --- a/grpc.go +++ b/grpc.go @@ -56,8 +56,8 @@ type grpcServer struct { } func init() { - encoding.RegisterCodec(jsonCodec{}) - encoding.RegisterCodec(bytesCodec{}) + encoding.RegisterCodec(wrapCodec{jsonCodec{}}) + encoding.RegisterCodec(wrapCodec{bytesCodec{}}) } func newGRPCServer(opts ...server.Option) server.Server {