Skip to content

Commit 20563c9

Browse files
committed
starlark/types: computed, new fn function to wrap computed values
1 parent 1b14da3 commit 20563c9

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

starlark/runtime/runtime.go

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime {
5454
"provisioner": types.BuiltinProvisioner(pm),
5555
"backend": types.BuiltinBackend(),
5656
"hcl": types.BuiltinHCL(),
57+
"fn": types.BuiltinFunctionComputed(),
5758
"struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
5859
},
5960
}

starlark/types/computed.go

+25
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,28 @@ func (c *Computed) Len() int {
118118

119119
return 1024
120120
}
121+
122+
func BuiltinFunctionComputed() starlark.Value {
123+
return starlark.NewBuiltin("fn", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
124+
var function starlark.String
125+
var computed *Computed
126+
switch len(args) {
127+
case 2:
128+
var ok bool
129+
function, ok = args.Index(0).(starlark.String)
130+
if !ok {
131+
return nil, fmt.Errorf("expected string, got %s", args.Index(0).Type())
132+
}
133+
134+
computed, ok = args.Index(1).(*Computed)
135+
if !ok {
136+
return nil, fmt.Errorf("expected Computed, got %s", args.Index(1).Type())
137+
}
138+
default:
139+
return nil, fmt.Errorf("unexpected positional arguments count")
140+
}
141+
142+
path := fmt.Sprintf("%s(%s)", function.GoString(), computed.path)
143+
return NewComputedWithPath(computed.r, computed.t, computed.name, path), nil
144+
})
145+
}

starlark/types/provider.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ func BuiltinProvider(pm *terraform.PluginManager) starlark.Value {
2020
var ok bool
2121
alias, ok = args.Index(2).(starlark.String)
2222
if !ok {
23-
return nil, fmt.Errorf("expected string, go %s", args.Index(2).Type())
23+
return nil, fmt.Errorf("expected string, got %s", args.Index(2).Type())
2424
}
2525
fallthrough
2626
case 2:
2727
var ok bool
2828
version, ok = args.Index(1).(starlark.String)
2929
if !ok {
30-
return nil, fmt.Errorf("expected string, go %s", args.Index(1).Type())
30+
return nil, fmt.Errorf("expected string, got %s", args.Index(1).Type())
3131
}
3232
fallthrough
3333
case 1:

starlark/types/provider_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func doTest(t *testing.T, filename string) {
6262
"provisioner": BuiltinProvisioner(pm),
6363
"backend": BuiltinBackend(),
6464
"hcl": BuiltinHCL(),
65+
"fn": BuiltinFunctionComputed(),
6566
}
6667

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

starlark/types/testdata/computed.star

+3
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ gcp = provider("google", "3.13.0")
3535
# computed on list with MaxItem:1
3636
cluster = gcp.resource.container_cluster("foo")
3737
assert.eq(str(cluster.master_auth.client_certificate), '"${google_container_cluster.foo.master_auth.0.client_certificate}"')
38+
39+
# fn wrapping
40+
assert.eq(str(fn("base64encode", web.ami)), '"${base64encode(data.aws_ami.id_3.id)}"')

0 commit comments

Comments
 (0)