diff --git a/starlark/types/collection.go b/starlark/types/collection.go index 5724897..ed6506d 100644 --- a/starlark/types/collection.go +++ b/starlark/types/collection.go @@ -32,7 +32,7 @@ func (c *ResourceCollection) String() string { // Type honors the starlark.Value interface. func (c *ResourceCollection) Type() string { - return "collection" + return fmt.Sprintf("ResourceCollection<%s.%s>", c.kind, c.typ) } // Truth honors the starlark.Value interface. diff --git a/starlark/types/computed.go b/starlark/types/computed.go index e5898ce..572f677 100644 --- a/starlark/types/computed.go +++ b/starlark/types/computed.go @@ -51,7 +51,7 @@ func NewComputedWithPath(r *Resource, t cty.Type, name, path string) *Computed { } func (*Computed) Type() string { - return "computed" + return "Computed" } func (c *Computed) InnerType() *Type { diff --git a/starlark/types/resource.go b/starlark/types/resource.go index 2205c2c..8172cd9 100644 --- a/starlark/types/resource.go +++ b/starlark/types/resource.go @@ -84,7 +84,7 @@ func (r *Resource) String() string { // Type honors the starlark.Value interface. func (r *Resource) Type() string { - return string(r.kind) + return fmt.Sprintf("Resource<%s.%s>", r.kind, r.typ) } // Truth honors the starlark.Value interface. diff --git a/starlark/types/testdata/computed.star b/starlark/types/testdata/computed.star index 70b9bdc..d4ce7b3 100644 --- a/starlark/types/testdata/computed.star +++ b/starlark/types/testdata/computed.star @@ -5,21 +5,21 @@ aws = provider("aws", "2.13.0") # compute of scalar web = aws.resource.instance() web.ami = aws.data.ami().id -assert.eq(type(web.ami), "computed") -assert.eq(str(web.ami), '"$${data.aws_ami.id_3.id}"') +assert.eq(type(web.ami), "Computed") +assert.eq(str(web.ami), '"${data.aws_ami.id_3.id}"') # compute of set table = aws.data.dynamodb_table() -assert.eq(str(table.ttl), '"$${data.aws_dynamodb_table.id_4.ttl}"') -assert.eq(str(table.ttl[0]), '"$${data.aws_dynamodb_table.id_4.ttl.0}"') -assert.eq(str(table.ttl[0].attribute_name), '"$${data.aws_dynamodb_table.id_4.ttl.0.attribute_name}"') +assert.eq(str(table.ttl), '"${data.aws_dynamodb_table.id_4.ttl}"') +assert.eq(str(table.ttl[0]), '"${data.aws_dynamodb_table.id_4.ttl.0}"') +assert.eq(str(table.ttl[0].attribute_name), '"${data.aws_dynamodb_table.id_4.ttl.0.attribute_name}"') # compute of list instance = aws.data.instance() -assert.eq(str(instance.credit_specification), '"$${data.aws_instance.id_5.credit_specification}"') -assert.eq(str(instance.credit_specification[0]), '"$${data.aws_instance.id_5.credit_specification.0}"') -assert.eq(str(instance.credit_specification[0].cpu_credits), '"$${data.aws_instance.id_5.credit_specification.0.cpu_credits}"') +assert.eq(str(instance.credit_specification), '"${data.aws_instance.id_5.credit_specification}"') +assert.eq(str(instance.credit_specification[0]), '"${data.aws_instance.id_5.credit_specification.0}"') +assert.eq(str(instance.credit_specification[0].cpu_credits), '"${data.aws_instance.id_5.credit_specification.0.cpu_credits}"') # compute of map computed = str(aws.resource.instance().root_block_device.volume_size) -assert.eq(computed, '"$${resource.aws_instance.id_6.root_block_device.volume_size}"') \ No newline at end of file +assert.eq(computed, '"${resource.aws_instance.id_6.root_block_device.volume_size}"') \ No newline at end of file diff --git a/starlark/types/testdata/nested.star b/starlark/types/testdata/nested.star index 510713b..097c209 100644 --- a/starlark/types/testdata/nested.star +++ b/starlark/types/testdata/nested.star @@ -3,11 +3,11 @@ load("assert.star", "assert") p = provider("aws", "2.13.0") d = p.data.ami() -assert.eq(type(d.filter), "collection") +assert.eq(type(d.filter), "ResourceCollection") bar = d.filter(name="bar", values=["qux"]) -assert.eq(type(bar), "nested") +assert.eq(type(bar), "Resource") assert.eq(bar.name, "bar") assert.eq(bar.values, ["qux"]) diff --git a/starlark/types/testdata/provider.star b/starlark/types/testdata/provider.star index f0576e7..b829bdc 100644 --- a/starlark/types/testdata/provider.star +++ b/starlark/types/testdata/provider.star @@ -8,9 +8,9 @@ assert.eq(len(dir(p.resource)), 506) resources = dir(p.resource) assert.contains(resources, "instance") -assert.eq(type(p.resource.instance), "collection") +assert.eq(type(p.resource.instance), "ResourceCollection") +assert.eq(type(p.resource.instance()), "Resource") -p.resource.instance() p.resource.instance() assert.eq(len(p.resource.instance), 2) diff --git a/starlark/types/testdata/resource.star b/starlark/types/testdata/resource.star index 1cf7de5..f42fa86 100644 --- a/starlark/types/testdata/resource.star +++ b/starlark/types/testdata/resource.star @@ -11,11 +11,11 @@ assert.eq(qux.uid, 42) assert.eq(qux.name, None) # attr not-exists -assert.fails(lambda: qux.foo, "data has no .foo field or method") +assert.fails(lambda: qux.foo, "Resource has no .foo field or method") # attr id -assert.eq(type(qux.id), "computed") -assert.eq(str(qux.id), '"$${data.ignition_user.id_14.id}"') +assert.eq(type(qux.id), "Computed") +assert.eq(str(qux.id), '"${data.ignition_user.id_14.id}"') # attr output assignation aws = provider("aws", "2.13.0") @@ -48,7 +48,7 @@ assert.eq(p.data.user(groups=["foo"]), p.data.user(groups=["foo"])) # constructor with name quux = p.data.user("quux") -assert.eq(str(quux.id), '"$${data.ignition_user.quux.id}"') +assert.eq(str(quux.id), '"${data.ignition_user.quux.id}"') # constructor from kwargs bar = p.data.user(uid=42, system=True) @@ -59,7 +59,7 @@ assert.eq(bar.system, True) fred = p.data.user("fred", uid=42, system=True) assert.eq(fred.uid, 42) assert.eq(fred.system, True) -assert.eq(str(fred.id), '"$${data.ignition_user.fred.id}"') +assert.eq(str(fred.id), '"${data.ignition_user.fred.id}"') # constructor from dict foo = p.data.user({"uid": 42, "system": True}) @@ -70,7 +70,7 @@ assert.eq(foo.system, True) baz = p.data.user("baz", {"uid": 42, "system": True}) assert.eq(baz.uid, 42) assert.eq(baz.system, True) -assert.eq(str(baz.id), '"$${data.ignition_user.baz.id}"') +assert.eq(str(baz.id), '"${data.ignition_user.baz.id}"') assert.eq(bar, foo) assert.eq(foo, p.data.user(foo.__dict__)) @@ -95,7 +95,7 @@ user.uid = 42 user.groups = ["foo", "bar"] user.system = True -assert.eq(type(user), "data") +assert.eq(type(user), "Resource") assert.eq(user.__dict__, { "name": "foo", "uid": 42, diff --git a/starlark/types/type.go b/starlark/types/type.go index 60fc88c..e7fbbdb 100644 --- a/starlark/types/type.go +++ b/starlark/types/type.go @@ -2,6 +2,7 @@ package types import ( "fmt" + "strings" "github.com/zclconf/go-cty/cty" "go.starlark.net/starlark" @@ -67,7 +68,7 @@ func (v *Value) Cty() cty.Value { } return cty.ListVal(values) - case "computed": + case "Computed": return cty.StringVal(v.v.(*Computed).GoString()) default: return cty.StringVal(fmt.Sprintf("unhandled: %s", v.t.typ)) @@ -141,6 +142,11 @@ func NewTypeFromStarlark(typ string) (*Type, error) { t := &Type{} t.typ = typ + complex := strings.SplitN(typ, "<", 2) + if len(complex) == 2 { + typ = complex[0] + } + switch typ { case "bool": t.cty = cty.Bool @@ -148,13 +154,11 @@ func NewTypeFromStarlark(typ string) (*Type, error) { t.cty = cty.Number case "string": t.cty = cty.String - case "collection": + case "list", "ResourceCollection": t.cty = cty.List(cty.NilType) - case "nested", "data", "resource": + case "Resource": t.cty = cty.Map(cty.NilType) - case "list": - t.cty = cty.List(cty.NilType) - case "computed": + case "Computed": t.cty = cty.String default: return nil, fmt.Errorf("unexpected %q type", typ) diff --git a/starlark/types/type_test.go b/starlark/types/type_test.go index 771cf3c..ec289ac 100644 --- a/starlark/types/type_test.go +++ b/starlark/types/type_test.go @@ -30,16 +30,10 @@ func TestNewTypeFromStarlark_NonScalar(t *testing.T) { typ := MustTypeFromStarlark("list") assert.True(t, typ.Cty().IsListType()) - typ = MustTypeFromStarlark("collection") + typ = MustTypeFromStarlark("ResourceCollection") assert.True(t, typ.Cty().IsListType()) - typ = MustTypeFromStarlark("data") - assert.True(t, typ.Cty().IsMapType()) - - typ = MustTypeFromStarlark("nested") - assert.True(t, typ.Cty().IsMapType()) - - typ = MustTypeFromStarlark("resource") + typ = MustTypeFromStarlark("Resource") assert.True(t, typ.Cty().IsMapType()) } @@ -77,13 +71,13 @@ func TestTypeValidate(t *testing.T) { {"int", starlark.Float(42.), false}, } - for _, tc := range testCases { + for i, tc := range testCases { typ := MustTypeFromStarlark(tc.t) err := typ.Validate(tc.v) if tc.err { - assert.Error(t, err) + assert.Error(t, err, i) } else { - assert.NoError(t, err) + assert.NoError(t, err, i) } } }