Skip to content

Commit da7d69c

Browse files
authored
Update macros regex to support function calls as macro arguments (#64)
1 parent 72e56af commit da7d69c

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

macros.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func trimAll(s []string) []string {
128128
}
129129

130130
func getMacroRegex(name string) string {
131-
return fmt.Sprintf("\\$__%s\\b(?:\\((.*?)\\))?", name)
131+
return fmt.Sprintf("\\$__%s\\b(?:\\((.*?\\)?)\\))?", name)
132132
}
133133

134134
// Interpolate returns an interpolated query string given a backend.DataQuery

macros_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ func TestInterpolate(t *testing.T) {
6060
{input: "select * from $__params(hello) AND $__params(world)", output: "select * from bar_hello AND bar_world", name: "same macro multiple times with different param"},
6161
{input: "select * from $__params(world) AND $__foo() AND $__params(hello)", output: "select * from bar_world AND bar AND bar_hello", name: "different macros with different params"},
6262
{input: "select * from foo where $__timeFilter(time)", output: "select * from foo where time >= '0001-01-01T00:00:00Z' AND time <= '0001-01-01T00:00:00Z'", name: "default timeFilter"},
63+
{input: "select * from foo where $__timeFilter(cast(sth as timestamp))", output: "select * from foo where cast(sth as timestamp) >= '0001-01-01T00:00:00Z' AND cast(sth as timestamp) <= '0001-01-01T00:00:00Z'", name: "default timeFilter"},
6364
{input: "select * from foo where $__timeTo(time)", output: "select * from foo where time <= '0001-01-01T00:00:00Z'", name: "default timeTo macro"},
6465
{input: "select * from foo where $__timeFrom(time)", output: "select * from foo where time >= '0001-01-01T00:00:00Z'", name: "default timeFrom macro"},
66+
{input: "select * from foo where $__timeFrom(cast(sth as timestamp))", output: "select * from foo where cast(sth as timestamp) >= '0001-01-01T00:00:00Z'", name: "default timeFrom macro"},
6567
{input: "select * from foo where $__timeGroup(time,minute)", output: "select * from foo where grouped!", name: "overriden timeGroup macro"},
6668
{input: "select $__column from $__table", output: "select my_col from my_table", name: "table and column macros"},
6769
}
@@ -81,7 +83,7 @@ func TestInterpolate(t *testing.T) {
8183
}
8284

8385
func TestGetMacroRegex_returns_composed_regular_expression(t *testing.T) {
84-
assert.Equal(t, `\$__some_string\b(?:\((.*?)\))?`, getMacroRegex("some_string"))
86+
assert.Equal(t, `\$__some_string\b(?:\((.*?\)?)\))?`, getMacroRegex("some_string"))
8587
}
8688

8789
func TestGetMatches(t *testing.T) {

0 commit comments

Comments
 (0)