2 Commits

Author SHA1 Message Date
af19b4f2e7 fix nil buf check
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-04-12 19:12:16 +03:00
b7f6b22fb2 add test file
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-04-12 16:54:00 +03:00
2 changed files with 21 additions and 1 deletions

18
codec_test.go Normal file
View File

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

View File

@@ -23,7 +23,7 @@ func (c *jsonCodec) Marshal(b interface{}) ([]byte, error) {
}
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
if b == nil {
if len(b) == 0 {
return nil
}
switch m := v.(type) {
@@ -49,6 +49,8 @@ func (c *jsonCodec) 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