Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-05-26 00:43:38 +03:00
parent b1264f5094
commit 4175c23a48

38
json.go
View File

@ -16,10 +16,11 @@ const (
) )
func (c *jsonCodec) Marshal(v interface{}) ([]byte, error) { func (c *jsonCodec) Marshal(v interface{}) ([]byte, error) {
switch m := v.(type) { if v == nil {
case nil:
return nil, nil return nil, nil
case *codec.Frame: }
if m, ok := v.(*codec.Frame); ok {
return m.Data, nil return m.Data, nil
} }
@ -31,13 +32,11 @@ func (c *jsonCodec) Marshal(v interface{}) ([]byte, error) {
} }
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error { func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
if len(b) == 0 { if len(b) == 0 || v == nil {
return nil return nil
} }
switch m := v.(type) {
case nil: if m, ok := v.(*codec.Frame); ok {
return nil
case *codec.Frame:
m.Data = b m.Data = b
return nil return nil
} }
@ -54,17 +53,7 @@ func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Message
} }
func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error { func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error {
switch m := v.(type) { if v == nil {
case nil:
return nil
case *codec.Frame:
buf, err := io.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
m.Data = buf
return nil return nil
} }
@ -83,16 +72,8 @@ func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error {
} }
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error { func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
switch m := v.(type) { if v == nil {
case nil:
return nil return nil
case *codec.Frame:
_, err := conn.Write(m.Data)
return err
}
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
v = nv
} }
buf, err := c.Marshal(v) buf, err := c.Marshal(v)
@ -100,7 +81,6 @@ func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error
return err return err
} }
_, err = conn.Write(buf) _, err = conn.Write(buf)
return err return err
} }