Update name and docs

This commit is contained in:
John Howard 2022-11-14 08:39:04 -08:00
parent 5b310565fa
commit 4e5487de13
3 changed files with 24 additions and 20 deletions

View File

@ -77,7 +77,7 @@ func (ls Set) AsValidatedSelector() (Selector, error) {
// perform any validation. // perform any validation.
// According to our measurements this is significantly faster // According to our measurements this is significantly faster
// in codepaths that matter at high scale. // in codepaths that matter at high scale.
// Note: this method copies the Set; if the Set is immutable, consider wrapping it with SetSelector // Note: this method copies the Set; if the Set is immutable, consider wrapping it with ValidatedSetSelector
// instead, which does not copy. // instead, which does not copy.
func (ls Set) AsSelectorPreValidated() Selector { func (ls Set) AsSelectorPreValidated() Selector {
return SelectorFromValidatedSet(ls) return SelectorFromValidatedSet(ls)

View File

@ -942,7 +942,7 @@ func ValidatedSelectorFromSet(ls Set) (Selector, error) {
// SelectorFromValidatedSet returns a Selector which will match exactly the given Set. // SelectorFromValidatedSet returns a Selector which will match exactly the given Set.
// A nil and empty Sets are considered equivalent to Everything(). // A nil and empty Sets are considered equivalent to Everything().
// It assumes that Set is already validated and doesn't do any validation. // It assumes that Set is already validated and doesn't do any validation.
// Note: this method copies the Set; if the Set is immutable, consider wrapping it with SetSelector // Note: this method copies the Set; if the Set is immutable, consider wrapping it with ValidatedSetSelector
// instead, which does not copy. // instead, which does not copy.
func SelectorFromValidatedSet(ls Set) Selector { func SelectorFromValidatedSet(ls Set) Selector {
if ls == nil || len(ls) == 0 { if ls == nil || len(ls) == 0 {
@ -966,15 +966,19 @@ func ParseToRequirements(selector string, opts ...field.PathOption) ([]Requireme
return parse(selector, field.ToPath(opts...)) return parse(selector, field.ToPath(opts...))
} }
// SetSelector wraps a Set, allowing it to implement the Selector interface. Unlike // ValidatedSetSelector wraps a Set, allowing it to implement the Selector interface. Unlike
// Set.AsSelectorPreValidated (which copies the input Set), this type simply wraps the underlying // Set.AsSelectorPreValidated (which copies the input Set), this type simply wraps the underlying
// Set. As a result, it is substantially more efficient, but requires the caller to not mutate the // Set. As a result, it is substantially more efficient. A nil and empty Sets are considered
// Set. // equivalent to Everything().
// None of the Selector methods mutate the underlying Set, but Add() and Requirements() convert to the //
// less optimized version. // Callers MUST ensure the underlying Set is not mutated, and that it is already validated. If these
type SetSelector Set // constraints are not met, Set.AsValidatedSelector should be preferred
//
// None of the Selector methods mutate the underlying Set, but Add() and Requirements() convert to
// the less optimized version.
type ValidatedSetSelector Set
func (s SetSelector) Matches(labels Labels) bool { func (s ValidatedSetSelector) Matches(labels Labels) bool {
for k, v := range s { for k, v := range s {
if !labels.Has(k) || v != labels.Get(k) { if !labels.Has(k) || v != labels.Get(k) {
return false return false
@ -983,11 +987,11 @@ func (s SetSelector) Matches(labels Labels) bool {
return true return true
} }
func (s SetSelector) Empty() bool { func (s ValidatedSetSelector) Empty() bool {
return len(s) == 0 return len(s) == 0
} }
func (s SetSelector) String() string { func (s ValidatedSetSelector) String() string {
keys := make([]string, 0, len(s)) keys := make([]string, 0, len(s))
for k := range s { for k := range s {
keys = append(keys, k) keys = append(keys, k)
@ -1008,29 +1012,29 @@ func (s SetSelector) String() string {
return b.String() return b.String()
} }
func (s SetSelector) Add(r ...Requirement) Selector { func (s ValidatedSetSelector) Add(r ...Requirement) Selector {
return s.toFullSelector().Add(r...) return s.toFullSelector().Add(r...)
} }
func (s SetSelector) Requirements() (requirements Requirements, selectable bool) { func (s ValidatedSetSelector) Requirements() (requirements Requirements, selectable bool) {
return s.toFullSelector().Requirements() return s.toFullSelector().Requirements()
} }
func (s SetSelector) DeepCopySelector() Selector { func (s ValidatedSetSelector) DeepCopySelector() Selector {
res := make(SetSelector, len(s)) res := make(ValidatedSetSelector, len(s))
for k, v := range s { for k, v := range s {
res[k] = v res[k] = v
} }
return res return res
} }
func (s SetSelector) RequiresExactMatch(label string) (value string, found bool) { func (s ValidatedSetSelector) RequiresExactMatch(label string) (value string, found bool) {
v, f := s[label] v, f := s[label]
return v, f return v, f
} }
func (s SetSelector) toFullSelector() Selector { func (s ValidatedSetSelector) toFullSelector() Selector {
return SelectorFromValidatedSet(Set(s)) return SelectorFromValidatedSet(Set(s))
} }
var _ Selector = SetSelector{} var _ Selector = ValidatedSetSelector{}

View File

@ -838,7 +838,7 @@ func BenchmarkSetSelector(b *testing.B) {
}) })
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
s := SetSelector(set) s := ValidatedSetSelector(set)
if s.Empty() { if s.Empty() {
b.Errorf("Unexpected selector") b.Errorf("Unexpected selector")
} }
@ -869,7 +869,7 @@ func TestSetSelectorString(t *testing.T) {
for _, tt := range cases { for _, tt := range cases {
t.Run(tt.out, func(t *testing.T) { t.Run(tt.out, func(t *testing.T) {
if got := SetSelector(tt.set).String(); tt.out != got { if got := ValidatedSetSelector(tt.set).String(); tt.out != got {
t.Fatalf("expected %v, got %v", tt.out, got) t.Fatalf("expected %v, got %v", tt.out, got)
} }
}) })