codec: fix noop codec to handle *broker.Message

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-01-29 16:07:21 +03:00
parent 925b3af46b
commit bcd7f6a551

View File

@ -1,6 +1,7 @@
package codec package codec
import ( import (
"encoding/json"
"io" "io"
"io/ioutil" "io/ioutil"
) )
@ -40,7 +41,7 @@ func (c *noopCodec) ReadBody(conn io.Reader, b interface{}) error {
case *Frame: case *Frame:
v.Data = buf v.Data = buf
default: default:
return ErrInvalidMessage return json.Unmarshal(buf, v)
} }
return nil return nil
@ -64,7 +65,11 @@ func (c *noopCodec) Write(conn io.Writer, m *Message, b interface{}) error {
case []byte: case []byte:
v = vb v = vb
default: default:
return ErrInvalidMessage var err error
v, err = json.Marshal(vb)
if err != nil {
return err
}
} }
_, err := conn.Write(v) _, err := conn.Write(v)
return err return err
@ -98,11 +103,11 @@ func (c *noopCodec) Marshal(v interface{}) ([]byte, error) {
case *Message: case *Message:
return ve.Body, nil return ve.Body, nil
} }
return nil, ErrInvalidMessage
return json.Marshal(v)
} }
func (c *noopCodec) Unmarshal(d []byte, v interface{}) error { func (c *noopCodec) Unmarshal(d []byte, v interface{}) error {
var err error
if v == nil { if v == nil {
return nil return nil
} }
@ -119,9 +124,7 @@ func (c *noopCodec) Unmarshal(d []byte, v interface{}) error {
ve.Data = d ve.Data = d
case *Message: case *Message:
ve.Body = d ve.Body = d
default:
err = ErrInvalidMessage
} }
return err return json.Unmarshal(d, v)
} }