@@ -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.
387424func interpolationFuncJoin () ast.Function {
0 commit comments