Initial implementation of compact() interpolation function

This commit is contained in:
Anthony Stanton 2015-09-15 09:34:57 +02:00
parent e7f3675fd5
commit 7610874264
2 changed files with 30 additions and 0 deletions

View File

@ -30,6 +30,7 @@ func init() {
"length": interpolationFuncLength(),
"replace": interpolationFuncReplace(),
"split": interpolationFuncSplit(),
"compact": interpolationFuncCompact(),
"base64encode": interpolationFuncBase64Encode(),
"base64decode": interpolationFuncBase64Decode(),
}
@ -279,6 +280,22 @@ func interpolationFuncSplit() ast.Function {
}
}
// interpolationFuncCompact strips a list of values (e.g. as returned by
// "split") of any empty strings
func interpolationFuncCompact() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Variadic: false,
Callback: func(args []interface{}) (interface{}, error) {
if !IsStringList(args[0].(string)) {
return args[0].(string), nil
}
return CompactStringList(StringList(args[0].(string))).String(), nil
},
}
}
// interpolationFuncLookup implements the "lookup" function that allows
// dynamic lookups of map types within a Terraform configuration.
func interpolationFuncLookup(vs map[string]ast.Variable) ast.Function {

View File

@ -74,3 +74,16 @@ func (sl StringList) String() string {
func IsStringList(s string) bool {
return strings.Contains(s, stringListDelim)
}
func CompactStringList(sl StringList) StringList {
parts := sl.Slice()
var newlist []string
// drop the empty strings
for i := range parts {
if parts[i] != "" {
newlist = append(newlist, parts[i])
}
}
return NewStringList(newlist)
}