Skip to content

Commit 35e96bd

Browse files
committed
Elaborate on YAML nesting
1 parent cffde0c commit 35e96bd

File tree

1 file changed

+98
-41
lines changed

1 file changed

+98
-41
lines changed

docs/config/development/config-data.rst

Lines changed: 98 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -357,38 +357,65 @@ Nesting Objects
357357
Because values in maps and items in lists can be of any type, It's
358358
possible to nest maps in maps, lists in lists, lists in maps, and so on.
359359

360-
Here is an example of a ``Map`` contained within the value of another
361-
``Map`` (which is the top level object):
360+
.. tip::
361+
362+
Defining something inside something else is commonly referred to as 'nesting'.
362363

363364
.. tab-set::
364365

365366
.. tab-item:: YAML
366367

367-
.. admonition:: Info
368-
:class: tip
368+
When setting the value of a map, typically it will just fit on the
369+
same line as the key, for example the ``Float`` 42 can just be written
370+
in-line with the key, after the colon like so:
371+
372+
.. code-block:: yaml
369373
370-
For simple data types like integers and strings it is clear which key
371-
corresponds to which value, as they are typically contained on the same
372-
line, but maps and lists may span multiple lines, so we need a way of
373-
defining which objects are defined under which keys and items. In YAML,
374-
we can specify this kind of relationship via *indentation* - which is
375-
simply how many spaces come before the key one a line. We conventionally
376-
use two spaces to indicate 'one level' of indentation in YAML configs.
374+
key: 42
375+
376+
Types that can span multiple lines, such as maps and lists won't fit
377+
on a single line. For example you man want the following map which spans
378+
multiple lines to be a value within another map:
377379

378380
.. code-block:: yaml
379-
:caption: config.yml
380-
:linenos:
381381
382-
parent-key:
383-
child-key: value
384-
sibling-key: another value
382+
foo: a
383+
bar: b
384+
385+
To nest this map as a value of a key, say ``baz``, in another map, it can be
386+
defined under the key with additional indentation like so:
387+
388+
.. code-block:: yaml
389+
390+
baz:
391+
foo: a
392+
bar: b
393+
394+
Indentation / indenting text refers to having some consistent number of spaces before
395+
each line in text. In YAML, the recommended number of spaces to indent is 2 as shown above.
396+
397+
Lists can be nested similarly like so:
398+
399+
.. code-block:: yaml
400+
401+
my-list:
402+
- item 1
403+
- item 2
385404
386-
You can see that the map containing ``child-key`` and ``sibling-key`` is
387-
indented by two spaces, and is defined under the ``parent-key`` key,
388-
signifying that it belongs to that key.
405+
Multiple 'levels of indentation' can be used, for example here is the prior map further
406+
nested under (as the value for the key) ``qux``:
407+
408+
.. code-block:: yaml
409+
410+
qux:
411+
baz:
412+
foo: a
413+
bar: b
389414
390415
.. tab-item:: JSON
391416

417+
Example of a ``Map`` defined in a ``Map``:
418+
392419
.. code-block:: json
393420
:caption: config.json
394421
:linenos:
@@ -400,36 +427,66 @@ Here is an example of a ``Map`` contained within the value of another
400427
}
401428
}
402429
403-
And here is a ``Map`` (the top level object) containing a ``List`` of
404-
``String``\ s:
405-
406-
.. tab-set::
407-
408-
.. tab-item:: YAML
409-
410-
.. code-block:: yaml
411-
:caption: config.yml
412-
:linenos:
413-
414-
list of strings:
415-
- item 1
416-
- item 2
417-
- item 3
418-
419-
.. tab-item:: JSON
430+
Example of a ``List`` defined in a ``Map``:
420431

421432
.. code-block:: json
422433
:caption: config.json
423434
:linenos:
424-
435+
425436
{
426-
"list of strings": [
427-
"item 1",
428-
"item 2",
429-
"item 3"
437+
"my-list": [
438+
"item 1",
439+
"item 2"
430440
]
431441
}
432442
443+
Illegally defining two values for one key
444+
-----------------------------------------
445+
446+
A common mistake in YAML is to accidentally assign two different
447+
values to the same key.
448+
449+
For example the following is invalid:
450+
451+
.. code-block:: yaml
452+
453+
key: foo
454+
baz: bar
455+
456+
The reason this is invalid is because there are two competing values being
457+
assigned to ``key``, which are ``foo`` and the map containing ``baz: bar``.
458+
459+
Deleting one of the values would make this valid YAML:
460+
461+
.. code-block:: yaml
462+
463+
key:
464+
baz: bar
465+
466+
Or
467+
468+
.. code-block:: yaml
469+
470+
key: foo
471+
472+
A config might end up in this invalid state for many reasons.
473+
474+
A key may have been deleted or omitted which could be remedied by re-adding it like so:
475+
476+
.. code-block:: yaml
477+
478+
key: foo
479+
missing:
480+
baz: bar
481+
482+
Indentation may have been changed by accident, for example removing indentation
483+
would make it valid like so:
484+
485+
.. code-block:: yaml
486+
487+
key: foo
488+
baz: bar
489+
433490
Combining Everything
434491
====================
435492

0 commit comments

Comments
 (0)