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

Ability to serialize uint64 values as fixed-length entries: #277

Merged
merged 2 commits into from
Jul 27, 2023
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ More examples in the [godoc](https://godoc.org/github.com/hamba/avro).
| `double` | `float64` | `float64` |
| `long` | `int64`, `uint32`\* | `int64`, `uint32` |
| `int` | `int`, `int32`, `int16`, `int8`, `uint8`\*, `uint16`\* | `int`, `uint8`, `uint16` |
| `fixed` | `uint64` | `uint64` |
| `string` | `string` | `string` |
| `array` | `[]T` | `[]any` |
| `enum` | `string` | `string` |
Expand Down
30 changes: 30 additions & 0 deletions codec_fixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func createDecoderOfFixed(schema Schema, typ reflect2.Type) ValDecoder {
}
return &fixedCodec{arrayType: typ.(*reflect2.UnsafeArrayType)}

case reflect.Uint64:
fixed := schema.(*FixedSchema)
if fixed.Size() != 8 {
break
}

return &fixedUint64Codec{}

case reflect.Struct:
ls := fixed.Logical()
if ls == nil {
Expand Down Expand Up @@ -51,6 +59,14 @@ func createEncoderOfFixed(schema Schema, typ reflect2.Type) ValEncoder {
}
return &fixedCodec{arrayType: typ.(*reflect2.UnsafeArrayType)}

case reflect.Uint64:
fixed := schema.(*FixedSchema)
if fixed.Size() != 8 {
break
}

return &fixedUint64Codec{}

case reflect.Ptr:
ptrType := typ.(*reflect2.UnsafePtrType)
elemType := ptrType.Elem()
Expand Down Expand Up @@ -80,6 +96,20 @@ func createEncoderOfFixed(schema Schema, typ reflect2.Type) ValEncoder {
}
}

type fixedUint64Codec [8]byte

func (c *fixedUint64Codec) Decode(ptr unsafe.Pointer, r *Reader) {
buffer := c[:]
r.Read(buffer)
*(*uint64)(ptr) = binary.BigEndian.Uint64(buffer)
}

func (c *fixedUint64Codec) Encode(ptr unsafe.Pointer, w *Writer) {
buffer := c[:]
binary.BigEndian.PutUint64(buffer, *(*uint64)(ptr))
_, _ = w.Write(buffer)
}

type fixedCodec struct {
arrayType *reflect2.UnsafeArrayType
}
Expand Down
28 changes: 28 additions & 0 deletions decoder_fixed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package avro_test
import (
"bytes"
"fmt"
"math"
"math/big"
"testing"

Expand Down Expand Up @@ -128,3 +129,30 @@ func TestDecoder_FixedLogicalDurationSizeNot12(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, fmt.Errorf("avro: avro.LogicalDuration is unsupported for Avro fixed, size=11"), err)
}

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

data := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
schema := `{"type":"fixed", "name": "test", "size": 8}`
dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
require.NoError(t, err)

var got uint64
err = dec.Decode(&got)
require.NoError(t, err)
assert.Equal(t, uint64(math.MaxUint64), got)
}
func TestDecoder_FixedUint64_Simple(t *testing.T) {
defer ConfigTeardown()

data := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}
schema := `{"type":"fixed", "name": "test", "size": 8}`
dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
require.NoError(t, err)

var got uint64
err = dec.Decode(&got)
require.NoError(t, err)
assert.Equal(t, uint64(256), got)
}
29 changes: 29 additions & 0 deletions encoder_fixed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package avro_test
import (
"bytes"
"fmt"
"math"
"math/big"
"testing"

Expand Down Expand Up @@ -121,3 +122,31 @@ func TestEncoder_FixedLogicalDurationSizeNot12(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, fmt.Errorf("avro: avro.LogicalDuration is unsupported for Avro fixed, size=11"), err)
}

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

schema := `{"type":"fixed", "name": "test", "size": 8}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)

err = enc.Encode(uint64(math.MaxUint64))

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

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

schema := `{"type":"fixed", "name": "test", "size": 8}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)

err = enc.Encode(uint64(256))

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