Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-11-29 21:03:18 +03:00
parent 38cf770f2e
commit 74c2210300

19
json.go
View File

@ -12,10 +12,9 @@ import (
type jsonCodec struct{} 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
} }
@ -24,10 +23,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
@ -41,10 +42,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 {
@ -58,10 +58,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