From 59dd7a172e5f491ce01f48fc46b884ee1cbd2d8d Mon Sep 17 00:00:00 2001 From: Teddy Budiono Hermawan Date: Tue, 8 Aug 2023 18:40:02 +0800 Subject: [PATCH] ignore bindEnvs when type is map --- config/viper/viper.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/config/viper/viper.go b/config/viper/viper.go index 8ff48f3..50c439b 100644 --- a/config/viper/viper.go +++ b/config/viper/viper.go @@ -35,6 +35,9 @@ func Load(confPath string, receiver interface{}) { } } +// viper supports unmarshaling from env vars if the keys are known +// +// bindEnvs is a hack to let viper know in advance what keys exists func bindEnvs(v reflect.Value, parts ...string) { if v.Kind() == reflect.Ptr { if v.IsNil() { @@ -56,6 +59,10 @@ func bindEnvs(v reflect.Value, parts ...string) { switch val.Kind() { case reflect.Struct: bindEnvs(val, append(parts, tv)...) + case reflect.Map: + // bindEnvs hack doesn't work for maps, because we don't know all the possible + // values for map keys. Therefore we do nothing to fallback to viper's default key detection. + continue default: if err := viper.BindEnv(strings.Join(append(parts, tv), ".")); err != nil { log.Fatal(err)