Skip to content

Commit

Permalink
avro: fix map encoding with more than blocklength keys (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
akihiro17 authored Jul 31, 2020
1 parent bd2b64b commit 3ded0d4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion codec_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (e *mapEncoder) Encode(ptr unsafe.Pointer, w *Writer) {
for {
wrote := w.WriteBlockCB(func(w *Writer) int64 {
var i int
for i = 0; iter.HasNext() || i > blockLength; i++ {
for i = 0; iter.HasNext() && i < blockLength; i++ {
keyPtr, elemPtr := iter.UnsafeNext()
w.WriteString(*((*string)(keyPtr)))
e.encoder.Encode(elemPtr, w)
Expand Down
24 changes: 24 additions & 0 deletions encoder_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,27 @@ func TestEncoder_MapError(t *testing.T) {

assert.Error(t, err)
}

func TestEncoder_MapWithMoreThanBlockLengthKeys(t *testing.T) {
avro.DefaultConfig = avro.Config{
TagKey: "avro",
BlockLength: 1,
UnionResolutionError: true,
}.Freeze()

schema := `{"type":"map", "values": "int"}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
assert.NoError(t, err)

err = enc.Encode(map[string]int{"foo": 1, "bar": 2})

assert.NoError(t, err)
assert.Condition(t, func() bool {
// {"foo": 1, "bar": 2}
foobar := bytes.Equal([]byte{0x01, 0x0a, 0x06, 0x66, 0x6F, 0x6F, 0x02, 0x01, 0x0a, 0x06, 0x62, 0x61, 0x72, 0x04, 0x0}, buf.Bytes())
// {"bar": 2, "foo": 1}
barfoo := bytes.Equal([]byte{0x01, 0x0a, 0x06, 0x62, 0x61, 0x72, 0x04, 0x01, 0x0a, 0x06, 0x66, 0x6F, 0x6F, 0x02, 0x0}, buf.Bytes())
return (foobar || barfoo)
})
}

0 comments on commit 3ded0d4

Please sign in to comment.