Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-11-29 21:08:49 +03:00
parent 03740d5647
commit c2fe5b084e
2 changed files with 19 additions and 20 deletions

View File

@ -39,10 +39,9 @@ type jsonCodec struct {
} }
func (c *jsonCodec) Marshal(b interface{}) ([]byte, error) { func (c *jsonCodec) Marshal(b interface{}) ([]byte, error) {
if b == nil {
return nil, nil
}
switch m := b.(type) { switch m := b.(type) {
case nil:
return nil, nil
case *codec.Frame: case *codec.Frame:
return m.Data, nil return m.Data, nil
} }
@ -54,10 +53,12 @@ func (c *jsonCodec) Marshal(b interface{}) ([]byte, error) {
} }
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error { func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
if b == nil || v == nil { if b == nil {
return nil return nil
} }
switch m := v.(type) { switch m := v.(type) {
case nil:
return nil
case *codec.Frame: case *codec.Frame:
m.Data = b m.Data = b
return nil return nil
@ -72,10 +73,9 @@ func (c *jsonCodec) ReadHeader(conn io.ReadWriter, m *codec.Message, t codec.Mes
} }
func (c *jsonCodec) ReadBody(conn io.ReadWriter, b interface{}) error { func (c *jsonCodec) ReadBody(conn io.ReadWriter, b interface{}) error {
if b == nil {
return nil
}
switch m := b.(type) { switch m := b.(type) {
case nil:
return nil
case *codec.Frame: case *codec.Frame:
buf, err := ioutil.ReadAll(conn) buf, err := ioutil.ReadAll(conn)
if err != nil { if err != nil {
@ -89,10 +89,9 @@ func (c *jsonCodec) ReadBody(conn io.ReadWriter, b interface{}) error {
} }
func (c *jsonCodec) Write(conn io.ReadWriter, m *codec.Message, b interface{}) error { func (c *jsonCodec) Write(conn io.ReadWriter, m *codec.Message, b interface{}) error {
if b == nil {
return nil
}
switch m := b.(type) { switch m := b.(type) {
case nil:
return nil
case *codec.Frame: case *codec.Frame:
_, err := conn.Write(m.Data) _, err := conn.Write(m.Data)
return err return err

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
// nolint: staticcheck
oldproto "github.com/golang/protobuf/proto" oldproto "github.com/golang/protobuf/proto"
"github.com/segmentio/encoding/proto" "github.com/segmentio/encoding/proto"
"github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/codec"
@ -14,10 +15,9 @@ import (
type protoCodec struct{} type protoCodec struct{}
func (c *protoCodec) Marshal(v interface{}) ([]byte, error) { func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
if v == nil {
return nil, nil
}
switch m := v.(type) { switch m := v.(type) {
case nil:
return nil, nil
case *codec.Frame: case *codec.Frame:
return m.Data, nil return m.Data, nil
case newproto.Message, oldproto.Message, proto.Message: case newproto.Message, oldproto.Message, proto.Message:
@ -27,10 +27,12 @@ func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
} }
func (c *protoCodec) Unmarshal(d []byte, v interface{}) error { func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
if d == nil || v == nil { if d == nil {
return nil return nil
} }
switch m := v.(type) { switch m := v.(type) {
case nil:
return nil
case *codec.Frame: case *codec.Frame:
m.Data = d m.Data = d
case newproto.Message, oldproto.Message, proto.Message: case newproto.Message, oldproto.Message, proto.Message:
@ -44,10 +46,9 @@ func (c *protoCodec) ReadHeader(conn io.ReadWriter, m *codec.Message, t codec.Me
} }
func (c *protoCodec) ReadBody(conn io.ReadWriter, b interface{}) error { func (c *protoCodec) ReadBody(conn io.ReadWriter, b interface{}) error {
if b == nil {
return nil
}
switch m := b.(type) { switch m := b.(type) {
case nil:
return nil
case *codec.Frame: case *codec.Frame:
buf, err := ioutil.ReadAll(conn) buf, err := ioutil.ReadAll(conn)
if err != nil { if err != nil {
@ -66,10 +67,9 @@ func (c *protoCodec) ReadBody(conn io.ReadWriter, b interface{}) error {
} }
func (c *protoCodec) Write(conn io.ReadWriter, m *codec.Message, 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) { switch m := b.(type) {
case nil:
return nil
case *codec.Frame: case *codec.Frame:
_, err := conn.Write(m.Data) _, err := conn.Write(m.Data)
return err return err