knadh / koanf

Simple, extremely lightweight, extensible, configuration management library for Go. Support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.
MIT License
2.71k stars 150 forks source link

Add option to emit empty on marshal #268

Closed ReallyLiri closed 7 months ago

ReallyLiri commented 8 months ago

Added MarshalWithConf overload that allows passing an option to omit empty values from when marshalling. This is useful when "dumping" config to a file that will later be merged with other such configs, then empty values won't overwrite some valid value. In addition its useful for dumping very large configs created using a struct, with many optional values.

knadh commented 7 months ago

Thanks for the PR @ReallyLiri. Not sure if this should be merged in the core as it's a bit of a niche usecase. Config files generally have fixed structures, so keys disappearing when marshalling / dumping is unconventional. If a key has a nil or empty value, that's still valid.

ReallyLiri commented 7 months ago

In my use case, the CLI has a very vast number of options, while in practice, the user would only want to customize a very small amount of them. So if the marshalled config will contain all the possible keys, it will just create lots of unused portions. I do understand if its not a use case you'd like to support

knadh commented 7 months ago

Understood. I think this wouldn't be ideal to merge into the core. Thanks again for the PR.