Loading structs with mixed field tags into koanf
alexbakker opened this issue · comments
Thanks for koanf! It's great.
I hope you don't mind me opening an issue to ask a question. I have a Config struct with a few fields:
type Config struct {
Dir string `koanf:"dir"`
Creds Creds `koanf:"creds"`
HTTP HTTP `koanf:"http"`
Log zap.Config `koanf:"log"`
}
All of the top-level fields in the struct have a koanf
tag, and most of the child structs also have fields with that tag. However, one of those structs is of a type that I don't control and thus doesn't have koanf
tags for its fields: zap.Config
. It does have tags for JSON and YAML though.
I tell koanf to load an instance of that struct with a couple of defaults as follows:
k.Load(structs.Provider(&defaultConfig, "koanf"), nil)
This works, but the problem is that this results in a mixed-case confMap
. All of the keys are lower case, except for the zap.Config
ones. I understand why this happens, but I'd like to find a way to make everything consistent without having to pull a copy of zap.Config
into my codebase and adding koanf
tags to all of its fields.
I suppose I could rename all of the koanf
tags in my own structs to yaml
to fix the issue, but this workaround falls apart if in the future there's another type I don't control that only has json
tags. What is the best way to approach this? Is there a way to tell koanf that it should look for a different tag for a certain path? I couldn't immediately find a way do that.
Sorry, missed this issue! You can specify and tag on struct field. Instead of koanf
, you can use json
.
k.UnmarshalWithConf("", &data, koanf.UnmarshalConf{Tag: "json"})