config: Add "flatten" interpolation function
This function turns a list of lists or any arbitrary number of nested lists into a flat list of primitive values.
This commit is contained in:
parent
f1042a1338
commit
117f44bcda
|
@ -76,6 +76,7 @@ func Funcs() map[string]ast.Function {
|
|||
"element": interpolationFuncElement(),
|
||||
"file": interpolationFuncFile(),
|
||||
"matchkeys": interpolationFuncMatchKeys(),
|
||||
"flatten": interpolationFuncFlatten(),
|
||||
"floor": interpolationFuncFloor(),
|
||||
"format": interpolationFuncFormat(),
|
||||
"formatlist": interpolationFuncFormatList(),
|
||||
|
@ -1453,3 +1454,30 @@ func interpolationFuncSubstr() ast.Function {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Flatten until it's not ast.TypeList
|
||||
func flattener(finalList []ast.Variable, flattenList []ast.Variable) []ast.Variable {
|
||||
for _, val := range flattenList {
|
||||
if val.Type == ast.TypeList {
|
||||
finalList = flattener(finalList, val.Value.([]ast.Variable))
|
||||
} else {
|
||||
finalList = append(finalList, val)
|
||||
}
|
||||
}
|
||||
return finalList
|
||||
}
|
||||
|
||||
// Flatten to single list
|
||||
func interpolationFuncFlatten() ast.Function {
|
||||
return ast.Function{
|
||||
ArgTypes: []ast.Type{ast.TypeList},
|
||||
ReturnType: ast.TypeList,
|
||||
Variadic: false,
|
||||
Callback: func(args []interface{}) (interface{}, error) {
|
||||
inputList := args[0].([]ast.Variable)
|
||||
|
||||
var outputList []ast.Variable
|
||||
return flattener(outputList, inputList), nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2503,3 +2503,49 @@ func TestInterpolateFuncBcrypt(t *testing.T) {
|
|||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestInterpolateFuncFlatten(t *testing.T) {
|
||||
testFunction(t, testFunctionConfig{
|
||||
Cases: []testFunctionCase{
|
||||
// empty string within array
|
||||
{
|
||||
`${flatten(split(",", "a,,b"))}`,
|
||||
[]interface{}{"a", "", "b"},
|
||||
false,
|
||||
},
|
||||
|
||||
// typical array
|
||||
{
|
||||
`${flatten(split(",", "a,b,c"))}`,
|
||||
[]interface{}{"a", "b", "c"},
|
||||
false,
|
||||
},
|
||||
|
||||
// empty array
|
||||
{
|
||||
`${flatten(split(",", ""))}`,
|
||||
[]interface{}{""},
|
||||
false,
|
||||
},
|
||||
|
||||
// list of lists
|
||||
{
|
||||
`${flatten(list(list("a"), list("b")))}`,
|
||||
[]interface{}{"a", "b"},
|
||||
false,
|
||||
},
|
||||
// list of lists of lists
|
||||
{
|
||||
`${flatten(list(list("a"), list(list("b","c"))))}`,
|
||||
[]interface{}{"a", "b", "c"},
|
||||
false,
|
||||
},
|
||||
// list of strings
|
||||
{
|
||||
`${flatten(list("a", "b", "c"))}`,
|
||||
[]interface{}{"a", "b", "c"},
|
||||
false,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
@ -232,6 +232,10 @@ The supported built-in functions are:
|
|||
* `floor(float)` - Returns the greatest integer value less than or equal to
|
||||
the argument.
|
||||
|
||||
* `flatten(list of lists)` - Flattens lists of lists down to a flat list of
|
||||
primitive values, eliminating any nested lists recursively. Examples:
|
||||
* `flatten(data.github_user.user.*.gpg_keys)`
|
||||
|
||||
* `format(format, args, ...)` - Formats a string according to the given
|
||||
format. The syntax for the format is standard `sprintf` syntax.
|
||||
Good documentation for the syntax can be [found here](https://golang.org/pkg/fmt/).
|
||||
|
|
Loading…
Reference in New Issue