I try to improve the user experience of a cli I maintain. A main goal is to provide reasonable defaults. It uses yaml
extensively for configuration.
A basic demo implementation of the configuration can be found here: https://github.com/unprofession-al/configuration/tree/bf5a89b3eee7338899b28c047f3795546ce3d2e6
General
The main configuration looks like this:
type Config map[string]ConfigSection
type ConfigSection struct {
Input InputConfig `yaml:"input"`
Output OutputConfig `yaml:"output"`
}
Config
holds a bunch of ConfigSections
. This allow the user to define variations of the configuration (lets say prod
, dev
and testing
for example) and use YAML Achors to do so.
The parts of the ConfigSection
(Input
and Output
) would be defined in the package that consumes the configuration. Each of this part provides a Defaults()
and a custom UnmarshalYAML()
func. Also ConfigSection
itself provides an UnmarshalYAML()
func. This idea is stolen from https://github.com/go-yaml/yaml/issues/165#issuecomment-255223956.
Question
In data.go
in the repo some test input as well as the expected output is defined. Running the tests (go test -v
) shows:
- Having nothing defined in the ConfigSection (
empty
example) no defaults are applied. - If a part (of a
ConfigSection
) with no data field is defined, this part will have no defaults. The "undefined" part has defaults (seeinput
,output
). - If both parts are defined (as is section
both
) but have no data fields, any defaults are set.
I see no pattern at all and ran out of ideas why this works like this and how to get the expected results (eg. get the test to pass).