Hardens the match interoplator against negative arguments.

This commit is contained in:
James Phillips 2016-03-07 13:32:32 -08:00
parent 63c826c2c0
commit a9d640c024
2 changed files with 4 additions and 1 deletions

View File

@ -128,7 +128,7 @@ func (ct *CompiledTemplate) Render(name string) (*structs.PreparedQuery, error)
Variadic: false,
Callback: func(inputs []interface{}) (interface{}, error) {
i, ok := inputs[0].(int)
if ok && i < len(matches) {
if ok && i >= 0 && i < len(matches) {
return matches[i], nil
} else {
return "", nil

View File

@ -214,6 +214,7 @@ func TestTemplate_Render(t *testing.T) {
Service: structs.ServiceQuery{
Service: "${name.prefix} xxx ${name.full} xxx ${name.suffix}",
Tags: []string{
"${match(-1)}",
"${match(0)}",
"${match(1)}",
"${match(2)}",
@ -243,6 +244,7 @@ func TestTemplate_Render(t *testing.T) {
Service: structs.ServiceQuery{
Service: "hello- xxx hello-foo-bar-none xxx foo-bar-none",
Tags: []string{
"",
"hello-foo-bar-none",
"hello",
"foo",
@ -277,6 +279,7 @@ func TestTemplate_Render(t *testing.T) {
"",
"",
"",
"",
"42",
},
},