change strValues's type to util.StringSet; rename Comparator to Operator; small syntax fix

This commit is contained in:
Meir Fischer 2014-07-31 23:56:37 -04:00
parent 80b1fa000e
commit 9076e78654
2 changed files with 26 additions and 28 deletions

View File

@ -20,6 +20,8 @@ import (
"fmt" "fmt"
"sort" "sort"
"strings" "strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
) )
// Selector represents a label selector. // Selector represents a label selector.
@ -105,40 +107,34 @@ func (t andTerm) String() string {
return strings.Join(terms, ",") return strings.Join(terms, ",")
} }
type Comparator int // Operator represents a key's relationship
// to a set of values in a Requirement.
// TODO: Should also represent key's existence.
type Operator int
const ( const (
IN Comparator = iota + 1 IN Operator = iota + 1
NOT_IN NOT_IN
) )
// only not named 'Selector' due to name // LabelSelector only not named 'Selector' due
// conflict until Selector is deprecated // to name conflict until Selector is deprecated.
type LabelSelector struct { type LabelSelector struct {
Requirements []Requirement Requirements []Requirement
} }
type Requirement struct { type Requirement struct {
key string key string
comparator Comparator operator Operator
strValues []string strValues util.StringSet
}
func (r *Requirement) containsStr(value string) bool {
for _, x := range r.strValues {
if value == x {
return true
}
}
return false
} }
func (r *Requirement) Matches(ls Labels) bool { func (r *Requirement) Matches(ls Labels) bool {
switch r.comparator { switch r.operator {
case IN: case IN:
return r.containsStr(ls.Get(r.key)) return r.strValues.Has(ls.Get(r.key))
case NOT_IN: case NOT_IN:
return !r.containsStr(ls.Get(r.key)) return !r.strValues.Has(ls.Get(r.key))
default: default:
return false return false
} }
@ -146,7 +142,7 @@ func (r *Requirement) Matches(ls Labels) bool {
func (sg *LabelSelector) Matches(ls Labels) bool { func (sg *LabelSelector) Matches(ls Labels) bool {
for _, req := range sg.Requirements { for _, req := range sg.Requirements {
if sat := req.Matches(ls); !sat { if !req.Matches(ls) {
return false return false
} }
} }

View File

@ -18,6 +18,8 @@ package labels
import ( import (
"testing" "testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
) )
func TestSelectorParse(t *testing.T) { func TestSelectorParse(t *testing.T) {
@ -178,10 +180,10 @@ func expectNoMatchRequirement(t *testing.T, req Requirement, ls Set) {
func TestRequirementMatches(t *testing.T) { func TestRequirementMatches(t *testing.T) {
s := Set{"x": "foo", "y": "baz"} s := Set{"x": "foo", "y": "baz"}
a := Requirement{key: "x", comparator: IN, strValues: []string{"foo"}} a := Requirement{key: "x", operator: IN, strValues: util.NewStringSet("foo")}
b := Requirement{key: "x", comparator: NOT_IN, strValues: []string{"beta"}} b := Requirement{key: "x", operator: NOT_IN, strValues: util.NewStringSet("beta")}
c := Requirement{key: "y", comparator: IN, strValues: nil} c := Requirement{key: "y", operator: IN, strValues: nil}
d := Requirement{key: "y", strValues: []string{"foo"}} d := Requirement{key: "y", strValues: util.NewStringSet("foo")}
expectMatchRequirement(t, a, s) expectMatchRequirement(t, a, s)
expectMatchRequirement(t, b, s) expectMatchRequirement(t, b, s)
expectNoMatchRequirement(t, c, s) expectNoMatchRequirement(t, c, s)
@ -204,14 +206,14 @@ func TestLabelSelectorMatches(t *testing.T) {
s := Set{"x": "foo", "y": "baz"} s := Set{"x": "foo", "y": "baz"}
allMatch := LabelSelector{ allMatch := LabelSelector{
Requirements: []Requirement{ Requirements: []Requirement{
{key: "x", comparator: IN, strValues: []string{"foo"}}, {key: "x", operator: IN, strValues: util.NewStringSet("foo")},
{key: "y", comparator: NOT_IN, strValues: []string{"alpha"}}, {key: "y", operator: NOT_IN, strValues: util.NewStringSet("alpha")},
}, },
} }
singleNonMatch := LabelSelector{ singleNonMatch := LabelSelector{
Requirements: []Requirement{ Requirements: []Requirement{
{key: "x", comparator: IN, strValues: []string{"foo"}}, {key: "x", operator: IN, strValues: util.NewStringSet("foo")},
{key: "y", comparator: IN, strValues: []string{"alpha"}}, {key: "y", operator: IN, strValues: util.NewStringSet("alpha")},
}, },
} }
expectMatchLabSelector(t, allMatch, s) expectMatchLabSelector(t, allMatch, s)