github.com/arvindram03/terraform@v0.3.7-0.20150212015210-408f838db36d/config/lang/builtins.go (about)

     1  package lang
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/hashicorp/terraform/config/lang/ast"
     7  )
     8  
     9  // NOTE: All builtins are tested in engine_test.go
    10  
    11  func registerBuiltins(scope *ast.BasicScope) *ast.BasicScope {
    12  	if scope == nil {
    13  		scope = new(ast.BasicScope)
    14  	}
    15  	if scope.FuncMap == nil {
    16  		scope.FuncMap = make(map[string]ast.Function)
    17  	}
    18  	scope.FuncMap["__builtin_IntToString"] = builtinIntToString()
    19  	scope.FuncMap["__builtin_StringToInt"] = builtinStringToInt()
    20  	return scope
    21  }
    22  
    23  func builtinIntToString() ast.Function {
    24  	return ast.Function{
    25  		ArgTypes:   []ast.Type{ast.TypeInt},
    26  		ReturnType: ast.TypeString,
    27  		Callback: func(args []interface{}) (interface{}, error) {
    28  			return strconv.FormatInt(int64(args[0].(int)), 10), nil
    29  		},
    30  	}
    31  }
    32  
    33  func builtinStringToInt() ast.Function {
    34  	return ast.Function{
    35  		ArgTypes:   []ast.Type{ast.TypeInt},
    36  		ReturnType: ast.TypeString,
    37  		Callback: func(args []interface{}) (interface{}, error) {
    38  			v, err := strconv.ParseInt(args[0].(string), 0, 0)
    39  			if err != nil {
    40  				return nil, err
    41  			}
    42  
    43  			return int(v), nil
    44  		},
    45  	}
    46  }