Skip to content

Commit

Permalink
pkg/apis/nfd: specify a dedicated type for regexp cache
Browse files Browse the repository at this point in the history
Having a dedicated type makes it possible to specify deepcopy functions
for it. We need to do this manually as deepcopy-gen doesn't know how to
create copies of regexps.
  • Loading branch information
marquiz committed Nov 16, 2021
1 parent 7486193 commit af8254f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
21 changes: 21 additions & 0 deletions pkg/apis/nfd/v1alpha1/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ var matchOps = map[MatchOp]struct{}{
MatchIsFalse: struct{}{},
}

type valueRegexpCache []*regexp.Regexp

// NewMatchExpressionSet returns a new MatchExpressionSet instance.
func NewMatchExpressionSet() *MatchExpressionSet {
return &MatchExpressionSet{Expressions: make(Expressions)}
Expand Down Expand Up @@ -432,3 +434,22 @@ 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()
}
}
4 changes: 1 addition & 3 deletions pkg/apis/nfd/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package v1alpha1

import (
"regexp"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -121,7 +119,7 @@ type MatchExpression struct {
Value MatchValue `json:"value,omitempty"`

// valueRe caches compiled regexps for "InRegexp" operator
valueRe []*regexp.Regexp `json:"-"`
valueRe valueRegexpCache `json:"-"`
}

// MatchOp is the match operator that is applied on values when evaluating a
Expand Down
13 changes: 1 addition & 12 deletions pkg/apis/nfd/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af8254f

Please sign in to comment.