Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-05-26 00:46:17 +03:00
parent 032a9d5823
commit a04f4446ba

View File

@ -73,50 +73,24 @@ func (c *jsonpbCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Messa
} }
func (c *jsonpbCodec) ReadBody(conn io.Reader, v interface{}) error { func (c *jsonpbCodec) ReadBody(conn io.Reader, v interface{}) error {
switch m := v.(type) { if v == nil {
case nil:
return nil return nil
case *codec.Frame: }
buf, err := io.ReadAll(conn) buf, err := io.ReadAll(conn)
if err != nil { if err != nil {
return err return err
} else if len(buf) == 0 { } else if len(buf) == 0 {
return nil return nil
} }
m.Data = buf return c.Unmarshal(buf, v)
return nil
case proto.Message:
buf, err := io.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
if nv, nerr := rutil.StructFieldByTag(m, codec.DefaultTagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
m = nm
}
}
return JsonpbUnmarshaler.Unmarshal(buf, m)
}
return codec.ErrInvalidMessage
} }
func (c *jsonpbCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error { func (c *jsonpbCodec) 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
case proto.Message:
if nv, nerr := rutil.StructFieldByTag(m, codec.DefaultTagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
m = nm
}
} }
buf, err := JsonpbMarshaler.Marshal(m) buf, err := c.Marshal(v)
if err != nil { if err != nil {
return err return err
} else if len(buf) == 0 { } else if len(buf) == 0 {
@ -125,8 +99,6 @@ func (c *jsonpbCodec) Write(conn io.Writer, m *codec.Message, v interface{}) err
_, err = conn.Write(buf) _, err = conn.Write(buf)
return err return err
} }
return codec.ErrInvalidMessage
}
func (c *jsonpbCodec) String() string { func (c *jsonpbCodec) String() string {
return "jsonpb" return "jsonpb"