Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for YAML list syntax in loops #194

Open
bobbygryzynger opened this issue Nov 18, 2024 · 4 comments
Open

Support for YAML list syntax in loops #194

bobbygryzynger opened this issue Nov 18, 2024 · 4 comments

Comments

@bobbygryzynger
Copy link

Thanks for your presentation at KubeCon. I'm interested in how yamlscript could function as a templating engine on top of YAML.

While playing around with some examples with this in mind, I found the following behavior surprising.

Given the following program:

$ cat <<YS | ys -Ys -
!yamlscript/v0/data
foo:
  :each i (1 .. 3):
    i
YS

The following is produced:

foo:
- 1
- 2
- 3

When considering ys as a templating engine for YAML, support for the following syntax feels more natural:

$ cat <<YS | ys -Ys -
!yamlscript/v0/data
foo:
  :each i (1 .. 3):
    - i
YS

Here, there is an explicit list syntax rather than the list syntax output being implicit. However, a program in this form results in

Compile error: Sequences (block and flow) not allowed in code mode

Any chance you would support this more explicit syntax? This support would likely feel more familiar to helm users currently leveraging go templates.

@ingydotnet
Copy link
Member

ingydotnet commented Nov 20, 2024

Greetings! Thanks for trying things out, asking questions and making suggestions, love it!

Just recovering from KubeCon here.

It's interesting to me how you formed that expression. The :expr ...: is new. I wouldn't have thought to do it that way. Cool!

I would have:

!yamlscript/v0/data
foo::
  each i (1 .. 3): i

Start in data mode. :: toggles to code mode. You are writing code now in this scop unless you toggle back.

We made an early decision to not allow {...} or [...] or - ... in code mode, to make YAML mixed with YS code easier to recognize what's going on (what the mode is at some point). Those things aren't needed by code. Just block mappings and scalars.

You can do this but the result is different:

$ cat <<YS | ys -Ys -
!yamlscript/v0/data
foo:
  :each i (1 .. 3)::
    - ! i
YS
foo:
- - 1
- - 2
- - 3

Here we switched to data mode to use - i but now i is data ("i") so need a ! to make it code (a variable).

I think you are better off to get more comfortable with YS code. It's powerful and concise and becoming more so.

But we're always keen on ways to improve, so keep the ideas coming please.

@ingydotnet
Copy link
Member

Also make sure you play around with ys -c ... and ys -cd ... as you go.
It will help solidify your mental model of what your code is doing...

@ingydotnet
Copy link
Member

ingydotnet commented Nov 20, 2024

Thinking about "templating", YS is good at it in various contexts.
For example: https://github.com/ingydotnet/yamlscript-vs-rosetta/blob/main/bin/ys-vs-rc#L101-L114

Related to what you posted, this:

!yamlscript/v0/data

nums =: 10 .. 20
list =: |
  - $(nums.0)
  - $(nums.3)
  - $(nums.7)

foo:: list:yaml/load

produces:

foo:
- 10
- 13
- 17

Play around with that and see what you can come up with. Keen to hear about it!

@ingydotnet
Copy link
Member

That last one was only meant to show that there are lots and lots of ways to accomplish things in YS.
Not intending you use that for what you asked, but maybe stirs up ideas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants