Automatically format YAML to use dot separator where possible

582 views Asked by At

I am using IntelliJ and want to find a quick way to format a lot of yaml files from this:

something:
  some:
    thing:
      enable: true
a:
  b:
    c: true
    d:
      e: false

to this:

something.some.thing.enable: true
a.b:
  c: true
  d.e: false

whereever possible. It is still valid syntax, but in the files in question, it is way better to read and often times, elements really just contain one value.

Is there an offline tool, plugin or maven/gradle build step I can use to achieve this?

1

There are 1 answers

2
flyx On

It is still valid syntax

Yes, but it doesn't do what you think it does. d.e in YAML will simply load as string "d.e". There is no YAML logic that splits at the dot like you seem to assume.

Java Spring does some postprocessing that causes this to be loaded properly, see also this question. However, this does not work generally with YAML.

See this answer for a yq command that does something like this – however it will print a full path for each value and you probably don't want that. Doing what you want is probably too complex for yq, but not impossible to achieve.

Also mind that queries about finding external tools are off-topic on StackOverflow.