Skip to content

Commit 66e6715

Browse files
authored
Merge pull request #55 from grafana/regex
Make regex stricter on macro name matching
2 parents ffd3f00 + f3b2510 commit 66e6715

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
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(?:\\((.*?)\\))?", name)
131+
return fmt.Sprintf("\\$__%s\\b(?:\\((.*?)\\))?", name)
132132
}
133133

134134
func interpolate(driver Driver, query *Query) (string, error) {

macros_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ func (h *MockDB) Macros() (macros Macros) {
1919
"foo": func(query *Query, args []string) (out string, err error) {
2020
return "bar", nil
2121
},
22+
"fooBaz": func(query *Query, args []string) (out string, err error) {
23+
return "qux", nil
24+
},
2225
"params": func(query *Query, args []string) (out string, err error) {
2326
if args[0] != "" {
2427
return "bar_" + args[0], nil
@@ -47,6 +50,7 @@ func TestInterpolate(t *testing.T) {
4750
tests := []test{
4851
{input: "select * from foo", output: "select * from foo", name: "macro with incorrect syntax"},
4952
{input: "select * from $__foo()", output: "select * from bar", name: "correct macro"},
53+
{input: "select * from $__fooBaz()", output: "select * from qux", name: "this macro name's substring is another macro"},
5054
{input: "select '$__foo()' from $__foo()", output: "select 'bar' from bar", name: "multiple instances of same macro"},
5155
{input: "select * from $__foo()$__foo()", output: "select * from barbar", name: "multiple instances of same macro without space"},
5256
{input: "select * from $__foo", output: "select * from bar", name: "macro without paranthesis"},

0 commit comments

Comments
 (0)