carvel-dev / ytt

YAML templating tool that works on YAML structure instead of text
https://carvel.dev/ytt
Apache License 2.0
1.65k stars 135 forks source link

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

Open cppforlife opened 3 years ago

cppforlife commented 3 years ago

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).

jtigger commented 3 years ago

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.

stealthybox commented 3 years ago

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: