Skip to content

Commit

Permalink
fix: record cache fingerprint
Browse files Browse the repository at this point in the history
  • Loading branch information
redaLaanait committed Jan 10, 2024
1 parent 17926ac commit 1dc0276
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
51 changes: 51 additions & 0 deletions config_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,57 @@ func TestConfig_ReusesDecoders(t *testing.T) {
assert.Same(t, dec1, dec2)
}

func TestConfig_ReusesDecoders_WithRecordFieldActions(t *testing.T) {
type testObj struct {
A int64 `avro:"a"`
B string `avro:"b"`
}
sch := `{
"type": "record",
"name": "test",
"fields" : [
{"name": "a", "type": "long"},
{"name": "a", "type": "string", "default": "foo"}
]
}`
typ := reflect2.TypeOfPtr(&testObj{})

t.Run("set default", func(t *testing.T) {
api := Config{
TagKey: "test",
BlockLength: 2,
}.Freeze()
cfg := api.(*frozenConfig)

schema1 := MustParse(sch)
schema2 := MustParse(sch)
schema2.(*RecordSchema).Fields()[1].action = FieldSetDefault

dec1 := cfg.DecoderOf(schema1, typ)
dec2 := cfg.DecoderOf(schema2, typ)

assert.NotSame(t, dec1, dec2)
})

t.Run("ignore", func(t *testing.T) {
api := Config{
TagKey: "test",
BlockLength: 2,
}.Freeze()
cfg := api.(*frozenConfig)

schema1 := MustParse(sch)
schema1.(*RecordSchema).Fields()[0].action = FieldIgnore
schema2 := MustParse(sch)

dec1 := cfg.DecoderOf(schema1, typ)
dec2 := cfg.DecoderOf(schema2, typ)

assert.NotSame(t, dec1, dec2)
})

}

func TestConfig_DisableCache_DoesNotReuseDecoders(t *testing.T) {
type testObj struct {
A int64 `avro:"a"`
Expand Down
3 changes: 3 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ func (s *RecordSchema) CacheFingerprint() [32]byte {
if field.HasDefault() && field.action == FieldSetDefault {
data = append(data, field.Name(), field.Default())
}
if field.action == FieldIgnore {
data = append(data, field.Name()+string(FieldIgnore))
}
}
if len(data) == 0 {
return s.Fingerprint()
Expand Down

0 comments on commit 1dc0276

Please sign in to comment.