crdoconnor / strictyaml

Type-safe YAML parser and validator.
https://hitchdev.com/strictyaml/
MIT License
1.47k stars 60 forks source link

Add support for writing out optional defaults #144

Open marxide opened 3 years ago

marxide commented 3 years ago

Thanks for the library!

I'm considering using this to handle user-provided configurations for a scientific data processing pipeline. Most of the config parameters have sane defaults, but these defaults themselves are configurable by the system admin who deployed the pipeline software.

I'd really like to be able to have default config parameters that users can leave out of their config files, but I'd like to write out a copy of the full config spec including those parameters that weren't explicitly supplied by the user. This would give us a copy of the config used for an execution of the pipeline that records every config parameter and its value whether the user supplied it or not. This is important for us as we want to keep records of the configs used for reproducibility and we anticipate that different deployments of the software will have different defaults. Ideally, I'd also insert a comment next to the parameters where the default value was used, but that's less important.

I was thinking perhaps this could be implemented by adding a kwarg to Map.to_yaml, e.g. Map.to_yaml(drop_defaults=True) where setting it to False would include the optional keys with their default values in the output.

For example:

from strictyaml import load, Map, Optional, Int

yaml_snippet = """
a: 1
"""

schema = Map({
    "a": Int(),
    Optional("b", default=5): Int(),
})

yaml = load(yaml_snippet, schema)

print(yaml.as_yaml())
print(yaml.as_yaml(drop_defaults=False))

would output

a: 1

a: 1
b: 5  # default

Do you think such a feature is warranted? I'm happy to contribute.

crdoconnor commented 3 years ago

My instinctive reaction is that I like this idea, yes.

crdoconnor commented 3 years ago

The only concerns are

If you'd like to contribute the change, please feel free. I've been pretty busy lately!