mirror of
https://github.com/mcuadros/ascode
synced 2024-11-26 06:01:08 +01:00
starlark/types: Attribute.Get, support for map attributes
This commit is contained in:
parent
8b962d26cc
commit
6fc80cda96
@ -155,9 +155,35 @@ func (c *Attribute) Index(i int) starlark.Value {
|
||||
return NewAttributeWithPath(c.r, *c.t.ListElementType(), c.name, path)
|
||||
}
|
||||
|
||||
if c.t.IsMapType() {
|
||||
return NewAttributeWithPath(c.r, c.t, c.name, path)
|
||||
}
|
||||
|
||||
return starlark.None
|
||||
}
|
||||
|
||||
// Get honors the starlark.Mapping interface.
|
||||
func (c *Attribute) Get(key starlark.Value) (v starlark.Value, found bool, err error) {
|
||||
switch vKey := key.(type) {
|
||||
case starlark.Int:
|
||||
if !c.t.IsSetType() && !c.t.IsListType() && !c.t.IsMapType() {
|
||||
return nil, false, fmt.Errorf("%s does not support index", c.name)
|
||||
}
|
||||
|
||||
index, _ := vKey.Int64()
|
||||
return c.Index(int(index)), true, nil
|
||||
case starlark.String:
|
||||
if !c.t.IsMapType() {
|
||||
return nil, false, fmt.Errorf("%s it's not a dict", c.name)
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s.%s", c.path, vKey.GoString())
|
||||
return NewAttributeWithPath(c.r, c.t, c.name, path), true, nil
|
||||
default:
|
||||
return nil, false, fmt.Errorf("%s: unexpected key type %s", c.name, key.Type())
|
||||
}
|
||||
}
|
||||
|
||||
// Len honors the starlark.Indexable interface.
|
||||
func (c *Attribute) Len() int {
|
||||
if !c.t.IsSetType() && !c.t.IsListType() {
|
||||
|
8
starlark/types/testdata/attribute.star
vendored
8
starlark/types/testdata/attribute.star
vendored
@ -47,6 +47,12 @@ assert.eq(str(cluster.master_auth.client_certificate), "${google_container_clust
|
||||
# attr non-object
|
||||
assert.fails(lambda: web.ami.foo, "Attribute<string> it's not a object")
|
||||
|
||||
|
||||
# fn wrapping
|
||||
assert.eq(str(fn("base64encode", web.ami)), "${base64encode(data.aws_ami.id_2.id)}")
|
||||
|
||||
# attribute of dict
|
||||
k8s = tf.provider("kubernetes")
|
||||
|
||||
secret = k8s.data.secret("foo")
|
||||
assert.eq(str(secret.data["qux"]), "${data.kubernetes_secret.foo.data.qux}")
|
||||
assert.eq(str(secret.data["qux"][0]), "${data.kubernetes_secret.foo.data.qux.0}")
|
||||
|
Loading…
Reference in New Issue
Block a user