Skip to content

Commit

Permalink
fix: panic on null type
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma committed Feb 6, 2025
1 parent 29f45e2 commit e4a8a64
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
8 changes: 6 additions & 2 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func decoderOfType(d *decoderContext, schema Schema, typ reflect2.Type) ValDecod
}

switch schema.Type() {
case Null:
return &nullCodec{}
case String, Bytes, Int, Long, Float, Double, Boolean:
return createDecoderOfNative(schema.(*PrimitiveSchema), typ)
case Record:
Expand Down Expand Up @@ -197,8 +199,10 @@ func encoderOfType(e *encoderContext, schema Schema, typ reflect2.Type) ValEncod
}

switch schema.Type() {
case String, Bytes, Int, Long, Float, Double, Boolean, Null:
return createEncoderOfNative(schema, typ)
case Null:
return &nullCodec{}
case String, Bytes, Int, Long, Float, Double, Boolean:
return createEncoderOfNative(schema.(*PrimitiveSchema), typ)
case Record:
key := cacheKey{fingerprint: schema.Fingerprint(), rtype: typ.RType()}
defEnc := &deferEncoder{}
Expand Down
2 changes: 2 additions & 0 deletions codec_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func genericReceiver(schema Schema) (reflect2.Type, error) {
}

switch schema.Type() {
case Null:
return reflect2.TypeOf((*null)(nil)), nil
case Boolean:
var v bool
return reflect2.TypeOf(v), nil
Expand Down
7 changes: 7 additions & 0 deletions codec_generic_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ func TestGenericDecode(t *testing.T) {
want any
wantErr require.ErrorAssertionFunc
}{
{
name: "Null",
data: []byte{},
schema: "null",
want: nil,
wantErr: require.NoError,
},
{
name: "Bool",
data: []byte{0x01},
Expand Down
8 changes: 3 additions & 5 deletions codec_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func createDecoderOfNative(schema *PrimitiveSchema, typ reflect2.Type) ValDecode
}

//nolint:maintidx // Splitting this would not make it simpler.
func createEncoderOfNative(schema Schema, typ reflect2.Type) ValEncoder {
func createEncoderOfNative(schema *PrimitiveSchema, typ reflect2.Type) ValEncoder {
switch typ.Kind() {
case reflect.Bool:
if schema.Type() != Boolean {
Expand Down Expand Up @@ -323,10 +323,6 @@ func createEncoderOfNative(schema Schema, typ reflect2.Type) ValEncoder {
return &bytesDecimalPtrCodec{prec: dec.Precision(), scale: dec.Scale()}
}

if schema.Type() == Null {
return &nullCodec{}
}

return &errorEncoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s", typ.String(), schema.Type())}
}

Expand All @@ -350,6 +346,8 @@ func getLogicalType(schema Schema) LogicalType {

type nullCodec struct{}

func (*nullCodec) Decode(unsafe.Pointer, *Reader) {}

func (*nullCodec) Encode(unsafe.Pointer, *Writer) {}

type boolCodec struct{}
Expand Down
5 changes: 3 additions & 2 deletions decoder_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ func TestDecoder_RecordMap(t *testing.T) {
"fields" : [
{"name": "a", "type": "long"},
{"name": "b", "type": "string"},
{"name": "c", "type": ["null","string"]}
{"name": "c", "type": ["null","string"]},
{"name": "d", "type": "null"}
]
}`
dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
Expand All @@ -253,7 +254,7 @@ func TestDecoder_RecordMap(t *testing.T) {
err = dec.Decode(&got)

require.NoError(t, err)
assert.Equal(t, map[string]any{"a": int64(27), "b": "foo", "c": "foo"}, got)
assert.Equal(t, map[string]any{"a": int64(27), "b": "foo", "c": "foo", "d": nil}, got)
}

func TestDecoder_RecordMapInvalidKey(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions encoder_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,11 @@ func TestEncoder_RecordMap(t *testing.T) {
"name": "test",
"fields" : [
{"name": "a", "type": "long"},
{"name": "b", "type": "string"}
{"name": "b", "type": "string"},
{"name": "c", "type": "null"}
]
}`
obj := map[string]any{"a": int64(27), "b": "foo"}
obj := map[string]any{"a": int64(27), "b": "foo", "c": nil}
buf := &bytes.Buffer{}
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)
Expand Down

0 comments on commit e4a8a64

Please sign in to comment.