mirror of
https://github.com/mcuadros/ascode
synced 2024-05-09 00:56:14 +02:00
starlark/runtime: fix repl and add math and time modules
This commit is contained in:
parent
5609ea2c19
commit
f8e6cb55d2
2
Makefile
2
Makefile
|
@ -16,6 +16,8 @@ RUNTIME_MODULES = \
|
|||
github.com/qri-io/starlib/encoding/json \
|
||||
github.com/qri-io/starlib/encoding/yaml \
|
||||
github.com/qri-io/starlib/re \
|
||||
github.com/qri-io/starlib/time \
|
||||
github.com/qri-io/starlib/math \
|
||||
github.com/qri-io/starlib/http
|
||||
|
||||
QUERY_GO_MOD_CMD = go run _scripts/query-go-mod.go
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
{{ template "functionName" . }}
|
||||
```go
|
||||
{{if ne .Receiver "types" -}}{{.Receiver}}.{{- end }}{{ .Signature }}
|
||||
{{if ne .Receiver "" -}}{{.Receiver}}.{{- end }}{{ .Signature }}
|
||||
```
|
||||
|
||||
{{- if ne .Description "" }}
|
||||
|
|
|
@ -14,7 +14,9 @@ import (
|
|||
"github.com/qri-io/starlib/encoding/json"
|
||||
"github.com/qri-io/starlib/encoding/yaml"
|
||||
"github.com/qri-io/starlib/http"
|
||||
"github.com/qri-io/starlib/math"
|
||||
"github.com/qri-io/starlib/re"
|
||||
"github.com/qri-io/starlib/time"
|
||||
"go.starlark.net/repl"
|
||||
"go.starlark.net/resolve"
|
||||
"go.starlark.net/starlark"
|
||||
|
@ -25,10 +27,17 @@ func init() {
|
|||
resolve.AllowRecursion = true
|
||||
resolve.AllowFloat = true
|
||||
resolve.AllowGlobalReassign = true
|
||||
resolve.AllowLambda = true
|
||||
resolve.AllowNestedDef = true
|
||||
resolve.AllowSet = true
|
||||
}
|
||||
|
||||
// LoadModuleFunc is a concurrency-safe and idempotent function that returns
|
||||
// the module when is called from the `load` funcion.
|
||||
type LoadModuleFunc func() (starlark.StringDict, error)
|
||||
|
||||
// Runtime represents the AsCode runtime, it defines the available modules,
|
||||
// the predeclared globals and handles how the `load` function behaves.
|
||||
type Runtime struct {
|
||||
Terraform *types.Terraform
|
||||
pm *terraform.PluginManager
|
||||
|
@ -39,6 +48,7 @@ type Runtime struct {
|
|||
path string
|
||||
}
|
||||
|
||||
// NewRuntime returns a new Runtime for the given terraform.PluginManager.
|
||||
func NewRuntime(pm *terraform.PluginManager) *Runtime {
|
||||
tf := types.NewTerraform(pm)
|
||||
|
||||
|
@ -55,7 +65,9 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime {
|
|||
"encoding/base64": base64.LoadModule,
|
||||
"encoding/csv": csv.LoadModule,
|
||||
"encoding/yaml": yaml.LoadModule,
|
||||
"math": math.LoadModule,
|
||||
"re": re.LoadModule,
|
||||
"time": time.LoadModule,
|
||||
"http": http.LoadModule,
|
||||
},
|
||||
predeclared: starlark.StringDict{
|
||||
|
@ -71,25 +83,30 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime {
|
|||
}
|
||||
}
|
||||
|
||||
// ExecFile parses, resolves, and executes a Starlark file.
|
||||
func (r *Runtime) ExecFile(filename string) (starlark.StringDict, error) {
|
||||
filename, _ = osfilepath.Abs(filename)
|
||||
r.path, _ = osfilepath.Split(filename)
|
||||
|
||||
thread := &starlark.Thread{Name: "thread", Load: r.load}
|
||||
thread.SetLocal("base_path", r.path)
|
||||
thread.SetLocal(types.PluginManagerLocal, r.pm)
|
||||
r.setLocals(thread)
|
||||
|
||||
return starlark.ExecFile(thread, filename, nil, r.predeclared)
|
||||
}
|
||||
|
||||
// REPL executes a read, eval, print loop.
|
||||
func (r *Runtime) REPL() {
|
||||
thread := &starlark.Thread{Name: "thread", Load: r.load}
|
||||
thread.SetLocal("base_path", r.path)
|
||||
thread.SetLocal(types.PluginManagerLocal, r.pm)
|
||||
r.setLocals(thread)
|
||||
|
||||
repl.REPL(thread, r.predeclared)
|
||||
}
|
||||
|
||||
func (r *Runtime) setLocals(t *starlark.Thread) {
|
||||
t.SetLocal("base_path", r.path)
|
||||
t.SetLocal(types.PluginManagerLocal, r.pm)
|
||||
}
|
||||
|
||||
func (r *Runtime) load(t *starlark.Thread, module string) (starlark.StringDict, error) {
|
||||
if m, ok := r.modules[module]; ok {
|
||||
return m()
|
||||
|
|
|
@ -6,4 +6,14 @@ mod = evaluate("includes/foo.star")
|
|||
print(mod.foo)
|
||||
|
||||
# module constructor
|
||||
module("foo")
|
||||
module("foo")
|
||||
|
||||
# test defined modules
|
||||
load("encoding/json", "json")
|
||||
load("encoding/base64", "base64")
|
||||
load("encoding/csv", "csv")
|
||||
load("encoding/yaml", "yaml")
|
||||
load("math", "math")
|
||||
load("re", "re")
|
||||
load("time", "time")
|
||||
load("http", "http")
|
Loading…
Reference in New Issue