add cruft

This commit is contained in:
Asim Aslam 2019-06-17 20:05:58 +01:00 committed by Vasiliy Tolstov
parent 6f50ba3575
commit ac860f37e2
2 changed files with 25 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/micro/go-micro/codec" "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/jsonrpc"
"github.com/micro/go-micro/codec/protorpc" "github.com/micro/go-micro/codec/protorpc"
"google.golang.org/grpc/encoding" "google.golang.org/grpc/encoding"
@ -14,6 +15,7 @@ import (
type jsonCodec struct{} type jsonCodec struct{}
type bytesCodec struct{} type bytesCodec struct{}
type protoCodec struct{} type protoCodec struct{}
type wrapCodec struct { encoding.Codec }
var ( var (
defaultGRPCCodecs = map[string]encoding.Codec{ 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) { func (protoCodec) Marshal(v interface{}) ([]byte, error) {
return proto.Marshal(v.(proto.Message)) return proto.Marshal(v.(proto.Message))
} }

View File

@ -56,8 +56,8 @@ type grpcServer struct {
} }
func init() { func init() {
encoding.RegisterCodec(jsonCodec{}) encoding.RegisterCodec(wrapCodec{jsonCodec{}})
encoding.RegisterCodec(bytesCodec{}) encoding.RegisterCodec(wrapCodec{bytesCodec{}})
} }
func newGRPCServer(opts ...server.Option) server.Server { func newGRPCServer(opts ...server.Option) server.Server {