2015-01-14 20:59:06 +01:00
|
|
|
package lang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config/lang/ast"
|
|
|
|
)
|
|
|
|
|
2015-01-14 21:02:26 +01:00
|
|
|
// NOTE: All builtins are tested in engine_test.go
|
|
|
|
|
2015-01-14 20:59:06 +01:00
|
|
|
func registerBuiltins(scope *Scope) {
|
|
|
|
if scope.FuncMap == nil {
|
|
|
|
scope.FuncMap = make(map[string]Function)
|
|
|
|
}
|
|
|
|
scope.FuncMap["__builtin_IntToString"] = builtinIntToString()
|
2015-01-14 21:02:26 +01:00
|
|
|
scope.FuncMap["__builtin_StringToInt"] = builtinStringToInt()
|
2015-01-14 20:59:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinIntToString() Function {
|
|
|
|
return Function{
|
|
|
|
ArgTypes: []ast.Type{ast.TypeInt},
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
Callback: func(args []interface{}) (interface{}, error) {
|
|
|
|
return strconv.FormatInt(int64(args[0].(int)), 10), nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 21:02:26 +01:00
|
|
|
|
|
|
|
func builtinStringToInt() Function {
|
|
|
|
return Function{
|
|
|
|
ArgTypes: []ast.Type{ast.TypeInt},
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
Callback: func(args []interface{}) (interface{}, error) {
|
|
|
|
v, err := strconv.ParseInt(args[0].(string), 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(v), nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|