Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-11-29 21:06:14 +03:00
parent f627fea89e
commit 012d8c3fcb

View File

@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"
// nolint: staticcheck
oldproto "github.com/golang/protobuf/proto"
"github.com/unistack-org/micro/v3/codec"
"google.golang.org/protobuf/proto"
@ -13,10 +14,9 @@ import (
type protoCodec struct{}
func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
if v == nil {
return nil, nil
}
switch m := v.(type) {
case nil:
return nil, nil
case *codec.Frame:
return m.Data, nil
case proto.Message:
@ -28,10 +28,12 @@ func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
}
func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
if d == nil || v == nil {
if d == nil {
return nil
}
switch m := v.(type) {
case nil:
return nil
case *codec.Frame:
m.Data = d
case proto.Message:
@ -47,10 +49,9 @@ func (c *protoCodec) ReadHeader(conn io.ReadWriter, m *codec.Message, t codec.Me
}
func (c *protoCodec) ReadBody(conn io.ReadWriter, b interface{}) error {
if b == nil {
return nil
}
switch m := b.(type) {
case nil:
return nil
case *codec.Frame:
buf, err := ioutil.ReadAll(conn)
if err != nil {
@ -75,10 +76,9 @@ func (c *protoCodec) ReadBody(conn io.ReadWriter, b interface{}) error {
}
func (c *protoCodec) Write(conn io.ReadWriter, m *codec.Message, b interface{}) error {
if b == nil {
return nil
}
switch m := b.(type) {
case nil:
return nil
case *codec.Frame:
_, err := conn.Write(m.Data)
return err