Merge pull request #145 from unistack-org/codec_fix

codec: add []byte support to noop codec
This commit is contained in:
Василий Толстов 2022-10-26 08:28:36 +03:00 committed by GitHub
commit 4120f79b55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -106,6 +106,10 @@ func (c *noopCodec) Unmarshal(d []byte, v interface{}, opts ...Option) error {
case *string:
*ve = string(d)
return nil
case []byte:
ve = make([]byte, len(d))
copy(ve, d)
return nil
case *[]byte:
*ve = d
return nil

View File

@ -5,7 +5,7 @@ import (
"testing"
)
func TestNoopBytes(t *testing.T) {
func TestNoopBytesPtr(t *testing.T) {
req := []byte("test req")
rsp := make([]byte, len(req))
@ -19,6 +19,20 @@ func TestNoopBytes(t *testing.T) {
}
}
func TestNoopBytes(t *testing.T) {
req := []byte("test req")
var rsp []byte
nc := NewCodec()
if err := nc.Unmarshal(req, &rsp); err != nil {
t.Fatal(err)
}
if !bytes.Equal(req, rsp) {
t.Fatalf("req not eq rsp: %s != %s", req, rsp)
}
}
func TestNoopString(t *testing.T) {
req := []byte("test req")
var rsp string