diff --git a/gateway/codec/codce.go b/gateway/codec/codce.go new file mode 100644 index 0000000..f4c535d --- /dev/null +++ b/gateway/codec/codce.go @@ -0,0 +1,60 @@ +package codec + +import ( + "google.golang.org/grpc/encoding" + "google.golang.org/protobuf/proto" +) + +// protoCodec 用于 gateway 解析全部的 grpc 类型的消息 +type protoCodec struct{} + +func (protoCodec) Name() string { + return "wheat-cache-proto" +} + +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)) +} + +type frame struct { + payload []byte +} + +type proxyCodec struct { + baseCodec encoding.Codec +} + +func (p *proxyCodec) Name() string { + return "wheat-cache-proxy" +} + +func (p *proxyCodec) Marshal(v interface{}) ([]byte, error) { + out, ok := v.(*frame) + if !ok { + return p.Marshal(v) + } + return out.payload, nil +} + +func (p *proxyCodec) Unmarshal(data []byte, v interface{}) error { + dst, ok := v.(*frame) + if !ok { + return p.Unmarshal(data, v) + } + + dst.payload = data + return nil +} + +// CodeWithParent 生成基于 proto 的解码器 +func CodeWithParent(parent encoding.Codec) encoding.Codec { + return &proxyCodec{parent} +} + +func Codec() encoding.Codec { + return CodeWithParent(protoCodec{}) +}