From ac860f37e24b431300d4555d43bc445d84e0e5b7 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Mon, 17 Jun 2019 20:05:58 +0100 Subject: [PATCH] add cruft --- codec.go | 23 +++++++++++++++++++++++ grpc.go | 4 ++-- 2 files changed, 25 insertions(+), 2 deletions(-) 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 {