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

feat: allow array block contain only length #265

Merged
merged 3 commits into from
Jun 8, 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
7 changes: 7 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ type Config struct {
// This defaults to 100.
BlockLength int

// DisableBlockSizeHeader disables encoding of an array/map size in bytes.
// Encoded array/map will be prefixed with only the number of elements in
// contrast with default behavior which prefixes them with the number of elements
// and the total number of bytes in the array/map. Both approaches are valid according to the
// Avro specification, however not all decoders support the latter.
DisableBlockSizeHeader bool

// UnionResolutionError determines if an error will be returned
// when a type cannot be resolved while decoding a union.
UnionResolutionError bool
Expand Down
2 changes: 1 addition & 1 deletion writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (w *Writer) WriteString(s string) {

// WriteBlockHeader writes a Block Header to the Writer.
func (w *Writer) WriteBlockHeader(l, s int64) {
if s > 0 {
if s > 0 && !w.cfg.config.DisableBlockSizeHeader {
w.WriteLong(-l)
w.WriteLong(s)
return
Expand Down
65 changes: 53 additions & 12 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,10 @@ func TestWriter_WriteString(t *testing.T) {

func TestWriter_WriteBlockHeader(t *testing.T) {
tests := []struct {
len int64
size int64
want []byte
len int64
size int64
disableSize bool
want []byte
}{
{
len: 64,
Expand All @@ -438,10 +439,20 @@ func TestWriter_WriteBlockHeader(t *testing.T) {
size: 64,
want: []byte{0x7F, 0x80, 0x01},
},
{
len: 64,
size: 64,
disableSize: true,
want: []byte{0x80, 0x01},
},
}

for _, test := range tests {
w := avro.NewWriter(nil, 50)
cfg := avro.Config{
DisableBlockSizeHeader: test.disableSize,
}.Freeze()

w := avro.NewWriter(nil, 50, avro.WithWriterConfig(cfg))

w.WriteBlockHeader(test.len, test.size)

Expand All @@ -450,17 +461,47 @@ func TestWriter_WriteBlockHeader(t *testing.T) {
}

func TestWriter_WriteBlockCB(t *testing.T) {
w := avro.NewWriter(nil, 50)
tests := []struct {
disableSize bool
writeCallback func(w *avro.Writer) int64
want []byte
wantLen int64
}{
{
writeCallback: func(w *avro.Writer) int64 {
w.WriteString("foo")
w.WriteString("avro")

wrote := w.WriteBlockCB(func(w *avro.Writer) int64 {
w.WriteString("foo")
w.WriteString("avro")
return 2
},
want: []byte{0x03, 0x12, 0x06, 0x66, 0x6F, 0x6F, 0x08, 0x61, 0x76, 0x72, 0x6F},
wantLen: 2,
},
{
disableSize: true,
writeCallback: func(w *avro.Writer) int64 {
w.WriteString("foo")
w.WriteString("avro")

return 2
})
return 2
},
want: []byte{0x04, 0x06, 0x66, 0x6F, 0x6F, 0x08, 0x61, 0x76, 0x72, 0x6F},
wantLen: 2,
},
}

for _, test := range tests {
cfg := avro.Config{
DisableBlockSizeHeader: test.disableSize,
}.Freeze()

assert.Equal(t, []byte{0x03, 0x12, 0x06, 0x66, 0x6F, 0x6F, 0x08, 0x61, 0x76, 0x72, 0x6F}, w.Buffer())
assert.Equal(t, int64(2), wrote)
w := avro.NewWriter(nil, 50, avro.WithWriterConfig(cfg))

wrote := w.WriteBlockCB(test.writeCallback)

assert.Equal(t, test.want, w.Buffer())
assert.Equal(t, test.wantLen, wrote)
}
}

type errorWriter struct{}
Expand Down