google / google/jsonnet

Thoughts on folded text block?

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

Description

This is cross-posted at [google/go-jsonnet](https://github.com/google/go-jsonnet/issues/538).

YAML has folded-style multi-line strings, which means that new-lines are replaced with spaces to end up with a single-line string. I find this convenient for splitting long shell commands over multiple lines for readability. Does this seem like something potentially good to implement?

I know that jsonnet has the `|||` text block but this does not replace newlines, behaving like a "literal" instead of "folded" YAML multi-line string. Syntax for a "folded" text block in jsonnet could be an extension of the text block syntax: e.g. `|||>`.

For example and context, I'm looking at using jsonnet to produce YAML Gitlab CI config files. By hand a simplified version looks like this:

```yaml
build:
stage: build
only:
- main
image: my.kaniko.image
script:
- init-kaniko
- >-
/kaniko/executor
--context .
--dockerfile Dockerfile
--destination $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
--destination $CI_REGISTRY_IMAGE:latest
```

To reproduce this in jsonnet I need to use a function to replace the newlines:

``` jsonnet
local folded_text_block(text_block) = std.strReplace(text_block, '\n', ' ');

{
build: {
stage: 'build',
only: ['main'],
image: 'my.kaniko.image',
script: [
'init-kaniko',
folded_text_block(
|||
/kaniko/executor
--context .
--dockerfile Dockerfile
--destination $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
--destination $CI_REGISTRY_IMAGE:latest
|||
),
],
},
}
```

Whereas with a builtin folded text block (using my made-up syntax from above) I could do:

``` jsonnet
{
build: {
stage: 'build',
only: ['main'],
image: 'my.kaniko.image',
script: [
'init-kaniko',
|||>
/kaniko/executor
--context .
--dockerfile Dockerfile
--destination $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
--destination $CI_REGISTRY_IMAGE:latest
|||,
],
},
}
```

The "long line" in the above example is also pretty tame and could be squished into a single line. Some commands are far longer and simply must be split over multiple lines for readability, and I think a "folded" text block could make these easy handle.

This is a small deal in the scheme of things. I get a lot of joy from the flexibility of YAML's multi-line strings and would love to see some of it in jsonnet.

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.