Added setter for strategies used.

- Had set method for controlling the map merge strategies available to
the viper instance
This commit is contained in:
Thomas Miller 2019-01-22 14:00:35 +13:00
parent 5a972e97cf
commit e1232d2990
2 changed files with 25 additions and 0 deletions

View file

@ -986,6 +986,11 @@ func (v *Viper) BindEnv(input ...string) error {
return nil
}
func SetStrategy(strategies ...MergeStrategy) { v.SetStrategy(strategies...) }
func (v *Viper) SetStrategy(strategies ...MergeStrategy) {
v.mergeStrategies = strategies
}
// Given a key, find the value.
// Viper will check in the following order:
// flag, env, config file, key/value store, default.

View file

@ -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) {
v := New()
v.SetDefault("ID", 1)