Reputation: 5889
I use the YAML component of Symfony to parse the YAML in this question.
I have the following YAML:
db_driver: orm
service:
user_provider: user_provider
options:
supported_scopes: >
user_basic_information
internal
which ends up in this string:
"user_basic_information internal
"
but I like to get this string:
"user_basic_information internal"
I found the follwoing workaround:
service:
user_provider: user_provider
options:
supported_scopes: >
user_basic_information
internal
db_driver: orm
this block ends up in the correct string without a line break. Is my YAML parser buggy or is this a lack of the YAML language definition?
Is there another way of terminating a folded style block so I don't have to do it the hacky way?
Upvotes: 3
Views: 2361
Reputation: 76599
The answer to both questions is yes. Adding a additional key value pair to the top-level, or any other of the mappings should not affect the final newline of the folded scalar.
Using >
, folded block style, you always get one newline at the end of the file, because the default is clipping:
Clipping is the default behavior used if no explicit chomping indicator is specified. In this case, the final line break character is preserved in the scalar’s content. However, any trailing empty lines are excluded from the scalar’s content.
If you don't want that, use an explicit chomping operator, in this case, to strip, use >-
instead of just >
. (Assuming of course that your parser interprets that correctly).
In Python, using ruamel.yaml (of which I am the author) this works correctly:
import ruamel.yaml
yaml_str = """\
db_driver: orm
service:
user_provider: user_provider
options:
supported_scopes: >-
user_basic_information
internal
"""
data = ruamel.yaml.safe_load(yaml_str)
print(repr(data['service']['options']['supported_scopes']))
You can also check that online here and here (this parser however has some other problems)
Upvotes: 5