Skip to content

Commit 8d40524

Browse files
committed
apis/nfd: drop the private regexp caching field
Drop the private field for caching parsed regexp from the MatchExpression type. This tidies up the API type definition and not so tied with particular implementation details. The change also elimiates potential concurrency problems as no locking is in place in the API types. If caching will be desired in the future, it's better to do it properly in a separate package, not directly in the API types.
1 parent cc6df8e commit 8d40524

File tree

3 files changed

+9
-42
lines changed

3 files changed

+9
-42
lines changed

pkg/apis/nfd/v1alpha1/expression.go

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ var matchOps = map[MatchOp]struct{}{
4141
MatchIsFalse: {},
4242
}
4343

44-
type valueRegexpCache []*regexp.Regexp
45-
4644
// CreateMatchExpression creates a new MatchExpression instance. Returns an
4745
// error if validation fails.
4846
func CreateMatchExpression(op MatchOp, values ...string) (*MatchExpression, error) {
@@ -70,8 +68,6 @@ func newMatchExpression(op MatchOp, values ...string) *MatchExpression {
7068

7169
// Validate validates the expression.
7270
func (m *MatchExpression) Validate() error {
73-
m.valueRe = nil
74-
7571
if _, ok := matchOps[m.Op]; !ok {
7672
return fmt.Errorf("invalid Op %q", m.Op)
7773
}
@@ -105,13 +101,11 @@ func (m *MatchExpression) Validate() error {
105101
if len(m.Value) == 0 {
106102
return fmt.Errorf("value must be non-empty for Op %q", m.Op)
107103
}
108-
m.valueRe = make([]*regexp.Regexp, len(m.Value))
109-
for i, v := range m.Value {
110-
re, err := regexp.Compile(v)
104+
for _, v := range m.Value {
105+
_, err := regexp.Compile(v)
111106
if err != nil {
112107
return fmt.Errorf("value must only contain valid regexps for Op %q (have %v)", m.Op, m.Value)
113108
}
114-
m.valueRe[i] = re
115109
}
116110
default:
117111
if len(m.Value) == 0 {
@@ -171,18 +165,15 @@ func (m *MatchExpression) Match(valid bool, value interface{}) (bool, error) {
171165
if len(m.Value) == 0 {
172166
return false, fmt.Errorf("invalid expression, 'value' field must be non-empty for Op %q", m.Op)
173167
}
174-
if m.valueRe == nil {
175-
m.valueRe = make([]*regexp.Regexp, len(m.Value))
176-
for i, v := range m.Value {
177-
re, err := regexp.Compile(v)
178-
if err != nil {
179-
m.valueRe = nil
180-
return false, fmt.Errorf("invalid expressiom, 'value' field must only contain valid regexps for Op %q (have %v)", m.Op, m.Value)
181-
}
182-
m.valueRe[i] = re
168+
valueRe := make([]*regexp.Regexp, len(m.Value))
169+
for i, v := range m.Value {
170+
re, err := regexp.Compile(v)
171+
if err != nil {
172+
return false, fmt.Errorf("invalid expressiom, 'value' field must only contain valid regexps for Op %q (have %v)", m.Op, m.Value)
183173
}
174+
valueRe[i] = re
184175
}
185-
for _, re := range m.valueRe {
176+
for _, re := range valueRe {
186177
if re.MatchString(value) {
187178
return true, nil
188179
}
@@ -490,23 +481,3 @@ func (m *MatchValue) UnmarshalJSON(data []byte) error {
490481

491482
return nil
492483
}
493-
494-
// DeepCopy supplements the auto-generated code
495-
func (in *valueRegexpCache) DeepCopy() *valueRegexpCache {
496-
if in == nil {
497-
return nil
498-
}
499-
out := new(valueRegexpCache)
500-
in.DeepCopyInto(out)
501-
return out
502-
}
503-
504-
// DeepCopyInto is a stub to augment the auto-generated code
505-
//
506-
//nolint:staticcheck // re.Copy is deprecated but we want to use it here
507-
func (in *valueRegexpCache) DeepCopyInto(out *valueRegexpCache) {
508-
*out = make(valueRegexpCache, len(*in))
509-
for i, re := range *in {
510-
(*out)[i] = re.Copy()
511-
}
512-
}

pkg/apis/nfd/v1alpha1/types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,6 @@ type MatchExpression struct {
226226
// In other cases Value should contain at least one element.
227227
// +optional
228228
Value MatchValue `json:"value,omitempty"`
229-
230-
// valueRe caches compiled regexps for "InRegexp" operator
231-
valueRe valueRegexpCache `json:"-"`
232229
}
233230

234231
// MatchOp is the match operator that is applied on values when evaluating a

pkg/apis/nfd/v1alpha1/zz_generated.deepcopy.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)