Skip to content

Commit

Permalink
change strValues's type to util.StringSet; rename Comparator to Opera…
Browse files Browse the repository at this point in the history
…tor; small syntax fix
  • Loading branch information
meirf committed Aug 1, 2014
1 parent 80b1fa0 commit 9076e78
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
36 changes: 16 additions & 20 deletions pkg/labels/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"
"sort"
"strings"

"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)

// Selector represents a label selector.
Expand Down Expand Up @@ -105,48 +107,42 @@ func (t andTerm) String() string {
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 (
IN Comparator = iota + 1
IN Operator = iota + 1
NOT_IN
)

// only not named 'Selector' due to name
// conflict until Selector is deprecated
// LabelSelector only not named 'Selector' due
// to name conflict until Selector is deprecated.
type LabelSelector struct {
Requirements []Requirement
}

type Requirement struct {
key string
comparator Comparator
strValues []string
}

func (r *Requirement) containsStr(value string) bool {
for _, x := range r.strValues {
if value == x {
return true
}
}
return false
key string
operator Operator
strValues util.StringSet
}

func (r *Requirement) Matches(ls Labels) bool {
switch r.comparator {
switch r.operator {
case IN:
return r.containsStr(ls.Get(r.key))
return r.strValues.Has(ls.Get(r.key))
case NOT_IN:
return !r.containsStr(ls.Get(r.key))
return !r.strValues.Has(ls.Get(r.key))
default:
return false
}
}

func (sg *LabelSelector) Matches(ls Labels) bool {
for _, req := range sg.Requirements {
if sat := req.Matches(ls); !sat {
if !req.Matches(ls) {
return false
}
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/labels/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package labels

import (
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)

func TestSelectorParse(t *testing.T) {
Expand Down Expand Up @@ -178,10 +180,10 @@ func expectNoMatchRequirement(t *testing.T, req Requirement, ls Set) {

func TestRequirementMatches(t *testing.T) {
s := Set{"x": "foo", "y": "baz"}
a := Requirement{key: "x", comparator: IN, strValues: []string{"foo"}}
b := Requirement{key: "x", comparator: NOT_IN, strValues: []string{"beta"}}
c := Requirement{key: "y", comparator: IN, strValues: nil}
d := Requirement{key: "y", strValues: []string{"foo"}}
a := Requirement{key: "x", operator: IN, strValues: util.NewStringSet("foo")}
b := Requirement{key: "x", operator: NOT_IN, strValues: util.NewStringSet("beta")}
c := Requirement{key: "y", operator: IN, strValues: nil}
d := Requirement{key: "y", strValues: util.NewStringSet("foo")}
expectMatchRequirement(t, a, s)
expectMatchRequirement(t, b, s)
expectNoMatchRequirement(t, c, s)
Expand All @@ -204,14 +206,14 @@ func TestLabelSelectorMatches(t *testing.T) {
s := Set{"x": "foo", "y": "baz"}
allMatch := LabelSelector{
Requirements: []Requirement{
{key: "x", comparator: IN, strValues: []string{"foo"}},
{key: "y", comparator: NOT_IN, strValues: []string{"alpha"}},
{key: "x", operator: IN, strValues: util.NewStringSet("foo")},
{key: "y", operator: NOT_IN, strValues: util.NewStringSet("alpha")},
},
}
singleNonMatch := LabelSelector{
Requirements: []Requirement{
{key: "x", comparator: IN, strValues: []string{"foo"}},
{key: "y", comparator: IN, strValues: []string{"alpha"}},
{key: "x", operator: IN, strValues: util.NewStringSet("foo")},
{key: "y", operator: IN, strValues: util.NewStringSet("alpha")},
},
}
expectMatchLabSelector(t, allMatch, s)
Expand Down

0 comments on commit 9076e78

Please sign in to comment.