1
1
mirror of https://github.com/mcuadros/ascode synced 2024-11-26 06:01:08 +01:00

starlark/types: evaluate include main thread predeclared variables

This commit is contained in:
Máximo Cuadros 2020-04-12 22:15:28 +02:00
parent 1643ed115e
commit bac0f66196
No known key found for this signature in database
GPG Key ID: 17A5DFEDC735AE4B
4 changed files with 25 additions and 22 deletions

@ -51,6 +51,16 @@ type Runtime struct {
// NewRuntime returns a new Runtime for the given terraform.PluginManager. // NewRuntime returns a new Runtime for the given terraform.PluginManager.
func NewRuntime(pm *terraform.PluginManager) *Runtime { func NewRuntime(pm *terraform.PluginManager) *Runtime {
tf := types.NewTerraform(pm) 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{ return &Runtime{
Terraform: tf, Terraform: tf,
@ -70,16 +80,7 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime {
"time": time.LoadModule, "time": time.LoadModule,
"http": http.LoadModule, "http": http.LoadModule,
}, },
predeclared: starlark.StringDict{ predeclared: predeclared,
"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),
},
} }
} }

@ -25,10 +25,9 @@ import (
// Defines the predeclared context for the execution. Execution does // Defines the predeclared context for the execution. Execution does
// not modify this dictionary // 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) { return starlark.NewBuiltin("evaluate", func(t *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var raw starlark.String var raw starlark.String
predeclared := starlark.StringDict{}
switch len(args) { switch len(args) {
case 2: case 2:

@ -13,6 +13,7 @@ import (
"github.com/mcuadros/ascode/terraform" "github.com/mcuadros/ascode/terraform"
"go.starlark.net/resolve" "go.starlark.net/resolve"
"go.starlark.net/starlark" "go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
) )
var id int var id int
@ -75,15 +76,16 @@ func doTestPrint(t *testing.T, filename string, print func(*starlark.Thread, str
test.SetReporter(thread, t) test.SetReporter(thread, t)
predeclared := starlark.StringDict{ predeclared := starlark.StringDict{}
"provisioner": BuiltinProvisioner(), predeclared["tf"] = NewTerraform(pm)
"backend": BuiltinBackend(), predeclared["provisioner"] = BuiltinProvisioner()
"hcl": BuiltinHCL(), predeclared["backend"] = BuiltinBackend()
"fn": BuiltinFunctionAttribute(), predeclared["hcl"] = BuiltinHCL()
"evaluate": BuiltinEvaluate(), predeclared["validate"] = BuiltinValidate()
"validate": BuiltinValidate(), predeclared["fn"] = BuiltinFunctionAttribute()
"tf": NewTerraform(pm), 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) _, err := starlark.ExecFile(thread, filename, nil, predeclared)
if err != nil { if err != nil {

@ -1 +1,2 @@
foo = bar foo = bar
tf