diff --git a/viper.go b/viper.go index 7eac4b7..e0eea24 100644 --- a/viper.go +++ b/viper.go @@ -944,20 +944,15 @@ func Sub(key string) *Viper { return v.Sub(key) } func (v *Viper) Sub(key string) *Viper { subv := New() + subv.parents = append(v.parents, strings.ToLower(key)) + subv.automaticEnvApplied = v.automaticEnvApplied + subv.envPrefix = v.envPrefix + subv.envKeyReplacer = v.envKeyReplacer data := v.Get(key) - if data == nil { - return nil - } - if reflect.TypeOf(data).Kind() == reflect.Map { - subv.parents = append(v.parents, strings.ToLower(key)) - subv.automaticEnvApplied = v.automaticEnvApplied - subv.envPrefix = v.envPrefix - subv.envKeyReplacer = v.envKeyReplacer subv.config = cast.ToStringMap(data) - return subv } - return nil + return subv } // GetString returns the value associated with the key as a string. diff --git a/viper_test.go b/viper_test.go index 8283b5c..14fda59 100644 --- a/viper_test.go +++ b/viper_test.go @@ -2619,6 +2619,15 @@ func TestSliceIndexAccess(t *testing.T) { assert.Equal(t, "Static", v.GetString("tv.0.episodes.1.2")) } +func TestSubUsesEnvPrefix(t *testing.T) { + testutil.Setenv(t, "TEST_SUB_VALUE", "test") + v.SetEnvPrefix("TEST") + v.AutomaticEnv() + + sub := v.Sub("sub") + assert.Equal(t, "test", sub.GetString("value")) +} + func BenchmarkGetBool(b *testing.B) { key := "BenchmarkGetBool" v = New()