mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-31 04:04:51 +00:00
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.
This commit is contained in:
parent
cc6df8e74f
commit
8d40524b88
3 changed files with 9 additions and 42 deletions
|
@ -41,8 +41,6 @@ var matchOps = map[MatchOp]struct{}{
|
|||
MatchIsFalse: {},
|
||||
}
|
||||
|
||||
type valueRegexpCache []*regexp.Regexp
|
||||
|
||||
// CreateMatchExpression creates a new MatchExpression instance. Returns an
|
||||
// error if validation fails.
|
||||
func CreateMatchExpression(op MatchOp, values ...string) (*MatchExpression, error) {
|
||||
|
@ -70,8 +68,6 @@ func newMatchExpression(op MatchOp, values ...string) *MatchExpression {
|
|||
|
||||
// Validate validates the expression.
|
||||
func (m *MatchExpression) Validate() error {
|
||||
m.valueRe = nil
|
||||
|
||||
if _, ok := matchOps[m.Op]; !ok {
|
||||
return fmt.Errorf("invalid Op %q", m.Op)
|
||||
}
|
||||
|
@ -105,13 +101,11 @@ func (m *MatchExpression) Validate() error {
|
|||
if len(m.Value) == 0 {
|
||||
return fmt.Errorf("value must be non-empty for Op %q", m.Op)
|
||||
}
|
||||
m.valueRe = make([]*regexp.Regexp, len(m.Value))
|
||||
for i, v := range m.Value {
|
||||
re, err := regexp.Compile(v)
|
||||
for _, v := range m.Value {
|
||||
_, err := regexp.Compile(v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("value must only contain valid regexps for Op %q (have %v)", m.Op, m.Value)
|
||||
}
|
||||
m.valueRe[i] = re
|
||||
}
|
||||
default:
|
||||
if len(m.Value) == 0 {
|
||||
|
@ -171,18 +165,15 @@ func (m *MatchExpression) Match(valid bool, value interface{}) (bool, error) {
|
|||
if len(m.Value) == 0 {
|
||||
return false, fmt.Errorf("invalid expression, 'value' field must be non-empty for Op %q", m.Op)
|
||||
}
|
||||
if m.valueRe == nil {
|
||||
m.valueRe = make([]*regexp.Regexp, len(m.Value))
|
||||
for i, v := range m.Value {
|
||||
re, err := regexp.Compile(v)
|
||||
if err != nil {
|
||||
m.valueRe = nil
|
||||
return false, fmt.Errorf("invalid expressiom, 'value' field must only contain valid regexps for Op %q (have %v)", m.Op, m.Value)
|
||||
}
|
||||
m.valueRe[i] = re
|
||||
valueRe := make([]*regexp.Regexp, len(m.Value))
|
||||
for i, v := range m.Value {
|
||||
re, err := regexp.Compile(v)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("invalid expressiom, 'value' field must only contain valid regexps for Op %q (have %v)", m.Op, m.Value)
|
||||
}
|
||||
valueRe[i] = re
|
||||
}
|
||||
for _, re := range m.valueRe {
|
||||
for _, re := range valueRe {
|
||||
if re.MatchString(value) {
|
||||
return true, nil
|
||||
}
|
||||
|
@ -490,23 +481,3 @@ func (m *MatchValue) UnmarshalJSON(data []byte) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeepCopy supplements the auto-generated code
|
||||
func (in *valueRegexpCache) DeepCopy() *valueRegexpCache {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(valueRegexpCache)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is a stub to augment the auto-generated code
|
||||
//
|
||||
//nolint:staticcheck // re.Copy is deprecated but we want to use it here
|
||||
func (in *valueRegexpCache) DeepCopyInto(out *valueRegexpCache) {
|
||||
*out = make(valueRegexpCache, len(*in))
|
||||
for i, re := range *in {
|
||||
(*out)[i] = re.Copy()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -226,9 +226,6 @@ type MatchExpression struct {
|
|||
// In other cases Value should contain at least one element.
|
||||
// +optional
|
||||
Value MatchValue `json:"value,omitempty"`
|
||||
|
||||
// valueRe caches compiled regexps for "InRegexp" operator
|
||||
valueRe valueRegexpCache `json:"-"`
|
||||
}
|
||||
|
||||
// MatchOp is the match operator that is applied on values when evaluating a
|
||||
|
|
|
@ -215,7 +215,6 @@ func (in *MatchExpression) DeepCopyInto(out *MatchExpression) {
|
|||
*out = make(MatchValue, len(*in))
|
||||
copy(*out, *in)
|
||||
}
|
||||
in.valueRe.DeepCopyInto(&out.valueRe)
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new MatchExpression.
|
||||
|
|
Loading…
Add table
Reference in a new issue