Skip to content

Commit

Permalink
avro: allow encoding int32 and float32 types as long and double
Browse files Browse the repository at this point in the history
  • Loading branch information
mcgrawia authored Aug 3, 2020
1 parent 25e9cc9 commit 436d34e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
30 changes: 24 additions & 6 deletions codec_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,13 @@ func createEncoderOfNative(schema Schema, typ reflect2.Type) ValEncoder {
return &int16Codec{}

case reflect.Int32:
if schema.Type() != Int {
break
switch schema.Type() {
case Long:
return &int32LongCodec{}

case Int:
return &int32Codec{}
}
return &int32Codec{}

case reflect.Int64:
st := schema.Type()
Expand All @@ -161,10 +164,13 @@ func createEncoderOfNative(schema Schema, typ reflect2.Type) ValEncoder {
}

case reflect.Float32:
if schema.Type() != Float {
break
switch schema.Type() {
case Double:
return &float32DoubleCodec{}

case Float:
return &float32Codec{}
}
return &float32Codec{}

case reflect.Float64:
if schema.Type() != Double {
Expand Down Expand Up @@ -296,6 +302,12 @@ func (*int32Codec) Encode(ptr unsafe.Pointer, w *Writer) {
w.WriteInt(*((*int32)(ptr)))
}

type int32LongCodec struct{}

func (*int32LongCodec) Encode(ptr unsafe.Pointer, w *Writer) {
w.WriteLong(int64(*((*int32)(ptr))))
}

type int64Codec struct{}

func (*int64Codec) Decode(ptr unsafe.Pointer, r *Reader) {
Expand All @@ -316,6 +328,12 @@ func (*float32Codec) Encode(ptr unsafe.Pointer, w *Writer) {
w.WriteFloat(*((*float32)(ptr)))
}

type float32DoubleCodec struct{}

func (*float32DoubleCodec) Encode(ptr unsafe.Pointer, w *Writer) {
w.WriteDouble(float64(*((*float32)(ptr))))
}

type float64Codec struct{}

func (*float64Codec) Decode(ptr unsafe.Pointer, r *Reader) {
Expand Down
28 changes: 28 additions & 0 deletions encoder_native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ func TestEncoder_Int64(t *testing.T) {
assert.Equal(t, []byte{0x36}, buf.Bytes())
}

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

schema := "long"
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
assert.NoError(t, err)

err = enc.Encode(int32(27))

assert.NoError(t, err)
assert.Equal(t, []byte{0x36}, buf.Bytes())
}

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

Expand Down Expand Up @@ -226,6 +240,20 @@ func TestEncoder_Float64(t *testing.T) {
assert.Equal(t, []byte{0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xF2, 0x3F}, buf.Bytes())
}

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

schema := "double"
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
assert.NoError(t, err)

err = enc.Encode(float32(1.15))

assert.NoError(t, err)
assert.Equal(t, []byte{0x0, 0x0, 0x0, 0x60, 0x66, 0x66, 0xf2, 0x3f}, buf.Bytes())
}

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

Expand Down

0 comments on commit 436d34e

Please sign in to comment.