Allow field/label users to get info from fields

Allows a consumer to get at the information stored in the field selector
for querying an underlying data store.  Not generic, but offers a
simple start.
This commit is contained in:
Clayton Coleman
2014-08-14 15:49:03 -04:00
parent 2606ece6e9
commit ae698bcff8
2 changed files with 63 additions and 1 deletions

View File

@@ -32,6 +32,12 @@ type Selector interface {
// Empty returns true if this selector does not restrict the selection space.
Empty() bool
// RequiresExactMatch allows a caller to introspect whether a given selector
// requires a single specific label to be set, and if so returns the value it
// requires.
// TODO: expand this to be more general
RequiresExactMatch(label string) (value string, found bool)
// String returns a human readable string that represents this selector.
String() string
}
@@ -53,6 +59,13 @@ func (t *hasTerm) Empty() bool {
return false
}
func (t *hasTerm) RequiresExactMatch(label string) (value string, found bool) {
if t.label == label {
return t.value, true
}
return "", false
}
func (t *hasTerm) String() string {
return fmt.Sprintf("%v=%v", t.label, t.value)
}
@@ -69,6 +82,10 @@ func (t *notHasTerm) Empty() bool {
return false
}
func (t *notHasTerm) RequiresExactMatch(label string) (value string, found bool) {
return "", false
}
func (t *notHasTerm) String() string {
return fmt.Sprintf("%v!=%v", t.label, t.value)
}
@@ -99,6 +116,18 @@ func (t andTerm) Empty() bool {
return true
}
func (t andTerm) RequiresExactMatch(label string) (string, bool) {
if t == nil || len([]Selector(t)) == 0 {
return "", false
}
for i := range t {
if value, found := t[i].RequiresExactMatch(label); found {
return value, found
}
}
return "", false
}
func (t andTerm) String() string {
var terms []string
for _, q := range t {
@@ -173,7 +202,7 @@ func SelectorFromSet(ls Set) Selector {
return andTerm(items)
}
// ParseSelector takes a string repsenting a selector and returns an
// ParseSelector takes a string representing a selector and returns an
// object suitable for matching, or an error.
func ParseSelector(selector string) (Selector, error) {
parts := strings.Split(selector, ",")