Allow for selector creation without validation

This commit is contained in:
Wojciech Tyczynski 2016-08-19 09:27:53 +02:00
parent e30c0b8dcd
commit 42aee3ac5e
2 changed files with 25 additions and 0 deletions

View File

@ -61,6 +61,15 @@ func (ls Set) AsSelector() Selector {
return SelectorFromSet(ls)
}
// ValidatedAsSelector converts labels into a selector, but
// assumes that labels are already validated and thus don't
// preform any validation.
// According to our measurements this is significantly faster
// in codepaths that matter at high sccale.
func (ls Set) AsSelectorPreValidated() Selector {
return SelectorFromValidatedSet(ls)
}
// FormatLables convert label map into plain string
func FormatLabels(labelMap map[string]string) string {
l := Set(labelMap).String()

View File

@ -796,6 +796,22 @@ func SelectorFromSet(ls Set) Selector {
return internalSelector(requirements)
}
// SelectorFromValidatedSet returns a Selector which will match exactly the given Set.
// A nil and empty Sets are considered equivalent to Everything().
// It assumes that Set is already validated and doesn't do any validation.
func SelectorFromValidatedSet(ls Set) Selector {
if ls == nil {
return internalSelector{}
}
var requirements internalSelector
for label, value := range ls {
requirements = append(requirements, Requirement{key: label, operator: selection.Equals, strValues: sets.NewString(value)})
}
// sort to have deterministic string representation
sort.Sort(ByKey(requirements))
return internalSelector(requirements)
}
// ParseToRequirements takes a string representing a selector and returns a list of
// requirements. This function is suitable for those callers that perform additional
// processing on selector requirements.