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

Fixes the pattern parser validation. #4308

Merged
merged 1 commit into from
Sep 10, 2021
Merged
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
25 changes: 14 additions & 11 deletions pkg/logql/log/pattern/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ func (e expr) validate() error {
if !e.hasCapture() {
return ErrNoCapture
}
// if there is at least 2 node, verify that none are consecutive.
if len(e) >= 2 {
for i := 0; i < len(e); i = i + 2 {
if i+1 >= len(e) {
break
}
if _, ok := e[i].(capture); ok {
if _, ok := e[i+1].(capture); ok {
return fmt.Errorf("found consecutive capture: %w", ErrInvalidExpr)
}
// Consecutive captures are not allowed.
for i, n := range e {
if i+1 >= len(e) {
break
}
if _, ok := n.(capture); ok {
if _, ok := e[i+1].(capture); ok {
return fmt.Errorf("found consecutive capture '%s': %w", n.String()+e[i+1].String(), ErrInvalidExpr)
}
}
}

caps := e.captures()
uniq := map[string]struct{}{}
for _, c := range caps {
Expand All @@ -46,7 +45,7 @@ func (e expr) validate() error {
func (e expr) captures() (captures []string) {
for _, n := range e {
if c, ok := n.(capture); ok && !c.isUnamed() {
captures = append(captures, c.String())
captures = append(captures, c.Name())
}
}
return
Expand All @@ -59,6 +58,10 @@ func (e expr) captureCount() (count int) {
type capture string

func (c capture) String() string {
return "<" + string(c) + ">"
}

func (c capture) Name() string {
return string(c)
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/logql/log/pattern/pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ func Test_Error(t *testing.T) {
{"<_>", ErrNoCapture},
{"foo <_> bar <_>", ErrNoCapture},
{"foo bar buzz", ErrNoCapture},
{"<f><f>", fmt.Errorf("found consecutive capture: %w", ErrInvalidExpr)},
{"<f> f<d><b>", fmt.Errorf("found consecutive capture: %w", ErrInvalidExpr)},
{"<f><f>", fmt.Errorf("found consecutive capture '<f><f>': %w", ErrInvalidExpr)},
{"<f> f<d><b>", fmt.Errorf("found consecutive capture '<d><b>': %w", ErrInvalidExpr)},
{"<f> f<f>", fmt.Errorf("duplicate capture name (f): %w", ErrInvalidExpr)},
{`f<f><_>`, fmt.Errorf("found consecutive capture '<f><_>': %w", ErrInvalidExpr)},
{`<f>f<f><_>`, fmt.Errorf("found consecutive capture '<f><_>': %w", ErrInvalidExpr)},
} {
t.Run(tt.name, func(t *testing.T) {
_, err := New(tt.name)
Expand Down