Skip to content

Commit

Permalink
ignore bindEnvs when type is map
Browse files Browse the repository at this point in the history
  • Loading branch information
Teddy Budiono Hermawan committed Aug 8, 2023
1 parent d8decaf commit 59dd7a1
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/viper/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand Down

0 comments on commit 59dd7a1

Please sign in to comment.