remove spaces around arguments before calling macro expansion

This commit is contained in:
Sven Klemm
2018-03-02 19:20:30 +01:00
parent 0f931f9e5e
commit f1ba9137c0
2 changed files with 15 additions and 3 deletions

View File

@@ -30,7 +30,11 @@ func (m *PostgresMacroEngine) Interpolate(query *tsdb.Query, timeRange *tsdb.Tim
var macroError error var macroError error
sql = replaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string { sql = replaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string {
res, err := m.evaluateMacro(groups[1], strings.Split(groups[2], ",")) args := strings.Split(groups[2], ",")
for i, arg := range args {
args[i] = strings.Trim(arg, " ")
}
res, err := m.evaluateMacro(groups[1], args)
if err != nil && macroError == nil { if err != nil && macroError == nil {
macroError = err macroError = err
return "macro_error()" return "macro_error()"
@@ -88,14 +92,14 @@ func (m *PostgresMacroEngine) evaluateMacro(name string, args []string) (string,
if len(args) < 2 { if len(args) < 2 {
return "", fmt.Errorf("macro %v needs time column and interval and optional fill value", name) return "", fmt.Errorf("macro %v needs time column and interval and optional fill value", name)
} }
interval, err := time.ParseDuration(strings.Trim(args[1], `' `)) interval, err := time.ParseDuration(strings.Trim(args[1], `'`))
if err != nil { if err != nil {
return "", fmt.Errorf("error parsing interval %v", args[1]) return "", fmt.Errorf("error parsing interval %v", args[1])
} }
if len(args) == 3 { if len(args) == 3 {
m.Query.Model.Set("fill", true) m.Query.Model.Set("fill", true)
m.Query.Model.Set("fillInterval", interval.Seconds()) m.Query.Model.Set("fillInterval", interval.Seconds())
if strings.Trim(args[2], " ") == "NULL" { if args[2] == "NULL" {
m.Query.Model.Set("fillNull", true) m.Query.Model.Set("fillNull", true)
} else { } else {
floatVal, err := strconv.ParseFloat(args[2], 64) floatVal, err := strconv.ParseFloat(args[2], 64)

View File

@@ -49,6 +49,14 @@ func TestMacroEngine(t *testing.T) {
So(sql, ShouldEqual, "GROUP BY (extract(epoch from time_column)/300)::bigint*300 AS time") So(sql, ShouldEqual, "GROUP BY (extract(epoch from time_column)/300)::bigint*300 AS time")
}) })
Convey("interpolate __timeGroup function with spaces between args", func() {
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
So(err, ShouldBeNil)
So(sql, ShouldEqual, "GROUP BY (extract(epoch from time_column)/300)::bigint*300 AS time")
})
Convey("interpolate __timeTo function", func() { Convey("interpolate __timeTo function", func() {
sql, err := engine.Interpolate(query, timeRange, "select $__timeTo(time_column)") sql, err := engine.Interpolate(query, timeRange, "select $__timeTo(time_column)")
So(err, ShouldBeNil) So(err, ShouldBeNil)