-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a flag to output a single line in ConvertTo-YAML
.
#147
Comments
Could you give an example of what you mean by having the output as a single line? |
@gabriel-samfira, https://stackoverflow.com/revisions/67645180/2#:~:text=networkMapping:%20%7B'test_net_1':%20%7B'external':%20true%7D%2C%20'test_net_2':%20%7B'external':%20true%7D%2C%20'test_net_3':%20%7B'external':%20true%7D%7D provides an example:
...which, surprisingly, doesn't appear to be supported whatsoever by this library:
|
@RokeJulianLockhart That's because your PowerShell command is not well-formed. The correct command is this: $TestYaml = "networkMapping: {'test_net_1': {'external': true}, 'test_net_2': {'external': true}, 'test_net_3': {'external': true}}" | ConvertFrom-Yaml Now you can inspect the resulting object: $TestYaml
$TestYaml.networkMapping
|
This comment was marked as off-topic.
This comment was marked as off-topic.
That's okay @RokeJulianLockhart ! Happened to me more than once. Glad things got sorted out. Closing this bug. |
@gabriel-samfira, the issue hasn't been solved! That was an aside. This issue requests the ability to output a single line. Instructing |
ConvertTo-YAML
.
Have you tried the |
@gabriel-samfira, thanks. However, having it in one line merely because it's JSON-compatible, instead of prettified like usual, doesn't seem to make much sense. Irrespective, I want to use YAML because it's more readable, even on a single line. Outputting JSON-compatible YAML rather nullifies that. |
So, this example: $TestYaml = 'networkMapping: {"test_net_1": {"external": true}, "test_net_2": {"external": true}, "test_net_3": {"external": true}}' is equivalent to: $TestYaml = '{"networkMapping": {"test_net_1": {"external": true}, "test_net_2": {"external": true}, "test_net_3": {"external": true}}}' In your example, you essentially have a JSON assigned to There is no flow style in yaml that gives you a one liner from a data structure that has nested data structures like arrays, maps, etc. Not unless you use json which allows you to have one line. YAML depends on newlines and indents if you want actual yaml. The only places where it doesn't is where you use JSON (which is a subset of yaml). As an example, here is how python does it. First, with # With default_flow_style=False
>>> import yaml
>>> data = {"a": [1, 2, 3], "b": {"c": 7}}
>>> print(yaml.dump(data, default_flow_style=False))
a:
- 1
- 2
- 3
b:
c: 7 Now let's try with >>> import yaml
>>> data = {"a": [1, 2, 3], "b": {"c": 7}}
>>> print(yaml.dump(data,default_flow_style=True))
{a: [1, 2, 3], b: {c: 7}}
>>> We essentially got a JSON. The only thing missing from that YAML output are the quotes around the keys. But it looks just as unfriendly as any other JSON. |
Don't get me wrong, I am willing to add a flag to enable the |
See if #148 suits your needs. It's still a WiP because I also need to write tests, but it should do what you need. |
Many thanks, @gabriel-samfira. I'm no expert at YAML, so I'll acquiesce - your explanation at #147 (comment) is fairly comprehensive. |
No worries! I usually add features when they make sense. If nothing else, flow style is useful when used in sequences, as it makes the yaml more compact. If we add flow for sequences, it makes sense to add for mappings as well. So the I need to write some tests and release a new version on PowerShell gallery. |
Suggestion
Sometimes, I want the YAML to be a single line, because some input forms solely permit that.
Cuurent Status
The current documentation doesn't indicate a way to accomplish this:
The text was updated successfully, but these errors were encountered: