File tree 2 files changed +29
-1
lines changed
2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -1106,7 +1106,11 @@ func (v *Viper) BindEnv(input ...string) error {
1106
1106
if len (input ) == 1 {
1107
1107
v .env [key ] = append (v .env [key ], v .mergeWithEnvPrefix (key ))
1108
1108
} else {
1109
- v .env [key ] = append (v .env [key ], input [1 :]... )
1109
+ envKeys := make ([]string , len (input [:1 ]))
1110
+ for i , envKey := range input [1 :] {
1111
+ envKeys [i ] = v .mergeWithEnvPrefix (envKey )
1112
+ }
1113
+ v .env [key ] = append (v .env [key ], envKeys ... )
1110
1114
}
1111
1115
1112
1116
return nil
Original file line number Diff line number Diff line change @@ -2590,6 +2590,30 @@ func TestFlagShadow(t *testing.T) {
2590
2590
assert .Equal (t , "" , v .GetString ("foo.bar1.bar2" ))
2591
2591
}
2592
2592
2593
+ func TestBindUsesEnvPrefix (t * testing.T ) {
2594
+ v := New ()
2595
+
2596
+ os .Setenv ("APP_FOO" , "foo" )
2597
+ os .Setenv ("APP_BAR" , "bar" )
2598
+
2599
+ v .SetEnvPrefix ("APP" )
2600
+ v .AutomaticEnv ()
2601
+ v .BindEnv ("foo1" , "FOO" )
2602
+ v .BindEnv ("bar1" , "BAR" )
2603
+
2604
+ assert .Equal (t , "foo" , v .Get ("foo1" ))
2605
+ assert .Equal (t , "bar" , v .Get ("bar1" ))
2606
+
2607
+ type TestStruct struct {
2608
+ Foo1 string `mapstructure:"foo1"`
2609
+ Bar1 string `mapstructure:"bar1"`
2610
+ }
2611
+ var ts TestStruct
2612
+ assert .NoError (t , v .Unmarshal (& ts ))
2613
+ assert .Equal (t , "foo" , ts .Foo1 )
2614
+ assert .Equal (t , "bar" , ts .Bar1 )
2615
+ }
2616
+
2593
2617
func BenchmarkGetBool (b * testing.B ) {
2594
2618
key := "BenchmarkGetBool"
2595
2619
v = New ()
You can’t perform that action at this time.
0 commit comments