Skip to content

Commit

Permalink
fix: Union Decoder uses readInt (#387)
Browse files Browse the repository at this point in the history
Co-authored-by: Brian Shih <brianshih@Brians-MacBook-Pro.local>
  • Loading branch information
brianshih1 and Brian Shih committed Apr 28, 2024
1 parent 2461d45 commit e42dea1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
8 changes: 8 additions & 0 deletions codec_generic_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ func TestGenericDecode(t *testing.T) {
want: map[string]any{"string": "foo"},
wantErr: require.NoError,
},
{
name: "Union Zero Index",
// 0x80 represents 128. So the bytes below will result in 0
// as a result of zig-zag encoding.
data: []byte{0x80, 0x80, 0x80, 0x80, 0x30},
schema: `["null"]`,
wantErr: require.NoError,
},
{
name: "Union Nil",
data: []byte{0x00},
Expand Down
18 changes: 9 additions & 9 deletions codec_union.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (e *mapUnionEncoder) Encode(ptr unsafe.Pointer, w *Writer) {
return
}

w.WriteLong(int64(pos))
w.WriteInt(int32(pos))

if schema.Type() == Null && val == nil {
return
Expand Down Expand Up @@ -259,26 +259,26 @@ func encoderOfNullableUnion(cfg *frozenConfig, schema Schema, typ reflect2.Type)
schema: union,
encoder: encoder,
isPtr: isPtr,
nullIdx: int64(nullIdx),
typeIdx: int64(typeIdx),
nullIdx: int32(nullIdx),
typeIdx: int32(typeIdx),
}
}

type unionNullableEncoder struct {
schema *UnionSchema
encoder ValEncoder
isPtr bool
nullIdx int64
typeIdx int64
nullIdx int32
typeIdx int32
}

func (e *unionNullableEncoder) Encode(ptr unsafe.Pointer, w *Writer) {
if *((*unsafe.Pointer)(ptr)) == nil {
w.WriteLong(e.nullIdx)
w.WriteInt(e.nullIdx)
return
}

w.WriteLong(e.typeIdx)
w.WriteInt(e.typeIdx)
newPtr := ptr
if e.isPtr {
newPtr = *((*unsafe.Pointer)(ptr))
Expand Down Expand Up @@ -445,15 +445,15 @@ type unionResolverEncoder struct {
}

func (e *unionResolverEncoder) Encode(ptr unsafe.Pointer, w *Writer) {
w.WriteLong(int64(e.pos))
w.WriteInt(int32(e.pos))

e.encoder.Encode(ptr, w)
}

func getUnionSchema(schema *UnionSchema, r *Reader) (int, Schema) {
types := schema.Types()

idx := int(r.ReadLong())
idx := int(r.ReadInt())
if idx < 0 || idx > len(types)-1 {
r.ReportError("decode union type", "unknown union type")
return 0, nil
Expand Down

0 comments on commit e42dea1

Please sign in to comment.