cue-lang / docs-and-content

A place to discuss, plan, and track documentation on cuelang.org
5 stars 1 forks source link

docs/howto/conditionally-include-multiple-list-elements: Conditionally including multiple elements in a list #117

Open jpluscplusm opened 2 months ago

jpluscplusm commented 2 months ago

From Slack: https://cuelang.slack.com/archives/CLT4FD7KP/p1712225294101449

We want to include multiple items in a list conditionally.

This works if multiple if blocks are used:

x: true
y: [
    1,
    2,
    if x {3},
    if x {4},
]

... but doesn't work if we try to emit multiple values from the same if clause:

// this CUE fails to evaluate
x: true
y: [
    1,
    2,
    if x {
        3,
        4,
    }
]

@mvdan pointed out an approach which permits just a single if block to be used:

x: true
y: [
    1,
    2,
    if x for e in [
        3,
        4,
    ] {e},
]