Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dash is considered as a field instead as separator #7524

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion filters/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ chomp:
return pos, tokenValue, s.input[pos:s.ppos]
case isFieldRune(ch):
s.scanField()
if ch == '-' || isDigitRune(ch) { // illegal token if it starts with '-' or any digit
s.error("illegal token field")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmcgowan / @estesp - The implementation was less strict than the syntax here. Do we want to make this change in 1.x?

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_]+
operator := "==" | "!=" | "~="
value := quoted | [^\s,]+
quoted := <go string syntax>

If we follow the syntax and update the implementation;

  • It would be better to explain why this is illegal in the error itself.
  • It should handle _ as well.

return pos, tokenIllegal, s.input[pos:s.ppos]
}
return pos, tokenField, s.input[pos:s.ppos]
}

Expand Down Expand Up @@ -249,7 +253,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
34 changes: 32 additions & 2 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",
expected: []tokenResult{
{pos: 0, token: tokenField, text: "name"},
{pos: 4, token: tokenOperator, text: "=="},
Expand All @@ -83,7 +83,13 @@ 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: tokenEOF},
},
},
{
Expand Down Expand Up @@ -310,6 +316,30 @@ func TestScanner(t *testing.T) {
{pos: 11, token: tokenEOF},
},
},
{
name: "IllegalTokenFiledWithNumber",
input: `labels.1io-foo==value`,
expected: []tokenResult{
{pos: 0, token: tokenField, text: "labels"},
{pos: 6, token: tokenSeparator, text: "."},
{pos: 7, token: tokenIllegal, text: `1io-foo`, err: "illegal token field"},
{pos: 14, token: tokenOperator, text: "=="},
{pos: 16, token: tokenValue, text: "value"},
{pos: 21, token: tokenEOF},
},
},
{
name: "IllegalTokenField",
input: `labels.-io-foo==value`,
expected: []tokenResult{
{pos: 0, token: tokenField, text: "labels"},
{pos: 6, token: tokenSeparator, text: "."},
{pos: 7, token: tokenIllegal, text: `-io-foo`, err: "illegal token field"},
{pos: 14, token: tokenOperator, text: "=="},
{pos: 16, token: tokenValue, text: "value"},
{pos: 21, token: tokenEOF},
},
},
{
name: "IllegalNumericEscapeSequence",
input: `labels."\xaz"`,
Expand Down