Skip to content

Commit

Permalink
fix: fixed data race in result (schemata caching)
Browse files Browse the repository at this point in the history
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
  • Loading branch information
fredbi committed Mar 4, 2024
1 parent e818230 commit 837325b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
18 changes: 16 additions & 2 deletions result.go
Expand Up @@ -220,12 +220,26 @@ func (r *Result) mergeForSlice(slice reflect.Value, i int, other *Result) *Resul
if r.itemSchemata == nil {
r.itemSchemata = make([]itemSchemata, slice.Len())
}
// clone other, as other is about to be redeemed to the pool TODO(fred)
var clone schemata
if other.rootObjectSchemata.one != nil {
*clone.one = *other.rootObjectSchemata.one
}
if other.rootObjectSchemata.multiple != nil {
clone.multiple = make([]*spec.Schema, len(other.rootObjectSchemata.multiple))
for idx := 0; i < len(other.rootObjectSchemata.multiple); idx++ {
s := new(spec.Schema)
*s = *other.rootObjectSchemata.multiple[idx]
clone.multiple[idx] = s
}
}
r.itemSchemata = append(r.itemSchemata, itemSchemata{
slice: slice,
index: i,
schemata: other.rootObjectSchemata,
schemata: clone,
})
}

if other.wantsRedeemOnMerge {
pools.poolOfResults.RedeemResult(other)
}
Expand Down Expand Up @@ -502,7 +516,7 @@ func (s *schemata) Slice() []*spec.Schema {
return s.multiple
}

// appendSchemata appends the schemata in other to s. It mutated s in-place.
// appendSchemata appends the schemata in other to s. It mutates s in-place.
func (s *schemata) Append(other schemata) {
if other.one == nil && len(other.multiple) == 0 {
return
Expand Down
10 changes: 0 additions & 10 deletions schema_props_test.go
Expand Up @@ -51,16 +51,6 @@ func TestSchemaPropsValidator_EdgeCases(t *testing.T) {
res := s.Validate(data)
require.NotNil(t, res)
require.Empty(t, res.Errors)

/* TODO(fred)
t.Run("validator should run once", func(t *testing.T) {
// we should not do that: the pool chain list is populated with a duplicate: needs a reset
t.Cleanup(resetPools)
require.NotPanics(t, func() {
_ = s.Validate(data)
})
})
*/
})

t.Run("should NOT validate unformatted string", func(t *testing.T) {
Expand Down

0 comments on commit 837325b

Please sign in to comment.