ytt icon indicating copy to clipboard operation
ytt copied to clipboard

enhance structs to support iteration, bracket accessor, ...

Open cppforlife opened this issue 4 years ago • 2 comments

currently we use struct package provided by starlark. it seems that it lacks few things that would make it friendlier to use (especially for case of data.values).

  • [ ] support iteration (e.g. for k in st)
  • [ ] support [] accessor (e.g. st["foo"] in addition to st.foo)
  • [ ] in check (e.g. "foo" in st)
  • [ ] anything else that we have done for yaml fragments https://github.com/k14s/ytt/blob/develop/pkg/yamltemplate/starlark_fragment.go?

cppforlife avatar Sep 25 '20 14:09 cppforlife

Started working on this. Thought I was done when struct.encode() was working as described in this issue. However, while writing up corresponding docs, discovered that struct.make() is an entirely different code-path. Will address that bit too.

jtigger avatar Oct 05 '20 02:10 jtigger

Just noting here that iterating over maps for this use-case does appear to work properly with ytt 0.31.0.

#@data/values
---
env:
    KUBE_SECRET: my-registry-token  #! does not yet exist -- will be created in the same Namespace
    SYNC_PERIOD: 3600  #! tokens expire; refresh faster than that
---
#! ... abbreviated

        env:
        #@ for k in data.values.env:
        - name: #@ str(k)
          value: #@ str(data.values.env[k])
        #@ end

This is common with config for k8s Deployments.

I'm not knowledgeable whether maps and structs are similar or related in Starlark, and there may be other technicalities that mean they are not fully iterable yet.

Either way, this wasn't clear from these examples:

  • https://carvel.dev/ytt/#example:example-for
  • https://carvel.dev/ytt/#example:example-datatypes

stealthybox avatar Mar 22 '21 20:03 stealthybox