mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 18:01:01 -06:00
config: reintroduce concat
This commit is contained in:
parent
8d51b6b1d4
commit
92af4801a1
@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
@ -15,12 +16,34 @@ var Funcs map[string]lang.Function
|
||||
|
||||
func init() {
|
||||
Funcs = map[string]lang.Function{
|
||||
"concat": interpolationFuncConcat(),
|
||||
"file": interpolationFuncFile(),
|
||||
"join": interpolationFuncJoin(),
|
||||
"element": interpolationFuncElement(),
|
||||
}
|
||||
}
|
||||
|
||||
// interpolationFuncConcat implements the "concat" function that
|
||||
// concatenates multiple strings. This isn't actually necessary anymore
|
||||
// since our language supports string concat natively, but for backwards
|
||||
// compat we do this.
|
||||
func interpolationFuncConcat() lang.Function {
|
||||
return lang.Function{
|
||||
ArgTypes: []ast.Type{ast.TypeString},
|
||||
ReturnType: ast.TypeString,
|
||||
Variadic: true,
|
||||
VariadicType: ast.TypeString,
|
||||
Callback: func(args []interface{}) (interface{}, error) {
|
||||
var b bytes.Buffer
|
||||
for _, v := range args {
|
||||
b.WriteString(v.(string))
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// interpolationFuncFile implements the "file" function that allows
|
||||
// loading contents from a file.
|
||||
func interpolationFuncFile() lang.Function {
|
||||
|
@ -10,6 +10,30 @@ import (
|
||||
"github.com/hashicorp/terraform/config/lang"
|
||||
)
|
||||
|
||||
func TestInterpolateFuncConcat(t *testing.T) {
|
||||
testFunction(t, testFunctionConfig{
|
||||
Cases: []testFunctionCase{
|
||||
{
|
||||
`${concat("foo", "bar")}`,
|
||||
"foobar",
|
||||
false,
|
||||
},
|
||||
|
||||
{
|
||||
`${concat("foo")}`,
|
||||
"foo",
|
||||
false,
|
||||
},
|
||||
|
||||
{
|
||||
`${concat()}`,
|
||||
nil,
|
||||
true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestInterpolateFuncFile(t *testing.T) {
|
||||
tf, err := ioutil.TempFile("", "tf")
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user