Skip to content

Commit df3e017

Browse files
janschumannjen20
authored andcommitted
1 parent b7d0bd5 commit df3e017

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎config/interpolate_funcs.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func Funcs() map[string]ast.Function {
7171
"lower": interpolationFuncLower(),
7272
"md5": interpolationFuncMd5(),
7373
"uuid": interpolationFuncUUID(),
74+
"uniq": interpolationFuncUniq(),
7475
"replace": interpolationFuncReplace(),
7576
"sha1": interpolationFuncSha1(),
7677
"sha256": interpolationFuncSha256(),
@@ -382,6 +383,42 @@ func interpolationFuncIndex() ast.Function {
382383
}
383384
}
384385

386+
// interpolationFuncUniq implements the "uniq" function that
387+
// removes duplicate elements from a list.
388+
func interpolationFuncUniq() ast.Function {
389+
return ast.Function{
390+
ArgTypes: []ast.Type{ast.TypeList},
391+
ReturnType: ast.TypeList,
392+
Variadic: true,
393+
VariadicType: ast.TypeList,
394+
Callback: func(args []interface{}) (interface{}, error) {
395+
var list []string
396+
397+
if len(args) != 1 {
398+
return nil, fmt.Errorf("uniq() excepts only one argument.")
399+
}
400+
401+
if argument, ok := args[0].([]ast.Variable); ok {
402+
for _, element := range argument {
403+
list = appendIfMissing(list, element.Value.(string))
404+
}
405+
}
406+
407+
return stringSliceToVariableValue(list), nil
408+
},
409+
}
410+
}
411+
412+
// helper function to add an element to a list, if it does not already exsit
413+
func appendIfMissing(slice []string, element string) []string {
414+
for _, ele := range slice {
415+
if ele == element {
416+
return slice
417+
}
418+
}
419+
return append(slice, element)
420+
}
421+
385422
// interpolationFuncJoin implements the "join" function that allows
386423
// multi-variable values to be joined by some character.
387424
func interpolationFuncJoin() ast.Function {

‎config/interpolate_funcs_test.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,31 @@ func TestInterpolationFuncConcatListOfMaps(t *testing.T) {
261261
}
262262
}
263263

264+
func TestInterpolateFuncUniq(t *testing.T) {
265+
testFunction(t, testFunctionConfig{
266+
Cases: []testFunctionCase{
267+
// 3 duplicates
268+
{
269+
`${uniq(concat(split(",", "user1,user2,user3"), split(",", "user1,user2,user3")))}`,
270+
[]interface{}{"user1", "user2", "user3"},
271+
false,
272+
},
273+
// 1 duplicate
274+
{
275+
`${uniq(concat(split(",", "user1,user2,user3"), split(",", "user1,user4")))}`,
276+
[]interface{}{"user1", "user2", "user3", "user4"},
277+
false,
278+
},
279+
// too many args
280+
{
281+
`${uniq(concat(split(",", "user1,user2,user3"), split(",", "user1,user4")), "foo")}`,
282+
nil,
283+
true,
284+
},
285+
},
286+
})
287+
}
288+
264289
func TestInterpolateFuncFile(t *testing.T) {
265290
tf, err := ioutil.TempFile("", "tf")
266291
if err != nil {

0 commit comments

Comments
 (0)