Skip to content
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

Enable configurable indentation #52

Closed
AnthonyWC opened this issue Mar 22, 2019 · 14 comments
Closed

Enable configurable indentation #52

AnthonyWC opened this issue Mar 22, 2019 · 14 comments

Comments

@AnthonyWC
Copy link

e.g.

foo.json

{
  "test": {
    "s1": "bar",
    "s2": [
      "snowflake",
      "s3",
      "gcloud"
    ]
  }
}

yq -y . foo.json

test:
  s1: bar
  s2:
  - snowflake
  - s3
  - gcloud

When valid yaml should be:

 test:
   s1: bar
   s2:
     - snowflake
     - s3
     - gcloud
@AnthonyWC AnthonyWC changed the title Non-valid YAML format is for list Non-valid YAML format output for list Mar 22, 2019
@kislyuk
Copy link
Owner

kislyuk commented Mar 23, 2019

That format is valid YAML.

@kislyuk kislyuk closed this as completed Mar 23, 2019
@AnthonyWC
Copy link
Author

Interesting; didn't realize the spec allowed for 0 spacing. However it seems it would fail some yaml linter; is it possible to specify a default format spacing?

@kislyuk
Copy link
Owner

kislyuk commented Mar 23, 2019

yq currently uses PyYAML for YAML encoding. If there is an option that can be passed to PyYAML to specify the spacing, then yes, we can make that happen. Can you take a look at the PyYAML docs and give an example of the option if it exists?

@AnthonyWC
Copy link
Author

From here there is some suggestions https://stackoverflow.com/questions/25108581/python-yaml-dump-bad-indentation but doesn't look like a native PyYAML option exists. Also there is a bug with indentation with lists in PyYAML (yaml/pyyaml#234)

@kislyuk
Copy link
Owner

kislyuk commented Mar 25, 2019

OK thanks. It looks like ruamel.yaml has that option. I'm going to take another look at switching away from PyYAML (it has a number of flaws).

@kislyuk kislyuk changed the title Non-valid YAML format output for list Enable configurable indentation Mar 25, 2019
@kislyuk kislyuk reopened this Mar 25, 2019
@gecube
Copy link

gecube commented Jun 19, 2019

Can we switch to https://github.com/crdoconnor/strictyaml ? Will it fix the issue or not?
I didn't make the comparison to PyYAML yet, but it looks very promising.

@kislyuk
Copy link
Owner

kislyuk commented Jun 20, 2019

No. It introduces too many incompatibilities with YAML features that are in common use.

@kislyuk
Copy link
Owner

kislyuk commented Jun 24, 2019

At this point I find there is no suitable replacement for PyYAML. Therefore this bug is blocked by yaml/pyyaml#234.

@jayvdb
Copy link

jayvdb commented Jul 12, 2019

Previous issue regarding indentation: #29

Previous issue regarding ruamel.yaml #15 , which I have left at comment at as I think ruamel.yaml is stable now (it wasnt when the issue was closed) and is highly desirable.

@daks
Copy link

daks commented Aug 21, 2019

Hello, I'm facing the same problem, my YAML lists are converted from

foo:
  - bar
  - baz

to

foo:
- bar
- baz

which introduces unecessary and hard-to-review changes. I would also like the possibility of defining custom indentation.

PS: Thanks for this useful tool

@jayvdb
Copy link

jayvdb commented Aug 22, 2019

I did some further analysis of available tools at adrienverge/yamllint#62 (comment) . In short, ruamel.yaml.cmd is a very useful tool to post-process yaml out of yq , to restore expected indentation.

It would be great if ruamel.yaml was used internally by yq for even better results.
ruamel.yaml is now 1.0+ , so the API is now stable.

@kislyuk
Copy link
Owner

kislyuk commented Aug 25, 2019

At this point I consider staying with PyYAML to be the safest course of action. PyYAML has at least 3 orders of magnitude more usage and exposure than ruamel.yaml, and recently regained tractable maintainership. This bug remains blocked by yaml/pyyaml#234.

@kislyuk
Copy link
Owner

kislyuk commented Oct 25, 2019

I have found a way to address this issue. By default, yq -y will now indent block lists by 2 spaces ( - item instead of - item). The old behavior (0 spaces, the PyYAML default) can be triggered via yq -y --indentless-lists.

Released in v2.8.0, please test.

@ghost
Copy link

ghost commented Oct 26, 2019

@kislyuk it’s working for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants