mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Initial implementation of compact() interpolation function
This commit is contained in:
parent
e7f3675fd5
commit
7610874264
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user