Fix: Get() returns nil when nested element not found

If foo.bar is defined but foo.bar.baz isn't, then v.Get("foo.bar.baz")
returns nil.

v.searchMap() modified for this purpose.
This commit is contained in:
Benoit Masson 2016-07-15 22:41:29 +02:00
parent fd1ed93339
commit 4db0eb0976

View file

@ -424,8 +424,12 @@ func (v *Viper) searchMap(source map[string]interface{}, path []string) interfac
// if the type of `next` is the same as the type being asserted // if the type of `next` is the same as the type being asserted
return v.searchMap(next.(map[string]interface{}), path[1:]) return v.searchMap(next.(map[string]interface{}), path[1:])
default: default:
if len(path) == 1 {
return next return next
} }
// got a value but nested key expected, return "nil" for not found
return nil
}
} else { } else {
return nil return nil
} }