Skip to content

Commit

Permalink
Dash should consider as field instead of separator
Browse files Browse the repository at this point in the history
Signed-off-by: Vaishnavi Vejella <vvejella@amazon.com>
  • Loading branch information
vvejell1 committed Oct 24, 2022
1 parent 1e6523f commit 91c4f50
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion filters/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The formal grammar is as follows:
selectors := selector ("," selector)*
selector := fieldpath (operator value)
fieldpath := field ('.' field)*
field := quoted | [A-Za-z] [A-Za-z0-9_]+
field := quoted | [A-Za-z-] [A-Za-z0-9_-]+
operator := "==" | "!=" | "~="
value := quoted | [^\s,]+
quoted := <go string syntax>
Expand Down
2 changes: 1 addition & 1 deletion filters/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func digitVal(ch rune) int {
}

func isFieldRune(r rune) bool {
return (r == '_' || isAlphaRune(r) || isDigitRune(r))
return (r == '-' || r == '_' || isAlphaRune(r) || isDigitRune(r))
}

func isAlphaRune(r rune) bool {
Expand Down
23 changes: 14 additions & 9 deletions filters/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestScanner(t *testing.T) {
},
{
name: "SelectorsWithFieldPaths",
input: "name==value,labels.foo=value,other.bar~=match",
input: "name==value,labels.foo=value,other.bar~=match,labels.io-foo==value,labels.-io-foo==value",
expected: []tokenResult{
{pos: 0, token: tokenField, text: "name"},
{pos: 4, token: tokenOperator, text: "=="},
Expand All @@ -83,7 +83,19 @@ func TestScanner(t *testing.T) {
{pos: 35, token: tokenField, text: "bar"},
{pos: 38, token: tokenOperator, text: "~="},
{pos: 40, token: tokenValue, text: "match"},
{pos: 45, token: tokenEOF},
{pos: 45, token: tokenSeparator, text: ","},
{pos: 46, token: tokenField, text: "labels"},
{pos: 52, token: tokenSeparator, text: "."},
{pos: 53, token: tokenField, text: "io-foo"},
{pos: 59, token: tokenOperator, text: "=="},
{pos: 61, token: tokenValue, text: "value"},
{pos: 66, token: tokenSeparator, text: ","},
{pos: 67, token: tokenField, text: "labels"},
{pos: 73, token: tokenSeparator, text: "."},
{pos: 74, token: tokenField, text: "-io-foo"},
{pos: 81, token: tokenOperator, text: "=="},
{pos: 83, token: tokenValue, text: "value"},
{pos: 88, token: tokenEOF},
},
},
{
Expand Down Expand Up @@ -324,14 +336,12 @@ func TestScanner(t *testing.T) {
t.Run(testcase.name, func(t *testing.T) {
var sc scanner
sc.init(testcase.input)

// If you leave the expected empty, the test case will just print
// out the token stream, which you can paste into the testcase when
// adding new cases.
if len(testcase.expected) == 0 {
fmt.Println("Name", testcase.name)
}

for i := 0; ; i++ {
pos, tok, s := sc.scan()
if len(testcase.expected) == 0 {
Expand All @@ -348,24 +358,19 @@ func TestScanner(t *testing.T) {
if tok == tokenIllegal {
tokv.err = sc.err
}

if tokv != testcase.expected[i] {
t.Fatalf("token unexpected: %v != %v", tokv, testcase.expected[i])
}
}

if tok == tokenEOF {
break
}

}

// make sure we've eof'd
_, tok, _ := sc.scan()
if tok != tokenEOF {
t.Fatal("must consume all input")
}

if len(testcase.expected) == 0 {
t.Fatal("must define expected tokens")
}
Expand Down

0 comments on commit 91c4f50

Please sign in to comment.