Skip to content

Commit

Permalink
Fix issue with typed union in record (hamba#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored Apr 2, 2019
1 parent d0e593a commit 89877ac
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions codec_union.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ type unionTypedDecoder struct {
}

func (d *unionTypedDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
if *((*unsafe.Pointer)(ptr)) == nil {
newPtr := d.typ.UnsafeNew()
*((*unsafe.Pointer)(ptr)) = newPtr
}
union := d.typ.UnsafeIndirect(ptr).(UnionType)

schema := getUnionSchema(d.schema, r)
Expand Down
20 changes: 20 additions & 0 deletions decoder_union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ func TestDecoder_UnionTyped(t *testing.T) {
assert.Equal(t, 27, got.Val)
}

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

data := []byte{0x02, 0x36}
schema := `{
"type": "record",
"name": "test",
"fields" : [
{"name": "a", "type": ["null", "int"]}
]
}`
dec, _ := avro.NewDecoder(schema, bytes.NewReader(data))

got := &TestUnionRecord{}
err := dec.Decode(&got)

assert.NoError(t, err)
assert.Equal(t, 27, got.A.Val)
}

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

Expand Down
4 changes: 4 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type TestNestedRecord struct {
B TestRecord `avro:"b"`
}

type TestUnionRecord struct {
A *TestUnionType `avro:"a"`
}

type TestUnionType struct {
Val interface{}
}
Expand Down

0 comments on commit 89877ac

Please sign in to comment.