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

Proposal: Array merge sufffix

m1k1o opened this issue · comments

When meging multiple configs, and I am dealing with arrays, sometimes I want to replace all values, and sometimes just append to the existing array. Currently it only replaces.

I am proposing to add some kind of array merge suffix, e.g. [] so when key ends with this suffix, array values will be appended, otherwise they will be overwritten (default behaviour).

I created working PoC here:
https://github.com/m1k1o/koanf/blob/2446e80c255997a038f7bee2ec7a699824809efe/maps/maps.go#L112-L124

Please let me know if this would be something users of koanf are interested in or its just my personal usecase. It would be really interesting to see support for this in upstream.

so when key ends with this suffix, array values will be appended

It wouldn't be ideal to have opinionated DSL-like behaviour in koanf's core. For niche scenarios like this, the right way to do it is to Get() the confmap out, apply your logic, and create a fresh koanf instance out of it, or merge the particular key back.