From 2abeb2d9ace1ac61c7b1e76265e1bfc568e55258 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 14 Jan 2015 22:01:42 -0800 Subject: [PATCH] config: use new API --- config/config.go | 7 ++++--- config/interpolate.go | 9 +++++---- config/interpolate_funcs.go | 25 ++++++++++++------------- config/interpolate_funcs_test.go | 9 ++++----- config/lang/ast/literal.go | 2 +- config/lang/check_identifier.go | 2 +- config/lang/eval.go | 3 +++ config/raw_config.go | 16 ++++++++-------- config/raw_config_test.go | 21 ++++++++++----------- helper/diff/diff_test.go | 5 ++--- helper/schema/schema_test.go | 9 ++++----- terraform/context.go | 27 +++++++++++++-------------- 12 files changed, 67 insertions(+), 68 deletions(-) diff --git a/config/config.go b/config/config.go index 7163e2829..c67b58a45 100644 --- a/config/config.go +++ b/config/config.go @@ -359,9 +359,10 @@ func (c *Config) Validate() error { r.RawCount.interpolate(func(root ast.Node) (string, error) { // Execute the node but transform the AST so that it returns // a fixed value of "5" for all interpolations. - var engine lang.Engine - out, _, err := engine.Execute(lang.FixedValueTransform( - root, &ast.LiteralNode{Value: "5", Type: ast.TypeString})) + out, _, err := lang.Eval( + lang.FixedValueTransform( + root, &ast.LiteralNode{Value: "5", Typex: ast.TypeString}), + nil) if err != nil { return "", err } diff --git a/config/interpolate.go b/config/interpolate.go index e6a709d94..b13cdb405 100644 --- a/config/interpolate.go +++ b/config/interpolate.go @@ -230,23 +230,24 @@ func DetectVariables(root ast.Node) ([]InterpolatedVariable, error) { var resultErr error // Visitor callback - fn := func(n ast.Node) { + fn := func(n ast.Node) ast.Node { if resultErr != nil { - return + return n } vn, ok := n.(*ast.VariableAccess) if !ok { - return + return n } v, err := NewInterpolatedVariable(vn.Name) if err != nil { resultErr = err - return + return n } result = append(result, v) + return n } // Visitor pattern diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index 825913588..c529adb8d 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -7,15 +7,14 @@ import ( "strconv" "strings" - "github.com/hashicorp/terraform/config/lang" "github.com/hashicorp/terraform/config/lang/ast" ) // Funcs is the mapping of built-in functions for configuration. -var Funcs map[string]lang.Function +var Funcs map[string]ast.Function func init() { - Funcs = map[string]lang.Function{ + Funcs = map[string]ast.Function{ "concat": interpolationFuncConcat(), "file": interpolationFuncFile(), "join": interpolationFuncJoin(), @@ -27,8 +26,8 @@ func init() { // 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{ +func interpolationFuncConcat() ast.Function { + return ast.Function{ ArgTypes: []ast.Type{ast.TypeString}, ReturnType: ast.TypeString, Variadic: true, @@ -46,8 +45,8 @@ func interpolationFuncConcat() lang.Function { // interpolationFuncFile implements the "file" function that allows // loading contents from a file. -func interpolationFuncFile() lang.Function { - return lang.Function{ +func interpolationFuncFile() ast.Function { + return ast.Function{ ArgTypes: []ast.Type{ast.TypeString}, ReturnType: ast.TypeString, Callback: func(args []interface{}) (interface{}, error) { @@ -63,8 +62,8 @@ func interpolationFuncFile() lang.Function { // interpolationFuncJoin implements the "join" function that allows // multi-variable values to be joined by some character. -func interpolationFuncJoin() lang.Function { - return lang.Function{ +func interpolationFuncJoin() ast.Function { + return ast.Function{ ArgTypes: []ast.Type{ast.TypeString, ast.TypeString}, ReturnType: ast.TypeString, Callback: func(args []interface{}) (interface{}, error) { @@ -81,8 +80,8 @@ func interpolationFuncJoin() lang.Function { // interpolationFuncLookup implements the "lookup" function that allows // dynamic lookups of map types within a Terraform configuration. -func interpolationFuncLookup(vs map[string]lang.Variable) lang.Function { - return lang.Function{ +func interpolationFuncLookup(vs map[string]ast.Variable) ast.Function { + return ast.Function{ ArgTypes: []ast.Type{ast.TypeString, ast.TypeString}, ReturnType: ast.TypeString, Callback: func(args []interface{}) (interface{}, error) { @@ -107,8 +106,8 @@ func interpolationFuncLookup(vs map[string]lang.Variable) lang.Function { // interpolationFuncElement implements the "element" function that allows // a specific index to be looked up in a multi-variable value. Note that this will // wrap if the index is larger than the number of elements in the multi-variable value. -func interpolationFuncElement() lang.Function { - return lang.Function{ +func interpolationFuncElement() ast.Function { + return ast.Function{ ArgTypes: []ast.Type{ast.TypeString, ast.TypeString}, ReturnType: ast.TypeString, Callback: func(args []interface{}) (interface{}, error) { diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 68fce1098..1a4a59057 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -109,8 +109,8 @@ func TestInterpolateFuncJoin(t *testing.T) { func TestInterpolateFuncLookup(t *testing.T) { testFunction(t, testFunctionConfig{ - Vars: map[string]lang.Variable{ - "var.foo.bar": lang.Variable{ + Vars: map[string]ast.Variable{ + "var.foo.bar": ast.Variable{ Value: "baz", Type: ast.TypeString, }, @@ -176,7 +176,7 @@ func TestInterpolateFuncElement(t *testing.T) { type testFunctionConfig struct { Cases []testFunctionCase - Vars map[string]lang.Variable + Vars map[string]ast.Variable } type testFunctionCase struct { @@ -192,8 +192,7 @@ func testFunction(t *testing.T, config testFunctionConfig) { t.Fatalf("%d: err: %s", i, err) } - engine := langEngine(config.Vars) - out, _, err := engine.Execute(ast) + out, _, err := lang.Eval(ast, langEvalConfig(config.Vars)) if (err != nil) != tc.Error { t.Fatalf("%d: err: %s", i, err) } diff --git a/config/lang/ast/literal.go b/config/lang/ast/literal.go index 9da3ff3a3..1714ff026 100644 --- a/config/lang/ast/literal.go +++ b/config/lang/ast/literal.go @@ -25,7 +25,7 @@ func (n *LiteralNode) GoString() string { } func (n *LiteralNode) String() string { - return fmt.Sprintf("Literal(%s, %v)", n.Type, n.Value) + return fmt.Sprintf("Literal(%s, %v)", n.Typex, n.Value) } func (n *LiteralNode) Type(Scope) (Type, error) { diff --git a/config/lang/check_identifier.go b/config/lang/check_identifier.go index a50673a2d..b835e8e4e 100644 --- a/config/lang/check_identifier.go +++ b/config/lang/check_identifier.go @@ -40,7 +40,7 @@ func (c *IdentifierCheck) visit(raw ast.Node) ast.Node { case *ast.LiteralNode: // Ignore default: - c.createErr(n, fmt.Sprintf("unknown node: %#v", raw)) + // Ignore } // We never do replacement with this visitor diff --git a/config/lang/eval.go b/config/lang/eval.go index 9b0a9839b..cc4d756df 100644 --- a/config/lang/eval.go +++ b/config/lang/eval.go @@ -27,6 +27,9 @@ type SemanticChecker func(ast.Node) error // of the output, and any error that occurred. func Eval(root ast.Node, config *EvalConfig) (interface{}, ast.Type, error) { // Copy the scope so we can add our builtins + if config == nil { + config = new(EvalConfig) + } scope := registerBuiltins(config.GlobalScope) implicitMap := map[ast.Type]map[ast.Type]string{ ast.TypeInt: { diff --git a/config/raw_config.go b/config/raw_config.go index 1fed79dfe..753e7f1b8 100644 --- a/config/raw_config.go +++ b/config/raw_config.go @@ -80,10 +80,10 @@ func (r *RawConfig) Config() map[string]interface{} { // Any prior calls to Interpolate are replaced with this one. // // If a variable key is missing, this will panic. -func (r *RawConfig) Interpolate(vs map[string]lang.Variable) error { - engine := langEngine(vs) +func (r *RawConfig) Interpolate(vs map[string]ast.Variable) error { + config := langEvalConfig(vs) return r.interpolate(func(root ast.Node) (string, error) { - out, _, err := engine.Execute(root) + out, _, err := lang.Eval(root, config) if err != nil { return "", err } @@ -202,16 +202,16 @@ type gobRawConfig struct { Raw map[string]interface{} } -// langEngine returns the lang.Engine to use for evaluating configurations. -func langEngine(vs map[string]lang.Variable) *lang.Engine { - funcMap := make(map[string]lang.Function) +// langEvalConfig returns the evaluation configuration we use to execute. +func langEvalConfig(vs map[string]ast.Variable) *lang.EvalConfig { + funcMap := make(map[string]ast.Function) for k, v := range Funcs { funcMap[k] = v } funcMap["lookup"] = interpolationFuncLookup(vs) - return &lang.Engine{ - GlobalScope: &lang.Scope{ + return &lang.EvalConfig{ + GlobalScope: &ast.BasicScope{ VarMap: vs, FuncMap: funcMap, }, diff --git a/config/raw_config_test.go b/config/raw_config_test.go index 726c6c955..49e13a45a 100644 --- a/config/raw_config_test.go +++ b/config/raw_config_test.go @@ -5,7 +5,6 @@ import ( "reflect" "testing" - "github.com/hashicorp/terraform/config/lang" "github.com/hashicorp/terraform/config/lang/ast" ) @@ -43,8 +42,8 @@ func TestRawConfig(t *testing.T) { t.Fatalf("bad: %#v", rc.Config()) } - vars := map[string]lang.Variable{ - "var.bar": lang.Variable{ + vars := map[string]ast.Variable{ + "var.bar": ast.Variable{ Value: "baz", Type: ast.TypeString, }, @@ -76,8 +75,8 @@ func TestRawConfig_double(t *testing.T) { t.Fatalf("err: %s", err) } - vars := map[string]lang.Variable{ - "var.bar": lang.Variable{ + vars := map[string]ast.Variable{ + "var.bar": ast.Variable{ Value: "baz", Type: ast.TypeString, }, @@ -95,8 +94,8 @@ func TestRawConfig_double(t *testing.T) { t.Fatalf("bad: %#v", actual) } - vars = map[string]lang.Variable{ - "var.bar": lang.Variable{ + vars = map[string]ast.Variable{ + "var.bar": ast.Variable{ Value: "what", Type: ast.TypeString, }, @@ -135,8 +134,8 @@ func TestRawConfig_unknown(t *testing.T) { t.Fatalf("err: %s", err) } - vars := map[string]lang.Variable{ - "var.bar": lang.Variable{ + vars := map[string]ast.Variable{ + "var.bar": ast.Variable{ Value: UnknownVariableValue, Type: ast.TypeString, }, @@ -178,8 +177,8 @@ func TestRawConfigValue(t *testing.T) { t.Fatalf("err: %#v", rc.Value()) } - vars := map[string]lang.Variable{ - "var.bar": lang.Variable{ + vars := map[string]ast.Variable{ + "var.bar": ast.Variable{ Value: "baz", Type: ast.TypeString, }, diff --git a/helper/diff/diff_test.go b/helper/diff/diff_test.go index 30d0aebfd..8afeddb14 100644 --- a/helper/diff/diff_test.go +++ b/helper/diff/diff_test.go @@ -8,7 +8,6 @@ import ( "testing" "github.com/hashicorp/terraform/config" - "github.com/hashicorp/terraform/config/lang" "github.com/hashicorp/terraform/config/lang/ast" "github.com/hashicorp/terraform/terraform" ) @@ -23,9 +22,9 @@ func testConfig( } if len(vs) > 0 { - vars := make(map[string]lang.Variable) + vars := make(map[string]ast.Variable) for k, v := range vs { - vars[k] = lang.Variable{Value: v, Type: ast.TypeString} + vars[k] = ast.Variable{Value: v, Type: ast.TypeString} } if err := rc.Interpolate(vars); err != nil { diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 8129d5184..8e1a6c98e 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/hashicorp/terraform/config" - "github.com/hashicorp/terraform/config/lang" "github.com/hashicorp/terraform/config/lang/ast" "github.com/hashicorp/terraform/terraform" ) @@ -1808,9 +1807,9 @@ func TestSchemaMap_Diff(t *testing.T) { } if len(tc.ConfigVariables) > 0 { - vars := make(map[string]lang.Variable) + vars := make(map[string]ast.Variable) for k, v := range tc.ConfigVariables { - vars[k] = lang.Variable{Value: v, Type: ast.TypeString} + vars[k] = ast.Variable{Value: v, Type: ast.TypeString} } if err := c.Interpolate(vars); err != nil { @@ -2585,9 +2584,9 @@ func TestSchemaMap_Validate(t *testing.T) { t.Fatalf("err: %s", err) } if tc.Vars != nil { - vars := make(map[string]lang.Variable) + vars := make(map[string]ast.Variable) for k, v := range tc.Vars { - vars[k] = lang.Variable{Value: v, Type: ast.TypeString} + vars[k] = ast.Variable{Value: v, Type: ast.TypeString} } if err := c.Interpolate(vars); err != nil { diff --git a/terraform/context.go b/terraform/context.go index 36db6c466..50ad2a2bc 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -11,7 +11,6 @@ import ( "sync/atomic" "github.com/hashicorp/terraform/config" - "github.com/hashicorp/terraform/config/lang" "github.com/hashicorp/terraform/config/lang/ast" "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/depgraph" @@ -1522,9 +1521,9 @@ func (c *walkContext) computeVars( } // Copy the default variables - vs := make(map[string]lang.Variable) + vs := make(map[string]ast.Variable) for k, v := range c.defaultVariables { - vs[k] = lang.Variable{ + vs[k] = ast.Variable{ Value: v, Type: ast.TypeString, } @@ -1537,7 +1536,7 @@ func (c *walkContext) computeVars( switch v.Type { case config.CountValueIndex: if r != nil { - vs[n] = lang.Variable{ + vs[n] = ast.Variable{ Value: int(r.CountIndex), Type: ast.TypeInt, } @@ -1545,7 +1544,7 @@ func (c *walkContext) computeVars( } case *config.ModuleVariable: if c.Operation == walkValidate { - vs[n] = lang.Variable{ + vs[n] = ast.Variable{ Value: config.UnknownVariableValue, Type: ast.TypeString, } @@ -1557,7 +1556,7 @@ func (c *walkContext) computeVars( return err } - vs[n] = lang.Variable{ + vs[n] = ast.Variable{ Value: value, Type: ast.TypeString, } @@ -1571,26 +1570,26 @@ func (c *walkContext) computeVars( v.FullKey(), err) } - vs[n] = lang.Variable{ + vs[n] = ast.Variable{ Value: wd, Type: ast.TypeString, } case config.PathValueModule: if t := c.Context.module.Child(c.Path[1:]); t != nil { - vs[n] = lang.Variable{ + vs[n] = ast.Variable{ Value: t.Config().Dir, Type: ast.TypeString, } } case config.PathValueRoot: - vs[n] = lang.Variable{ + vs[n] = ast.Variable{ Value: c.Context.module.Config().Dir, Type: ast.TypeString, } } case *config.ResourceVariable: if c.Operation == walkValidate { - vs[n] = lang.Variable{ + vs[n] = ast.Variable{ Value: config.UnknownVariableValue, Type: ast.TypeString, } @@ -1608,14 +1607,14 @@ func (c *walkContext) computeVars( return err } - vs[n] = lang.Variable{ + vs[n] = ast.Variable{ Value: attr, Type: ast.TypeString, } case *config.UserVariable: val, ok := c.Variables[v.Name] if ok { - vs[n] = lang.Variable{ + vs[n] = ast.Variable{ Value: val, Type: ast.TypeString, } @@ -1623,7 +1622,7 @@ func (c *walkContext) computeVars( } if _, ok := vs[n]; !ok && c.Operation == walkValidate { - vs[n] = lang.Variable{ + vs[n] = ast.Variable{ Value: config.UnknownVariableValue, Type: ast.TypeString, } @@ -1634,7 +1633,7 @@ func (c *walkContext) computeVars( // those are map overrides. Include those. for k, val := range c.Variables { if strings.HasPrefix(k, v.Name+".") { - vs["var."+k] = lang.Variable{ + vs["var."+k] = ast.Variable{ Value: val, Type: ast.TypeString, }