Skip to content

Commit

Permalink
avro: fix map union ptr type encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored Feb 11, 2021
1 parent 6ca1b6d commit 65106f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion codec_union.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ func (e *mapUnionEncoder) Encode(ptr unsafe.Pointer, w *Writer) {

elemType := reflect2.TypeOf(val)
elemPtr := reflect2.PtrOf(val)
encoderOfType(e.cfg, schema, elemType).Encode(elemPtr, w)

encoder := encoderOfType(e.cfg, schema, elemType)
if elemType.LikePtr() {
encoder = &onePtrEncoder{encoder}
}
encoder.Encode(elemPtr, w)
}

func decoderOfPtrUnion(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValDecoder {
Expand Down
21 changes: 21 additions & 0 deletions encoder_union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ func TestEncoder_UnionMap(t *testing.T) {
assert.Equal(t, []byte{0x02, 0x06, 0x66, 0x6F, 0x6F}, buf.Bytes())
}

func TestEncoder_UnionMapRecord(t *testing.T) {
defer ConfigTeardown()

schema := `["null", {
"type": "record",
"name": "test",
"fields" : [
{"name": "a", "type": ["string", "null"], "default": "test"},
{"name": "b", "type": "string"}
]
}]`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
assert.NoError(t, err)

err = enc.Encode(map[string]interface{}{"test": map[string]interface{}{"b": "foo"}})

assert.NoError(t, err)
assert.Equal(t, []byte{0x02, 0x00, 0x08, 0x74, 0x65, 0x73, 0x74, 0x06, 0x66, 0x6F, 0x6F}, buf.Bytes())
}

func TestEncoder_UnionMapNamed(t *testing.T) {
defer ConfigTeardown()

Expand Down

0 comments on commit 65106f8

Please sign in to comment.