From 4db0eb097614d95a9d927ce8aad2bcd7f82a5eeb Mon Sep 17 00:00:00 2001 From: Benoit Masson Date: Fri, 15 Jul 2016 22:41:29 +0200 Subject: [PATCH] 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. --- viper.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/viper.go b/viper.go index 3517fc7..1d3c7f6 100644 --- a/viper.go +++ b/viper.go @@ -424,7 +424,11 @@ func (v *Viper) searchMap(source map[string]interface{}, path []string) interfac // if the type of `next` is the same as the type being asserted return v.searchMap(next.(map[string]interface{}), path[1:]) default: - return next + if len(path) == 1 { + return next + } + // got a value but nested key expected, return "nil" for not found + return nil } } else { return nil