2 Commits

Author SHA1 Message Date
aa06453219 fix nil buf check
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-04-12 19:13:29 +03:00
87e4e04634 add test file
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-04-12 16:58:55 +03:00
2 changed files with 23 additions and 2 deletions

19
codec_test.go Normal file
View File

@@ -0,0 +1,19 @@
package jsonpb
import (
"bytes"
"testing"
)
func TestReadBody(t *testing.T) {
t.Skip("skip without proto message")
s := &struct {
Name string
}{}
c := NewCodec()
b := bytes.NewReader(nil)
err := c.ReadBody(b, s)
if err != nil {
t.Fatal(err)
}
}

View File

@@ -39,7 +39,7 @@ func (c *jsonpbCodec) Marshal(v interface{}) ([]byte, error) {
}
func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}) error {
if d == nil {
if len(d) == 0 {
return nil
}
switch m := v.(type) {
@@ -65,6 +65,8 @@ func (c *jsonpbCodec) ReadBody(conn io.Reader, b interface{}) error {
buf, err := ioutil.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
m.Data = buf
return nil
@@ -72,7 +74,7 @@ func (c *jsonpbCodec) ReadBody(conn io.Reader, b interface{}) error {
buf, err := ioutil.ReadAll(conn)
if err != nil {
return err
} else if buf == nil {
} else if len(buf) == 0 {
return nil
}
return JsonpbUnmarshaler.Unmarshal(buf, m)