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

Add non-template TOML formatting #38

Merged
merged 9 commits into from
Jul 31, 2023
Merged

Add non-template TOML formatting #38

merged 9 commits into from
Jul 31, 2023

Conversation

mthuurne
Copy link
Member

A configuration template is useful as a starting point for a manually crafted configuration. However, in some cases we want to automatically produce a full configuration that doesn't require manual editing. In those cases, we want just the given data to end up in the output and no examples for optional elements that could have been configured but weren't.

This PR adds a format_toml() method, which operates similar to format_toml_template() but leaves out optional elements unless there is data present for them.

The output of format_toml() when called on a Binder that wraps a dataclass instance is guaranteed to be valid TOML. That guarantee doesn't hold when a dataclass itself is wrapped, because there is no data to put into mandatory fields then.

mthuurne added 9 commits July 27, 2023 06:56
Some formatted strings used `NAME_fmt` and some `fmt_NAME`.
Switch to the former style exclusively.
This definition was moved forward since it is also needed to name child
tables. However, it was still defined at its original location as well.
In `_iter_format_key()` we called `_iter_format_value()` to format
non-bare keys,  but it is confusing and wasteful to call a function
that can handle a bunch of types when we already know the exact type
we want to format.
This prepares the code to be able to not output comments if there is
no value line.
Currently the comment block before a key-value pair is never empty,
but that will change.
This formats data in a non-template fashion:
- only includes the docstrings as comments
- optional fields without a value are skipped
This is consistent with how key-value pairs are formatted.
Mandatory tables should always be present, even in non-template output.
@mthuurne mthuurne force-pushed the format-nontemplate branch from 500e09c to 3688912 Compare July 31, 2023 13:34
@mthuurne mthuurne merged commit 061dd1e into main Jul 31, 2023
@mthuurne mthuurne deleted the format-nontemplate branch July 31, 2023 13:41
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

Successfully merging this pull request may close these issues.

1 participant