Skip to content
Open
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
2 changes: 1 addition & 1 deletion common/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (dt VPAType) String() string {
}

type Query struct {
Predicate string `json:"predicate" required:"true"`
Predicate string `json:"predicate"`
Offset int64 `json:"offset"`
Limit int64 `json:"limit"`
}
Expand Down
5 changes: 5 additions & 0 deletions common/query/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ import (
// ParsePredicate parses a predicate string according to the grammar defined in Query.g4.
// This function builds the predicate structure based on the ANTLR parse tree.
// NOTE if the ANTLR grammar changes, the visiting logic in predicateBuilder will need to be updated.
// An empty or whitespace-only input is treated as a match-all predicate (no filter).
func ParsePredicate(input string) (Predicate, error) {
if strings.TrimSpace(input) == "" {
return &MatchAllPredicate{}, nil
}

is := antlr.NewInputStream(input)
lexer := NewQueryLexer(is)
stream := antlr.NewCommonTokenStream(lexer, 0)
Expand Down
36 changes: 23 additions & 13 deletions common/query/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,32 +421,42 @@ func TestParse_CaseInsensitiveKeywords(t *testing.T) {
// TestParse_EdgeCases tests edge cases and error handling
func TestParse_EdgeCases(t *testing.T) {
tests := []struct {
name string
input string
expectError bool
name string
input string
expectError bool
expectMatchAll bool
}{
{
name: "Empty string",
input: "",
expectError: true,
name: "Empty string returns match-all",
input: "",
expectError: false,
expectMatchAll: true,
},
{
name: "Whitespace only",
input: " ",
expectError: true,
name: "Whitespace only returns match-all",
input: " ",
expectError: false,
expectMatchAll: true,
},
{
name: "Valid AND",
input: "Name = 'Alice' AND Age > 25",
expectError: false,
name: "Valid AND",
input: "Name = 'Alice' AND Age > 25",
expectError: false,
expectMatchAll: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := ParsePredicate(tt.input)
pred, err := ParsePredicate(tt.input)
if (err != nil) != tt.expectError {
t.Errorf("ParsePredicate() error = %v, expectError = %v", err, tt.expectError)
return
}
if tt.expectMatchAll {
if _, ok := pred.(*MatchAllPredicate); !ok {
t.Errorf("expected *MatchAllPredicate, got %T", pred)
}
}
})
}
Expand Down