Skip to content

Commit

Permalink
fix: encoding a struct with a sub-record field default value (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
redaLaanait authored Nov 5, 2023
1 parent 58cadd0 commit 8419cd2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
7 changes: 6 additions & 1 deletion codec_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,14 @@ func encoderOfStruct(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValEnc
}

defaultType := reflect2.TypeOf(def)
defaultEncoder := encoderOfType(cfg, field.Type(), defaultType)
if defaultType.LikePtr() {
defaultEncoder = &onePtrEncoder{defaultEncoder}
}

fields = append(fields, &structFieldEncoder{
defaultPtr: reflect2.PtrOf(def),
encoder: encoderOfType(cfg, field.Type(), defaultType),
encoder: defaultEncoder,
})
}
return &structEncoder{typ: typ, fields: fields}
Expand Down
73 changes: 73 additions & 0 deletions encoder_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,42 @@ func TestEncoder_RecordStructPartialWithNullDefault(t *testing.T) {
assert.Equal(t, []byte{0x00, 0x06, 0x66, 0x6f, 0x6f}, buf.Bytes())
}

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

_, err := avro.Parse(`{
"type": "record",
"name": "test",
"fields" : [
{"name": "a", "type": "long"},
{"name": "b", "type": "string"}
]
}`)
require.NoError(t, err)

schema := `{
"type": "record",
"name": "parent",
"fields" : [
{
"name": "a",
"type": "test",
"default": {"a": 1000, "b": "def b"}
},
{"name": "b", "type": "string"}
]
}`
obj := TestPartialRecord{B: "foo"}
buf := &bytes.Buffer{}
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)

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

assert.Equal(t, []byte{0xd0, 0xf, 0xa, 0x64, 0x65, 0x66, 0x20, 0x62, 0x6, 0x66, 0x6f, 0x6f}, buf.Bytes())
}

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

Expand Down Expand Up @@ -410,6 +446,43 @@ func TestEncoder_RecordMapWithDefault(t *testing.T) {
assert.Equal(t, []byte{0x36, 0x06, 0x66, 0x6f, 0x6f}, buf.Bytes())
}

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

_, err := avro.Parse(`{
"type": "record",
"name": "test",
"fields" : [
{"name": "a", "type": "long"},
{"name": "b", "type": "string"}
]
}`)
require.NoError(t, err)

schema := `{
"type": "record",
"name": "parent",
"fields" : [
{
"name": "a",
"type": "test",
"default": {"a": 1000, "b": "def b"}
},
{"name": "b", "type": "string"}
]
}`

obj := map[string]any{"b": "foo"}
buf := &bytes.Buffer{}
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)

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

assert.Equal(t, []byte{0xd0, 0xf, 0xa, 0x64, 0x65, 0x66, 0x20, 0x62, 0x6, 0x66, 0x6f, 0x6f}, buf.Bytes())
}

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

Expand Down

0 comments on commit 8419cd2

Please sign in to comment.