0

YAML is supposed to be a "human-friendly" configuration language, however "simple" deeply nested configuration values can sometimes end up looking kind of ugly and hard to read:

system:
    config:
        subsystem:
            project:
                name: "coolproject"
                version: "1.0.0"

stages:
    - something:
          name: "foo"
    - something:
          name: "bar"
    - something:
          name: "baz"

Is there some way to collapse such deeply nested configurations? In particular, I would like to be able to "squash" the levels in simple nested structures (levels that have only one child).

Ideally, I would have liked to be able to do something along the lines of

system: config: subsystem: project:
    name: "coolproject"
    version: "1.0.0"

stages:
    - something: name: "foo"
    - something: name: "bar"
    - something: name: "baz"

but this isn't valid YAML. Other attempts like system:config:subsystem:project: (without spaces), system.config.subsystem.project: and system/config/subsystem/project: didn't work either (well, they did work, but they are represented as keys with colons, dots or slashes in them, rather than nested stuctures).

A cursory reading of the spec also seems to indicate that there is no support for such a thing, but I still decided to ask this question, in case I have missed some clever trick or workaround.

RuRo
  • 311
  • 4
  • 17

1 Answers1

1

YAML is supposed to be a "human-friendly" configuration language

A common misconception. YAML, according to the spec, is

a human-friendly, cross language, Unicode based data serialization language

While setting single deeply nested values is common in configuration files (because they are usually sparse), it's an exotic use-case in data serialization (which isn't usually sparse), which is why YAML is not terribly good at doing that.

You can employ flow style to have fewer lines:

system: { config: { subsystem: { project: {
        name: "coolproject",
        version: "1.0.0"
    }}}}

stages:
    - something: {name: "foo"}
    - something: {name: "bar"}
    - something: {name: "baz"}

It's not as simple as you might have hoped but the best you can do within YAML's spec. The required indentation of }}}} shows that this is not exactly what YAML was designed for (some implementations allow this unindented, but don't rely on it unless your implementation specifically tells you it's fine).

You might want to look into TOML instead, which does support what you're asking for, albeit with its on caveats (see this question for a comparison).

flyx
  • 35,506
  • 7
  • 89
  • 126