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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use ErrorUnused mapstructure decoder feature

Alertal opened this issue · comments

mapstructure decoder offers an option ErrorUnused that returns an error if a key exists in its map but does not correspond to any entry in the targeted object when unmarshaling.

I looked into how we could activate this feature for koanf. With the current implementation, it seems we'd have to override the decoder configuration with the following

&mapstructure.DecoderConfig{
			DecodeHook: mapstructure.ComposeDecodeHookFunc(
				mapstructure.StringToTimeDurationHookFunc(),
				mapstructure.StringToSliceHookFunc(","),
				mapstructure.TextUnmarshallerHookFunc()),
			Metadata:         nil,
			Result:           o,
			ErrorUnused:      true,
			WeaklyTypedInput: true,
		}

What bothers me with this solution is that I have to take the copy paste the default configuration chosen by koanf into my code and tweak it locally. If koanf default configuration changes, I will not benefit from it.

Is this the solution you'd recommend ?

I'd be in favour of having either a default configuration as a constant that users can rely on if they so choose, or having a koanf option so that users don't even have to access a mapstructure.DecoderConfig to enable feature. What's your take on these proposals ?

This is the right approach. koanf here exposes the underlying mapstructure internals so that you can interact with it and modify it directly. Configuring the raw interface looks a bit verbose like in the above example, but it's explicit and gives you full control. Any sort of koanf abstraction that we try to build on top of this will be complicated and will still require this exit hatch.

The koanf default config is just there as a helper. Even if it changes, it's irrelevant when you're configuring mapstructure explicitly to suit your needs.