de-jcup / eclipse-yaml-editor

Eclipse YAML editor
https://marketplace.eclipse.org/content/yaml-editor
Apache License 2.0
35 stars 10 forks source link

Volumes after format applied got a null #91

Closed alanwilter closed 4 years ago

alanwilter commented 4 years ago

It's not actually an issue but I can't find anywhere why when use shift-cmd-F, to format my yml file, it adds a null to my Volumes:

volumes:
   db:

becomes

volumes:
   db: null

I don't see anything about it written in the docker-compose documentation.

de-jcup commented 4 years ago

Clear up

You will not find anything about this in docker-compose documentation, because Yaml Editor is for any YAML formatted file so it's just about a YAML problem/situation.

Technical thoughts

Yaml Editor uses internally SnakeYaml and its contained pretty printer - also some additions are made to preserve comments (which are always dropped by SnakeYaml).

How to skip null values by SnakeYaml parser/pretty printer

At https://bitbucket.org/asomov/snakeyaml/wiki/Howto#markdown-header-to-be-done there is an entry about "Provide example for skipping null and empty collections" - so this should be possible. An example can be found at: https://stackoverflow.com/questions/39745130/filter-out-null-fields-in-snakeyaml

Empty fields in Yaml

I think https://stackoverflow.com/questions/34089496/empty-field-in-yaml is a good explanation why snake did format this with null.

Changes necessary/possible ?

@alanwilter : In your example above, the db key did not set a value - so value of key "db" is null. What exactly are you supposing to have after a format when you write this? Skipping null values by snake yaml parser would IMHO result in

volumes:
  db:
other:
  xyz: 1234
other:
  xyz: 1234

so destroys /removes unnecessary parts

Summary

I think it is okay to have null there - it keeps the given structure (so do not remove empty ones).

@alanwilter : Is the explanation good enough for you or do you really need a change here? I think the current behaviour is okay.

alanwilter commented 4 years ago

Thank @de-jcup !