Skip to content

Commit c5436f7

Browse files
committed
starlark/types: evaluate function implementation
1 parent 9ba8f7e commit c5436f7

File tree

7 files changed

+74
-0
lines changed

7 files changed

+74
-0
lines changed

starlark/runtime/runtime.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime {
5858
"backend": types.BuiltinBackend(),
5959
"hcl": types.BuiltinHCL(),
6060
"fn": types.BuiltinFunctionComputed(),
61+
"evaluate": types.BuiltinEvaluate(),
6162
"struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
63+
"module": starlark.NewBuiltin("module", starlarkstruct.MakeModule),
6264
},
6365
}
6466
}
@@ -68,6 +70,8 @@ func (r *Runtime) ExecFile(filename string) (starlark.StringDict, error) {
6870
r.path, _ = osfilepath.Split(filename)
6971

7072
thread := &starlark.Thread{Name: "thread", Load: r.load}
73+
thread.SetLocal("base_path", r.path)
74+
7175
return starlark.ExecFile(thread, filename, nil, r.predeclared)
7276
}
7377

starlark/runtime/testdata/load.star

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
# test relative loading
22
load("includes/foo.star", "foo")
3+
4+
# evaluate and correct base_path
5+
mod = evaluate("includes/foo.star")
6+
print(mod.foo)
7+
8+
# module constructor
9+
module("foo")

starlark/types/evaluate.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package types
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
7+
"go.starlark.net/starlark"
8+
"go.starlark.net/starlarkstruct"
9+
)
10+
11+
func BuiltinEvaluate() starlark.Value {
12+
return starlark.NewBuiltin("evaluate", func(t *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
13+
var raw starlark.String
14+
switch len(args) {
15+
case 1:
16+
var ok bool
17+
raw, ok = args.Index(0).(starlark.String)
18+
if !ok {
19+
return nil, fmt.Errorf("expected string, got %s", args.Index(0).Type())
20+
}
21+
default:
22+
return nil, fmt.Errorf("unexpected positional arguments count")
23+
}
24+
25+
dict := starlark.StringDict{}
26+
for _, kwarg := range kwargs {
27+
dict[kwarg.Index(0).(starlark.String).GoString()] = kwarg.Index(1)
28+
}
29+
30+
filename := raw.GoString()
31+
if base, ok := t.Local("base_path").(string); ok {
32+
filename = filepath.Join(base, filename)
33+
}
34+
35+
_, file := filepath.Split(filename)
36+
name := file[:len(file)-len(filepath.Ext(file))]
37+
38+
global, err := starlark.ExecFile(t, filename, nil, dict)
39+
return &starlarkstruct.Module{
40+
Name: name,
41+
Members: global,
42+
}, err
43+
})
44+
}

starlark/types/evaluate_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package types
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestEvaluate(t *testing.T) {
8+
doTest(t, "testdata/evaluate.star")
9+
}

starlark/types/provider_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func doTest(t *testing.T, filename string) {
5757

5858
log.SetOutput(ioutil.Discard)
5959
thread := &starlark.Thread{Load: load}
60+
thread.SetLocal("base_path", "testdata")
61+
6062
test.SetReporter(thread, t)
6163

6264
pm := &terraform.PluginManager{".providers"}
@@ -67,6 +69,7 @@ func doTest(t *testing.T, filename string) {
6769
"backend": BuiltinBackend(),
6870
"hcl": BuiltinHCL(),
6971
"fn": BuiltinFunctionComputed(),
72+
"evaluate": BuiltinEvaluate(),
7073
}
7174

7275
_, err := starlark.ExecFile(thread, filename, nil, predeclared)

starlark/types/testdata/evaluate.star

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
load("assert.star", "assert")
2+
3+
bar = "bar"
4+
module = evaluate("evaluate/test.star", bar=bar)
5+
assert.eq(str(module), '<module "test">')
6+
assert.eq(module.foo, bar)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo = bar

0 commit comments

Comments
 (0)