Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Union Decoder uses readInt #387

Merged
merged 3 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading