mirror of
https://github.com/spf13/viper
synced 2025-05-07 20:57:18 +00:00
Added setter for strategies used.
- Had set method for controlling the map merge strategies available to the viper instance
This commit is contained in:
parent
5a972e97cf
commit
e1232d2990
2 changed files with 25 additions and 0 deletions
5
viper.go
5
viper.go
|
@ -986,6 +986,11 @@ func (v *Viper) BindEnv(input ...string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetStrategy(strategies ...MergeStrategy) { v.SetStrategy(strategies...) }
|
||||||
|
func (v *Viper) SetStrategy(strategies ...MergeStrategy) {
|
||||||
|
v.mergeStrategies = strategies
|
||||||
|
}
|
||||||
|
|
||||||
// Given a key, find the value.
|
// Given a key, find the value.
|
||||||
// Viper will check in the following order:
|
// Viper will check in the following order:
|
||||||
// flag, env, config file, key/value store, default.
|
// flag, env, config file, key/value store, default.
|
||||||
|
|
|
@ -1332,6 +1332,26 @@ func TestMergeMapsSliceWithStategy(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMergeMultipleConfigSlicesWithStrategy(t *testing.T) {
|
||||||
|
config1 := []byte(`
|
||||||
|
sliceData:
|
||||||
|
- one
|
||||||
|
- two`)
|
||||||
|
config2 := []byte(`
|
||||||
|
sliceData:
|
||||||
|
- three`)
|
||||||
|
|
||||||
|
v := New()
|
||||||
|
v.SetStrategy(SliceAppendStrategy())
|
||||||
|
v.SetConfigType("yaml")
|
||||||
|
v.MergeConfig(bytes.NewReader(config1))
|
||||||
|
v.MergeConfig(bytes.NewReader(config2))
|
||||||
|
val := v.GetStringSlice("sliceData")
|
||||||
|
if !reflect.DeepEqual(val, []string{"one", "two", "three"}) {
|
||||||
|
t.Fatalf("unexpected key value wanted %s got %s", []string{"one", "two", "three"}, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestUnmarshalingWithAliases(t *testing.T) {
|
func TestUnmarshalingWithAliases(t *testing.T) {
|
||||||
v := New()
|
v := New()
|
||||||
v.SetDefault("ID", 1)
|
v.SetDefault("ID", 1)
|
||||||
|
|
Loading…
Add table
Reference in a new issue