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

Add parseraw function #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
18 changes: 13 additions & 5 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ import (
//
// MetricsQL is backwards-compatible with PromQL.
func Parse(s string) (Expr, error) {
e, err := ParseRaw(s)
if err != nil {
return nil, err
}
e = removeParensExpr(e)
e = simplifyConstants(e)
if err := checkSupportedFunctions(e); err != nil {
return nil, err
}
return e, nil
}

func ParseRaw(s string) (Expr, error) {
// Parse s
e, err := parseInternal(s)
if err != nil {
Expand All @@ -24,11 +37,6 @@ func Parse(s string) (Expr, error) {
if e, err = expandWithExpr(was, e); err != nil {
return nil, fmt.Errorf(`cannot expand WITH expressions: %s`, err)
}
e = removeParensExpr(e)
e = simplifyConstants(e)
if err := checkSupportedFunctions(e); err != nil {
return nil, err
}
return e, nil
}

Expand Down
17 changes: 15 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"testing"
)

func TestParseSuccess(t *testing.T) {
func helpers(t *testing.T, parser func(string) (Expr, error)) (func(string, string), func(string)) {
another := func(s string, sExpected string) {
t.Helper()

e, err := Parse(s)
e, err := parser(s)
if err != nil {
t.Fatalf("unexpected error when parsing %s: %s", s, err)
}
Expand All @@ -17,10 +17,16 @@ func TestParseSuccess(t *testing.T) {
t.Fatalf("unexpected string constructed;\ngot\n%s\nwant\n%s", res, sExpected)
}
}

same := func(s string) {
t.Helper()
another(s, s)
}
return another, same
}

func TestParseSuccess(t *testing.T) {
another, same := helpers(t, Parse)

// metricExpr
same(`{}`)
Expand Down Expand Up @@ -952,3 +958,10 @@ func TestParseError(t *testing.T) {
f(`with (x={a="b" or c="d"}) {x,d="e"}`)
f(`with (x={a="b" or c="d"}) {x,d="e" or z="c"}`)
}

func TestParseRawSuccess(t *testing.T) {
another, same := helpers(t, ParseRaw)

same("(metric[5m])")
another(`100 / 100`, `(100 / 100)`)
}