Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrapping a list over multiple lines yaml

Tags:

yaml

In YAML I know that it is possible to wrap a string over multiple lines like this:

my_string: this is my very very very very very reeeeeeeally quite long string

is equivalent to:

my_string:> 
       this is my very very very very
       very reeeeeeeally quite long string

Is it possible to do the same thing with a list? Something like:

[my, very, long, list, that, is, waaaaaaaaaaaaaaay, tooooo, long]

written as:

my_list:>
     [my, very, long, 
     list, that, is, 
     waaaaaaaaaaaaaaay, tooooo, long]

but broken up over multiple lines, the docs were not specific on this. And when I tried to do it the way above when I loaded the YAML file I got the error:

yaml.scanner.ScannerError: mapping values are not allowed here
like image 553
Graham Booth Avatar asked Aug 05 '15 18:08

Graham Booth


2 Answers

What you have there is a "Flow sequence" and there is no requirement that it is put on a single line. You do however want to keep things indented relative to the (mapping) key it belongs to (although some parsers relax this requirement):

my_list: [my, very, long, 
         list, that, is, 
         waaaaaaaaaaaaaaay, tooooo, long]

Alternatively you can decide to use block style sequence, but that is less flexible as each sequence element has to be on its own line:

my_list:
- my
- very
- long
- list
- that
- is
- waaaaaaaaaaaaaaay
- tooooo
- long

In this case you can, but don't have to, indent the dash (-) before the list elements relative to the mapping key (my_list)

That you got the error mapping values are not allowed here is most likely because you did put a key-value map after the

my_list:>
     [my, very, long, 
     list, that, is, 
     waaaaaaaaaaaaaaay, tooooo, long]
xyz: 123

you get that error on the above with online parsers. There reason for that is that, because there is no space after the colon, my_list is never key for a mapping, but the beginning of a multiline scalar (string), with embedded newlines converted to spaced, that ends with long]. It is essentially equivalent to writing:

"my_list:> [my, very, long, list, that, is, waaaaaaaaaaaaaaay, tooooo, long]"

because the space-after-colon is missing.

like image 156
Anthon Avatar answered Nov 17 '22 03:11

Anthon


Just figured it out, YAML has another list syntax like this:

my_list:
    - this
    - is
    - my 
    - reaaaaally
    - long 
    - list

Which allows me to wrap a list

like image 24
Graham Booth Avatar answered Nov 17 '22 03:11

Graham Booth