Merge pull request #7174 from hashicorp/finanzcheck-interpolate_uniq

core: Rebase #7111 and rename unique() to distinct()
This commit is contained in:
James Nugent 2016-06-15 14:18:02 +02:00 committed by GitHub
commit 1deff4eaca
3 changed files with 66 additions and 0 deletions

View File

@ -60,6 +60,7 @@ func Funcs() map[string]ast.Function {
"coalesce": interpolationFuncCoalesce(),
"compact": interpolationFuncCompact(),
"concat": interpolationFuncConcat(),
"distinct": interpolationFuncDistinct(),
"element": interpolationFuncElement(),
"file": interpolationFuncFile(),
"format": interpolationFuncFormat(),
@ -382,6 +383,42 @@ func interpolationFuncIndex() ast.Function {
}
}
// interpolationFuncDistinct implements the "distinct" function that
// removes duplicate elements from a list.
func interpolationFuncDistinct() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeList},
ReturnType: ast.TypeList,
Variadic: true,
VariadicType: ast.TypeList,
Callback: func(args []interface{}) (interface{}, error) {
var list []string
if len(args) != 1 {
return nil, fmt.Errorf("distinct() excepts only one argument.")
}
if argument, ok := args[0].([]ast.Variable); ok {
for _, element := range argument {
list = appendIfMissing(list, element.Value.(string))
}
}
return stringSliceToVariableValue(list), nil
},
}
}
// helper function to add an element to a list, if it does not already exsit
func appendIfMissing(slice []string, element string) []string {
for _, ele := range slice {
if ele == element {
return slice
}
}
return append(slice, element)
}
// interpolationFuncJoin implements the "join" function that allows
// multi-variable values to be joined by some character.
func interpolationFuncJoin() ast.Function {

View File

@ -261,6 +261,31 @@ func TestInterpolationFuncConcatListOfMaps(t *testing.T) {
}
}
func TestInterpolateFuncDistinct(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
// 3 duplicates
{
`${distinct(concat(split(",", "user1,user2,user3"), split(",", "user1,user2,user3")))}`,
[]interface{}{"user1", "user2", "user3"},
false,
},
// 1 duplicate
{
`${distinct(concat(split(",", "user1,user2,user3"), split(",", "user1,user4")))}`,
[]interface{}{"user1", "user2", "user3", "user4"},
false,
},
// too many args
{
`${distinct(concat(split(",", "user1,user2,user3"), split(",", "user1,user4")), "foo")}`,
nil,
true,
},
},
})
}
func TestInterpolateFuncFile(t *testing.T) {
tf, err := ioutil.TempFile("", "tf")
if err != nil {

View File

@ -114,6 +114,10 @@ The supported built-in functions are:
* `concat(list1, list2)` - Combines two or more lists into a single list.
Example: `concat(aws_instance.db.*.tags.Name, aws_instance.web.*.tags.Name)`
* `distinct(list)` - Removes duplicate items from a list. Keeps the first
occurrence of each element, and removes subsequent occurences.
Example: `distinct(var.usernames)`
* `element(list, index)` - Returns a single element from a list
at the given index. If the index is greater than the number of
elements, this function will wrap using a standard mod algorithm.