mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
fix: apply envPrefix to all BindEnv vars
This commit is contained in:
parent
e71d7bf15c
commit
f0483c5d9e
2 changed files with 29 additions and 1 deletions
6
viper.go
6
viper.go
|
@ -1106,7 +1106,11 @@ func (v *Viper) BindEnv(input ...string) error {
|
||||||
if len(input) == 1 {
|
if len(input) == 1 {
|
||||||
v.env[key] = append(v.env[key], v.mergeWithEnvPrefix(key))
|
v.env[key] = append(v.env[key], v.mergeWithEnvPrefix(key))
|
||||||
} else {
|
} else {
|
||||||
v.env[key] = append(v.env[key], input[1:]...)
|
envKeys := make([]string, len(input[:1]))
|
||||||
|
for i, envKey := range input[1:] {
|
||||||
|
envKeys[i] = v.mergeWithEnvPrefix(envKey)
|
||||||
|
}
|
||||||
|
v.env[key] = append(v.env[key], envKeys...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -2590,6 +2590,30 @@ func TestFlagShadow(t *testing.T) {
|
||||||
assert.Equal(t, "", v.GetString("foo.bar1.bar2"))
|
assert.Equal(t, "", v.GetString("foo.bar1.bar2"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBindUsesEnvPrefix(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
|
||||||
|
os.Setenv("APP_FOO", "foo")
|
||||||
|
os.Setenv("APP_BAR", "bar")
|
||||||
|
|
||||||
|
v.SetEnvPrefix("APP")
|
||||||
|
v.AutomaticEnv()
|
||||||
|
v.BindEnv("foo1", "FOO")
|
||||||
|
v.BindEnv("bar1", "BAR")
|
||||||
|
|
||||||
|
assert.Equal(t, "foo", v.Get("foo1"))
|
||||||
|
assert.Equal(t, "bar", v.Get("bar1"))
|
||||||
|
|
||||||
|
type TestStruct struct {
|
||||||
|
Foo1 string `mapstructure:"foo1"`
|
||||||
|
Bar1 string `mapstructure:"bar1"`
|
||||||
|
}
|
||||||
|
var ts TestStruct
|
||||||
|
assert.NoError(t, v.Unmarshal(&ts))
|
||||||
|
assert.Equal(t, "foo", ts.Foo1)
|
||||||
|
assert.Equal(t, "bar", ts.Bar1)
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkGetBool(b *testing.B) {
|
func BenchmarkGetBool(b *testing.B) {
|
||||||
key := "BenchmarkGetBool"
|
key := "BenchmarkGetBool"
|
||||||
v = New()
|
v = New()
|
||||||
|
|
Loading…
Reference in a new issue