From e1232d2990f3cc2bd00ae82bc16c644751e2dcc4 Mon Sep 17 00:00:00 2001 From: Thomas Miller Date: Tue, 22 Jan 2019 14:00:35 +1300 Subject: [PATCH] Added setter for strategies used. - Had set method for controlling the map merge strategies available to the viper instance --- viper.go | 5 +++++ viper_test.go | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) 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)