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 passing in custom options for OCF decoder. #460

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions ocf/ocf.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ type Decoder struct {

// NewDecoder returns a new decoder that reads from reader r.
func NewDecoder(r io.Reader) (*Decoder, error) {
reader := avro.NewReader(r, 1024)
return NewDecoderWithConfig(r, avro.DefaultConfig)
}

// NewDecoderWithConfig returns a new decoder that uses the provided config.
func NewDecoderWithConfig(r io.Reader, conf avro.API) (*Decoder, error) {
nrwiersma marked this conversation as resolved.
Show resolved Hide resolved
reader := avro.NewReader(r, 1024, avro.WithReaderConfig(conf))

h, err := readHeader(reader)
if err != nil {
Expand All @@ -67,7 +72,7 @@ func NewDecoder(r io.Reader) (*Decoder, error) {
return &Decoder{
reader: reader,
resetReader: decReader,
decoder: avro.NewDecoderForSchema(h.Schema, decReader),
decoder: conf.NewDecoder(h.Schema, decReader),
meta: h.Meta,
sync: h.Sync,
codec: h.Codec,
Expand Down
62 changes: 62 additions & 0 deletions ocf/ocf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"flag"
"io"
"os"
"strings"
"testing"

"github.com/hamba/avro/v2"
Expand Down Expand Up @@ -453,6 +454,67 @@ func TestDecoder_InvalidBlock(t *testing.T) {
assert.Error(t, dec.Error())
}

func TestDecoder_WithConfig(t *testing.T) {
const defaultMax = 1_048_576

unionStr := "union value"
longString := strings.Repeat("a", defaultMax+1)
record := FullRecord{
Strings: []string{"string1", "string2", "string3", "string4", "string5"},
Longs: []int64{1, 2, 3, 4, 5},
Enum: "C",
Map: map[string]int{
"key1": 1,
"key2": 2,
"key3": 3,
"key4": 4,
"key5": 5,
},
Nullable: &unionStr,
Fixed: [16]byte{0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04},
Record: &TestRecord{
Long: 1925639126735,
String: longString,
Int: 666,
Float: 7171.17,
Double: 916734926348163.01973408746523,
Bool: true,
},
}

buf := &bytes.Buffer{}
enc, err := ocf.NewEncoder(schema, buf)
require.NoError(t, err)

err = enc.Encode(record)
require.NoError(t, err)

err = enc.Close()
require.NoError(t, err)

t.Run("Default Fails", func(t *testing.T) {
dec, err := ocf.NewDecoder(bytes.NewReader(buf.Bytes()))
require.NoError(t, err)

var got FullRecord
require.True(t, dec.HasNext())
require.ErrorContains(t, dec.Decode(&got), "size is greater than `Config.MaxByteSliceSize`")
})

t.Run("Custom Config Is Used", func(t *testing.T) {
dec, err := ocf.NewDecoderWithConfig(
bytes.NewReader(buf.Bytes()),
avro.Config{MaxByteSliceSize: defaultMax + 1}.Freeze(),
)
require.NoError(t, err)

var got FullRecord
require.True(t, dec.HasNext())
require.NoError(t, dec.Decode(&got))
require.Equal(t, record, got)
})
}

func TestNewEncoder_InvalidSchema(t *testing.T) {
buf := &bytes.Buffer{}

Expand Down