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

comment-preserving parser/generator? #284

Closed
tve opened this issue Jan 8, 2015 · 10 comments
Closed

comment-preserving parser/generator? #284

tve opened this issue Jan 8, 2015 · 10 comments

Comments

@tve
Copy link

tve commented Jan 8, 2015

Has anyone written a comment-preserving parser/generator? The reason I'm asking is that I like config files, but I also like to be able to machine-edit them, for example through a web interface. Using the plain parser/generator one looses all comments in the config file.

@mojombo
Copy link
Member

mojombo commented Jan 15, 2015

I'm not aware of any, but that would be a great addition to a TOML parser.

@mojombo
Copy link
Member

mojombo commented Jan 16, 2015

I don't think you're going to get an answer here. Have you tried asking on Twitter or browsing through the list of parsers? Gonna close for now.

@mojombo mojombo closed this as completed Jan 16, 2015
@kawing-chiu
Copy link

I think this feature should really go into the toml spec...

@TheElectronWill
Copy link
Contributor

@tve @kawing-chiu If it helps, I've written one in java that preserve most comments (not 100%, e.g. comments in arrays are not preserved for now). It's part of the Night-Config library.

@tshepang
Copy link

tshepang commented Sep 6, 2017

tomllib (written in Rust) preserves comments I believe

@shadow-light
Copy link

Python tomlkit preserves comments. In fact that is why I was going to change all of my projects to TOML! Since learning it isn't actually in the spec I'm not so sure now. Putting this in the spec would give a huge advantage in my opinion over other config languages.

@ChristianSi
Copy link
Contributor

ChristianSi commented Aug 20, 2018

@shadow-light Why should something like that be in the spec? Parsers/writers can implement things like preserving comments and order of elements, but most TOML apps will only read or write TOML files, not both, hence preserving syntactic sugar is irrelevant for them. If Python tomlkit preserves such details, that might be nice for some use cases (say a built-in editor for your TOML config), so by all means use it if it fits your needs.

Just remember that TOML tables are unordered so you cannot require your users to write keys in a specific order; and, since comments are ignored, you cannot rely on the absence or presence of specific comments. I think that's a good thing.

@shadow-light
Copy link

Because comments are highly valuable sources of information to remember why certain values have been set. When you've written a lot of comments in a config file, it is very annoying to have them all wiped out because one of the values needed to be updated by a CLI for example.

It is sufficient for my use cases at least that the Python implementation does this. But putting it in the spec would guarantee it stays that way, and the feature doesn't get removed, or one can switch to another language implementation and not worry.

@bradleypeabody
Copy link

bradleypeabody commented Jul 15, 2021

most TOML apps will only read or write TOML files, not both

While I understand why this comment was made, I don't think it's a safe assumption at all. It is also a sort of natural progression to start with a simple hand-edited configuration file and then want to add a UI or other automation to it later. I've literally encountered this problem on nearly every significantly-sized project I've worked on.

@federico-terzi-anima
Copy link

For people looking for a solution to this in 2024, in Rust we have the toml_edit crate: https://crates.io/crates/toml_edit

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

9 participants