Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indenting a YAML sequence inside a mapping

Tags:

yaml

Should the following be valid?

parent:
- child
- child

So what we have is a sequence of values inside a mapping.

The specific question is about whether the indentation for the 2nd and 3rd lines is valid. The Ruby YAML.dump generated this code, but the Yaml parser here rejects it, because the child lines are not indented.

i.e. it wants something like:

parent:
  - child
  - child

Who is right?

Looking at the YAML spec, it's certainly not obvious, and the line

The “-”, “?” and “:” characters used to denote block collection entries are perceived by people to be part of the indentation

doesn't help much.

like image 581
Charliemouse Avatar asked May 10 '11 16:05

Charliemouse


People also ask

Which is a valid YAML indentation?

The valid YAML indentation is shown below − a: b: - c - d - e f: "ghi" You should remember the following rules while working with indentation in YAML:Flow blocks must be intended with at least some spaces with surrounding current block level. Flow content of YAML spans multiple lines.

What are sequences in YAML?

Sequences can also be known as lists, arrays. Along with mapping, YAML can also be considered as a collection. In YAML, the collection is represented with proper sequence styles. The first example shows the collection. It contains a list under roles. The datacenter mapping, location, cab, and cab_unit are also a collection.

What are the different types of YAML features?

In this file, we actually write a pretty typical YAML file using just two basic YAML features: mapping, sequences, and combination of mapping and sequences.

What is the ordered sequence of nodes in YAML structure block style?

# Ordered sequence of nodes in YAML STRUCTURE Block style: !!seq - Mercury # Rotates - no light/dark sides. - Venus # Deadliest.


1 Answers

Yes, that is legal YAML. The relevant text from the spec is here:

Since people perceive the “-” indicator as indentation, nested block sequences may be indented by one less space to compensate, except, of course, if nested inside another block sequence (block-out context vs. block-in context).

and the subsequent example 8.22:

sequence: !!seq
- entry
- !!seq
 - nested
mapping: !!map
 foo: bar
like image 101
Jesse Beder Avatar answered Sep 18 '22 06:09

Jesse Beder