From bac0f66196010a3cbf519d98fe46d1959fc94d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Sun, 12 Apr 2020 22:15:28 +0200 Subject: [PATCH] starlark/types: evaluate include main thread predeclared variables --- starlark/runtime/runtime.go | 21 +++++++++++---------- starlark/types/evaluate.go | 3 +-- starlark/types/provider_test.go | 20 +++++++++++--------- starlark/types/testdata/evaluate/test.star | 3 ++- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/starlark/runtime/runtime.go b/starlark/runtime/runtime.go index ed411e5..7092dd2 100644 --- a/starlark/runtime/runtime.go +++ b/starlark/runtime/runtime.go @@ -51,6 +51,16 @@ type Runtime struct { // NewRuntime returns a new Runtime for the given terraform.PluginManager. func NewRuntime(pm *terraform.PluginManager) *Runtime { tf := types.NewTerraform(pm) + predeclared := starlark.StringDict{} + predeclared["tf"] = tf + predeclared["provisioner"] = types.BuiltinProvisioner() + predeclared["backend"] = types.BuiltinBackend() + predeclared["validate"] = types.BuiltinValidate() + predeclared["hcl"] = types.BuiltinHCL() + predeclared["fn"] = types.BuiltinFunctionAttribute() + predeclared["evaluate"] = types.BuiltinEvaluate(predeclared) + predeclared["struct"] = starlark.NewBuiltin("struct", starlarkstruct.Make) + predeclared["module"] = starlark.NewBuiltin("module", starlarkstruct.MakeModule) return &Runtime{ Terraform: tf, @@ -70,16 +80,7 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime { "time": time.LoadModule, "http": http.LoadModule, }, - predeclared: starlark.StringDict{ - "tf": tf, - "provisioner": types.BuiltinProvisioner(), - "backend": types.BuiltinBackend(), - "hcl": types.BuiltinHCL(), - "fn": types.BuiltinFunctionAttribute(), - "evaluate": types.BuiltinEvaluate(), - "struct": starlark.NewBuiltin("struct", starlarkstruct.Make), - "module": starlark.NewBuiltin("module", starlarkstruct.MakeModule), - }, + predeclared: predeclared, } } diff --git a/starlark/types/evaluate.go b/starlark/types/evaluate.go index 86c1a18..6b56d65 100644 --- a/starlark/types/evaluate.go +++ b/starlark/types/evaluate.go @@ -25,10 +25,9 @@ import ( // Defines the predeclared context for the execution. Execution does // not modify this dictionary // -func BuiltinEvaluate() starlark.Value { +func BuiltinEvaluate(predeclared starlark.StringDict) starlark.Value { return starlark.NewBuiltin("evaluate", func(t *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { var raw starlark.String - predeclared := starlark.StringDict{} switch len(args) { case 2: diff --git a/starlark/types/provider_test.go b/starlark/types/provider_test.go index 1e73e6e..8dd9dae 100644 --- a/starlark/types/provider_test.go +++ b/starlark/types/provider_test.go @@ -13,6 +13,7 @@ import ( "github.com/mcuadros/ascode/terraform" "go.starlark.net/resolve" "go.starlark.net/starlark" + "go.starlark.net/starlarkstruct" ) var id int @@ -75,15 +76,16 @@ func doTestPrint(t *testing.T, filename string, print func(*starlark.Thread, str test.SetReporter(thread, t) - predeclared := starlark.StringDict{ - "provisioner": BuiltinProvisioner(), - "backend": BuiltinBackend(), - "hcl": BuiltinHCL(), - "fn": BuiltinFunctionAttribute(), - "evaluate": BuiltinEvaluate(), - "validate": BuiltinValidate(), - "tf": NewTerraform(pm), - } + predeclared := starlark.StringDict{} + predeclared["tf"] = NewTerraform(pm) + predeclared["provisioner"] = BuiltinProvisioner() + predeclared["backend"] = BuiltinBackend() + predeclared["hcl"] = BuiltinHCL() + predeclared["validate"] = BuiltinValidate() + predeclared["fn"] = BuiltinFunctionAttribute() + predeclared["evaluate"] = BuiltinEvaluate(predeclared) + predeclared["struct"] = starlark.NewBuiltin("struct", starlarkstruct.Make) + predeclared["module"] = starlark.NewBuiltin("module", starlarkstruct.MakeModule) _, err := starlark.ExecFile(thread, filename, nil, predeclared) if err != nil { diff --git a/starlark/types/testdata/evaluate/test.star b/starlark/types/testdata/evaluate/test.star index 1c6bae4..a0c2247 100644 --- a/starlark/types/testdata/evaluate/test.star +++ b/starlark/types/testdata/evaluate/test.star @@ -1 +1,2 @@ -foo = bar \ No newline at end of file +foo = bar +tf \ No newline at end of file