config: reintroduce concat
This commit is contained in:
parent
8d51b6b1d4
commit
92af4801a1
|
@ -1,6 +1,7 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -15,12 +16,34 @@ var Funcs map[string]lang.Function
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Funcs = map[string]lang.Function{
|
Funcs = map[string]lang.Function{
|
||||||
|
"concat": interpolationFuncConcat(),
|
||||||
"file": interpolationFuncFile(),
|
"file": interpolationFuncFile(),
|
||||||
"join": interpolationFuncJoin(),
|
"join": interpolationFuncJoin(),
|
||||||
"element": interpolationFuncElement(),
|
"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
|
// interpolationFuncFile implements the "file" function that allows
|
||||||
// loading contents from a file.
|
// loading contents from a file.
|
||||||
func interpolationFuncFile() lang.Function {
|
func interpolationFuncFile() lang.Function {
|
||||||
|
|
|
@ -10,6 +10,30 @@ import (
|
||||||
"github.com/hashicorp/terraform/config/lang"
|
"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) {
|
func TestInterpolateFuncFile(t *testing.T) {
|
||||||
tf, err := ioutil.TempFile("", "tf")
|
tf, err := ioutil.TempFile("", "tf")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue