Merge pull request #9795 from hashicorp/b-formatlist-empty
config: formatlist accepts an empty list
This commit is contained in:
commit
260fb81373
|
@ -7,7 +7,6 @@ import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
|
@ -499,15 +498,25 @@ func interpolationFuncFormatList() ast.Function {
|
||||||
varargs := make([]interface{}, len(args)-1)
|
varargs := make([]interface{}, len(args)-1)
|
||||||
copy(varargs, args[1:])
|
copy(varargs, args[1:])
|
||||||
|
|
||||||
|
// Verify we have some arguments
|
||||||
|
if len(varargs) == 0 {
|
||||||
|
return nil, fmt.Errorf("no arguments to formatlist")
|
||||||
|
}
|
||||||
|
|
||||||
// Convert arguments that are lists into slices.
|
// Convert arguments that are lists into slices.
|
||||||
// Confirm along the way that all lists have the same length (n).
|
// Confirm along the way that all lists have the same length (n).
|
||||||
var n int
|
var n int
|
||||||
|
listSeen := false
|
||||||
for i := 1; i < len(args); i++ {
|
for i := 1; i < len(args); i++ {
|
||||||
s, ok := args[i].([]ast.Variable)
|
s, ok := args[i].([]ast.Variable)
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mark that we've seen at least one list
|
||||||
|
listSeen = true
|
||||||
|
|
||||||
|
// Convert the ast.Variable to a slice of strings
|
||||||
parts, err := listVariableValueToStringSlice(s)
|
parts, err := listVariableValueToStringSlice(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -527,8 +536,11 @@ func interpolationFuncFormatList() ast.Function {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if n == 0 {
|
// If we didn't see a list this is an error because we
|
||||||
return nil, errors.New("no lists in arguments to formatlist")
|
// can't determine the return value length.
|
||||||
|
if !listSeen {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"formatlist requires at least one list argument")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do the formatting.
|
// Do the formatting.
|
||||||
|
|
|
@ -994,6 +994,18 @@ func TestInterpolateFuncFormatList(t *testing.T) {
|
||||||
[]interface{}{"demo-rest-elb.id"},
|
[]interface{}{"demo-rest-elb.id"},
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
|
// Works with empty lists [GH-7607]
|
||||||
|
{
|
||||||
|
`${formatlist("%s", var.emptylist)}`,
|
||||||
|
[]interface{}{},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Vars: map[string]ast.Variable{
|
||||||
|
"var.emptylist": {
|
||||||
|
Type: ast.TypeList,
|
||||||
|
Value: []ast.Variable{},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1928,7 +1940,6 @@ func testFunction(t *testing.T, config testFunctionConfig) {
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := hil.Eval(ast, langEvalConfig(config.Vars))
|
result, err := hil.Eval(ast, langEvalConfig(config.Vars))
|
||||||
t.Logf("err: %v", err)
|
|
||||||
if err != nil != tc.Error {
|
if err != nil != tc.Error {
|
||||||
t.Fatalf("Case #%d:\ninput: %#v\nerr: %v", i, tc.Input, err)
|
t.Fatalf("Case #%d:\ninput: %#v\nerr: %v", i, tc.Input, err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue