pelletier / go-toml

Go library for the TOML file format
https://github.com/pelletier/go-toml
Other
1.72k stars 209 forks source link

Way to keep commented out lines after unmarshalling #903

Closed fmuyassarov closed 6 months ago

fmuyassarov commented 1 year ago

Hi. Does go-toml v2 support keeping the commented out lines of a TOML file when unmarshaling and marshaling? I'm manipulating a TOML based configuration file and eventually, commented out lines are getting clean up. It would be nice to have an option to keep them as it is.

Example

# The CRI-O configuration file specifies all of the available configuration
# options and command-line flags for the crio(8) OCI Kubernetes Container Runtime
# daemon, but in a TOML format that can be more easily modified and versioned.
#
# Please refer to crio.conf(5) for details of all configuration options.
[crio]
pelletier commented 1 year ago

The answer to this question depends largely on what types are involved and the kind of operations you want to make.

go-toml has support for commented struct fields (example).

You can also use the unstable.AST to parse the document, keeping comments. However there is no easy way to write it back out at the moment. You should be able to generate your own struct types dynamically to achieve this. Check out https://github.com/pelletier/go-toml/issues/822 about this topic – pull request to add encoder methods to the unstable.AST would be welcomed!

pelletier commented 6 months ago

Closing this, feel free to open a discussion if there is more to discuss about this subject!