ProtixIT / dataclass-binder

Python library to bind TOML data to dataclasses in a type-safe way
MIT License
13 stars 2 forks source link

Add non-template TOML formatting #38

Closed mthuurne closed 1 year ago

mthuurne commented 1 year ago

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.