config: reintroduce concat

This commit is contained in:
Mitchell Hashimoto 2015-01-13 12:47:54 -08:00
parent 8d51b6b1d4
commit 92af4801a1
2 changed files with 47 additions and 0 deletions

View File

@ -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 {

View File

@ -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 {