This commit is contained in:
Knut Zuidema 2023-10-24 16:24:58 +08:00 committed by GitHub
commit 3397996d63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 10 deletions

View file

@ -951,20 +951,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.

View file

@ -2468,6 +2468,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()