diff --git a/list.go b/list.go index 23b0c15a..28cbb8d4 100644 --- a/list.go +++ b/list.go @@ -330,7 +330,11 @@ func (l TextList) BytesAt(i int) ([]byte, error) { if err != nil { return nil, err } - return p.Data(), nil + b := p.Data() + if len(b) == 0 { + return b, nil + } + return b[:len(b)-1 : len(b)], nil } // Set sets the i'th string in the list to v. diff --git a/list_test.go b/list_test.go index 5ad8c846..28844d47 100644 --- a/list_test.go +++ b/list_test.go @@ -1,6 +1,7 @@ package capnp import ( + "bytes" "testing" ) @@ -52,6 +53,32 @@ func TestToListDefault(t *testing.T) { } } +func TestTextListBytesAt(t *testing.T) { + msg := &Message{Arena: SingleSegment([]byte{ + 0, 0, 0, 0, 0, 0, 0, 0, + 0x01, 0, 0, 0, 0x22, 0, 0, 0, + 'f', 'o', 'o', 0, 0, 0, 0, 0, + })} + seg, err := msg.Segment(0) + if err != nil { + t.Fatal(err) + } + list := TextList{List{ + seg: seg, + off: 8, + length: 1, + size: ObjectSize{DataSize: 1}, + depthLimit: maxDepth, + }} + b, err := list.BytesAt(0) + if err != nil { + t.Errorf("list.BytesAt(0) error: %v", err) + } + if !bytes.Equal(b, []byte("foo")) { + t.Errorf("list.BytesAt(0) = %q; want \"foo\"", b) + } +} + func TestListValue(t *testing.T) { _, seg, err := NewMessage(SingleSegment(nil)) if err != nil {