Skip to content

Commit

Permalink
feat: allow block headers to contain only length (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
meandnano authored Jun 8, 2023
1 parent 8578d51 commit 88b071f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
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

0 comments on commit 88b071f

Please sign in to comment.