Skip to content

Commit

Permalink
Use the 'eq' operator for regex and equality.
Browse files Browse the repository at this point in the history
GCE supports using 'eq' for regex matches and '=' for
exact matches. However, when using multiple filters, they need to
all use 'eq' or all '='. This commit changes the code to always use
'eq'.
  • Loading branch information
prameshj authored and bowei committed Aug 22, 2019
1 parent 079a4d3 commit 27a4ced
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
23 changes: 13 additions & 10 deletions pkg/cloud/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,32 @@ var (
None *F
)

// Regexp returns a filter for fieldName ~ regexp v.
// Regexp returns a filter for fieldName eq regexp v.
func Regexp(fieldName, v string) *F {
return (&F{}).AndRegexp(fieldName, v)
}

// NotRegexp returns a filter for fieldName !~ regexp v.
// NotRegexp returns a filter for fieldName ne regexp v.
func NotRegexp(fieldName, v string) *F {
return (&F{}).AndNotRegexp(fieldName, v)
}

// EqualInt returns a filter for fieldName = v.
// EqualInt returns a filter for fieldName eq v.
func EqualInt(fieldName string, v int) *F {
return (&F{}).AndEqualInt(fieldName, v)
}

// NotEqualInt returns a filter for fieldName != v.
// NotEqualInt returns a filter for fieldName ne v.
func NotEqualInt(fieldName string, v int) *F {
return (&F{}).AndNotEqualInt(fieldName, v)
}

// EqualBool returns a filter for fieldName = v.
// EqualBool returns a filter for fieldName eq v.
func EqualBool(fieldName string, v bool) *F {
return (&F{}).AndEqualBool(fieldName, v)
}

// NotEqualBool returns a filter for fieldName != v.
// NotEqualBool returns a filter for fieldName ne v.
func NotEqualBool(fieldName string, v bool) *F {
return (&F{}).AndNotEqualBool(fieldName, v)
}
Expand Down Expand Up @@ -199,13 +199,16 @@ func (fp *filterPredicate) String() string {
var op string
switch fp.op {
case regexpEquals:
op = "~"
// GCE API maps regexp comparison to 'eq'
op = "eq"
case regexpNotEquals:
op = "!~"
op = "ne"
// Since GCE API does not allow using a mix of 'eq' and '=' operators,
// we use 'eq' everywhere
case equals:
op = "="
op = "eq"
case notEquals:
op = "!="
op = "ne"
default:
op = "invalidOp"
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/cloud/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ func TestFilterToString(t *testing.T) {
f *F
want string
}{
{Regexp("field1", "abc"), `field1 ~ abc`},
{NotRegexp("field1", "abc"), `field1 !~ abc`},
{EqualInt("field1", 13), "field1 = 13"},
{NotEqualInt("field1", 13), "field1 != 13"},
{EqualBool("field1", true), "field1 = true"},
{NotEqualBool("field1", true), "field1 != true"},
{Regexp("field1", "abc").AndRegexp("field2", "def"), `(field1 ~ abc) (field2 ~ def)`},
{Regexp("field1", "abc").AndNotEqualInt("field2", 17), `(field1 ~ abc) (field2 != 17)`},
{Regexp("field1", "abc").And(EqualInt("field2", 17)), `(field1 ~ abc) (field2 = 17)`},
{Regexp("field1", "abc"), `field1 eq abc`},
{NotRegexp("field1", "abc"), `field1 ne abc`},
{EqualInt("field1", 13), "field1 eq 13"},
{NotEqualInt("field1", 13), "field1 ne 13"},
{EqualBool("field1", true), "field1 eq true"},
{NotEqualBool("field1", true), "field1 ne true"},
{Regexp("field1", "abc").AndRegexp("field2", "def"), `(field1 eq abc) (field2 eq def)`},
{Regexp("field1", "abc").AndNotEqualInt("field2", 17), `(field1 eq abc) (field2 ne 17)`},
{Regexp("field1", "abc").And(EqualInt("field2", 17)), `(field1 eq abc) (field2 eq 17)`},
} {
if tc.f.String() != tc.want {
t.Errorf("filter %#v String() = %q, want %q", tc.f, tc.f.String(), tc.want)
Expand Down

0 comments on commit 27a4ced

Please sign in to comment.