Skip to content

Commit

Permalink
starlark/types: computed, new fn function to wrap computed values
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Mar 17, 2020
1 parent 1b14da3 commit 20563c9
Showing 5 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions starlark/runtime/runtime.go
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime {
"provisioner": types.BuiltinProvisioner(pm),
"backend": types.BuiltinBackend(),
"hcl": types.BuiltinHCL(),
"fn": types.BuiltinFunctionComputed(),
"struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
},
}
25 changes: 25 additions & 0 deletions starlark/types/computed.go
Original file line number Diff line number Diff line change
@@ -118,3 +118,28 @@ func (c *Computed) Len() int {

return 1024
}

func BuiltinFunctionComputed() starlark.Value {
return starlark.NewBuiltin("fn", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var function starlark.String
var computed *Computed
switch len(args) {
case 2:
var ok bool
function, ok = args.Index(0).(starlark.String)
if !ok {
return nil, fmt.Errorf("expected string, got %s", args.Index(0).Type())
}

computed, ok = args.Index(1).(*Computed)
if !ok {
return nil, fmt.Errorf("expected Computed, got %s", args.Index(1).Type())
}
default:
return nil, fmt.Errorf("unexpected positional arguments count")
}

path := fmt.Sprintf("%s(%s)", function.GoString(), computed.path)
return NewComputedWithPath(computed.r, computed.t, computed.name, path), nil
})
}
4 changes: 2 additions & 2 deletions starlark/types/provider.go
Original file line number Diff line number Diff line change
@@ -20,14 +20,14 @@ func BuiltinProvider(pm *terraform.PluginManager) starlark.Value {
var ok bool
alias, ok = args.Index(2).(starlark.String)
if !ok {
return nil, fmt.Errorf("expected string, go %s", args.Index(2).Type())
return nil, fmt.Errorf("expected string, got %s", args.Index(2).Type())
}
fallthrough
case 2:
var ok bool
version, ok = args.Index(1).(starlark.String)
if !ok {
return nil, fmt.Errorf("expected string, go %s", args.Index(1).Type())
return nil, fmt.Errorf("expected string, got %s", args.Index(1).Type())
}
fallthrough
case 1:
1 change: 1 addition & 0 deletions starlark/types/provider_test.go
Original file line number Diff line number Diff line change
@@ -62,6 +62,7 @@ func doTest(t *testing.T, filename string) {
"provisioner": BuiltinProvisioner(pm),
"backend": BuiltinBackend(),
"hcl": BuiltinHCL(),
"fn": BuiltinFunctionComputed(),
}

_, err := starlark.ExecFile(thread, filename, nil, predeclared)
3 changes: 3 additions & 0 deletions starlark/types/testdata/computed.star
Original file line number Diff line number Diff line change
@@ -35,3 +35,6 @@ gcp = provider("google", "3.13.0")
# computed on list with MaxItem:1
cluster = gcp.resource.container_cluster("foo")
assert.eq(str(cluster.master_auth.client_certificate), '"${google_container_cluster.foo.master_auth.0.client_certificate}"')

# fn wrapping
assert.eq(str(fn("base64encode", web.ami)), '"${base64encode(data.aws_ami.id_3.id)}"')

0 comments on commit 20563c9

Please sign in to comment.