diff --git a/codec_record.go b/codec_record.go index 9638a709..dd2edfd7 100644 --- a/codec_record.go +++ b/codec_record.go @@ -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} diff --git a/encoder_record_test.go b/encoder_record_test.go index 73c28e4b..1aba99bc 100644 --- a/encoder_record_test.go +++ b/encoder_record_test.go @@ -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() @@ -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()