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-15 05:58:46 +01:00
|
|
|
func registerBuiltins(scope *ast.BasicScope) {
|
2015-01-14 20:59:06 +01:00
|
|
|
if scope.FuncMap == nil {
|
2015-01-15 05:58:46 +01:00
|
|
|
scope.FuncMap = make(map[string]ast.Function)
|
2015-01-14 20:59:06 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-15 05:58:46 +01:00
|
|
|
func builtinIntToString() ast.Function {
|
|
|
|
return ast.Function{
|
2015-01-14 20:59:06 +01:00
|
|
|
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
|
|
|
|
2015-01-15 05:58:46 +01:00
|
|
|
func builtinStringToInt() ast.Function {
|
|
|
|
return ast.Function{
|
2015-01-14 21:02:26 +01:00
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|