diff --git a/viper.go b/viper.go index f38be4c..6fa7c74 100644 --- a/viper.go +++ b/viper.go @@ -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. diff --git a/viper_test.go b/viper_test.go index 39e114c..d940967 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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)