From 3535c75fa87d780db2436955c5d105de3e68f125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 5 Dec 2018 15:19:25 +0100 Subject: [PATCH] Add MergeConfigMap Fixes #605 --- go.mod | 2 ++ go.sum | 5 +++++ viper.go | 12 +++++++++--- viper_test.go | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 3f4e1c2..ce610b3 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,12 @@ require ( github.com/magiconair/properties v1.8.0 github.com/mitchellh/mapstructure v1.0.0 github.com/pelletier/go-toml v1.2.0 + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/afero v1.1.2 github.com/spf13/cast v1.2.0 github.com/spf13/jwalterweatherman v1.0.0 github.com/spf13/pflag v1.0.2 + github.com/stretchr/testify v1.2.2 golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 // indirect golang.org/x/text v0.3.0 // indirect gopkg.in/yaml.v2 v2.2.1 diff --git a/go.sum b/go.sum index 3e3b874..24cf77e 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -9,6 +10,8 @@ github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KH github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.2.0 h1:HHl1DSRbEQN2i8tJmtS6ViPyHx35+p51amrdsiTCrkg= @@ -17,6 +20,8 @@ github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9 github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc= github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg= golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= diff --git a/viper.go b/viper.go index 1875627..ca1a2de 100644 --- a/viper.go +++ b/viper.go @@ -1259,13 +1259,19 @@ func (v *Viper) ReadConfig(in io.Reader) error { // MergeConfig merges a new configuration with an existing config. func MergeConfig(in io.Reader) error { return v.MergeConfig(in) } func (v *Viper) MergeConfig(in io.Reader) error { - if v.config == nil { - v.config = make(map[string]interface{}) - } cfg := make(map[string]interface{}) if err := v.unmarshalReader(in, cfg); err != nil { return err } + return v.MergeConfigMap(cfg) +} + +// MergeConfigMap merges the configuration from the map given with an existing config. +func MergeConfigMap(cfg map[string]interface{}) error { return v.MergeConfigMap(cfg) } +func (v *Viper) MergeConfigMap(cfg map[string]interface{}) error { + if v.config == nil { + v.config = make(map[string]interface{}) + } mergeMaps(cfg, v.config, nil) return nil } diff --git a/viper_test.go b/viper_test.go index 6097867..e4b4b83 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1230,6 +1230,41 @@ func TestMergeConfigNoMerge(t *testing.T) { } } +func TestMergeConfigMap(t *testing.T) { + v := New() + v.SetConfigType("yml") + if err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleTgt)); err != nil { + t.Fatal(err) + } + + assert := func(i int) { + large := v.GetInt("hello.lagrenum") + pop := v.GetInt("hello.pop") + if large != 765432101234567 { + t.Fatal("Got large num:", large) + } + + if pop != i { + t.Fatal("Got pop:", pop) + } + } + + assert(37890) + + update := map[string]interface{}{ + "hello": map[string]interface{}{ + "pop": 1234, + }, + } + + if err := v.MergeConfigMap(update); err != nil { + t.Fatal(err) + } + + assert(1234) + +} + func TestUnmarshalingWithAliases(t *testing.T) { v := New() v.SetDefault("ID", 1)