google / google/jsonnet

Syntax for optional array concatenation

Open
#234 13 comments 3 reactions 0 assignees View on GitHub
enhancement
Dominant language
Jsonnet
Stars
7.6k
Forks
475
PR merge metrics
No merged PRs in 30d

Description

At Box we've often had a need to optionally include elements in an array, e.g.

```
util.list([
namespace.object(),
deployment.object(logtailer),
service.object(),
] + logtailer.configMaps
+ if cluster.isDSV31 then [ logback.configMap() ] else []
)
```

While the current approach is effective it has two downsides:
1. It isn't super readable.
2. The tendency is to prepend or append the items so you don't have to break up the list to insert an array.

Either some guidance on how best to handle cases like this, or an extension to the syntax would be nice. I talked to @sparkprime yesterday in slack and he spun up a suggestion to talk about:

```
util.list([
namespace.object(),
configMap for configMap in logtailer.configMaps,
logback.configMap() if cluster.isDSV31,
deployment.object(logtailer),
service.object(),
])
```

Which is much more readable and deals with #1 and #2.

We began talking about potential implementations for this, and explored the idea of an undefined type/value which stays in the list until render time (and comparison) at which point it is removed. That idea has a lot of merit and would even allow functions to return undefined thereby pushing certain branching logic down, but there are obviously a lot of interesting corner cases.

Dave also mentioned that if such a proposal went through he'd probably want to move from
{ [if false then "foo"]: "bar" } to { foo: "bar" if false }.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.