From 266e588e9eca7ee317e9f0446a01934675a4d261 Mon Sep 17 00:00:00 2001 From: Alexander Krasnukhin Date: Fri, 3 Mar 2017 12:09:20 +0100 Subject: [PATCH 1/4] Add string slice support to defaultDecoderConfig This way it correctly decodes string slices as well. --- viper.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/viper.go b/viper.go index 2a221e5..92366d6 100644 --- a/viper.go +++ b/viper.go @@ -747,13 +747,16 @@ func (v *Viper) Unmarshal(rawVal interface{}) error { } // defaultDecoderConfig returns default mapsstructure.DecoderConfig with suppot -// of time.Duration values +// of time.Duration values & string slices func defaultDecoderConfig(output interface{}) *mapstructure.DecoderConfig { return &mapstructure.DecoderConfig{ Metadata: nil, Result: output, WeaklyTypedInput: true, - DecodeHook: mapstructure.StringToTimeDurationHookFunc(), + DecodeHook: mapstructure.ComposeDecodeHookFunc( + mapstructure.StringToTimeDurationHookFunc(), + mapstructure.StringToSliceHookFunc(","), + ), } } From d9cca5ef33035202efb1586825bdbb15ff9ec3ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 29 Sep 2017 23:06:42 +0200 Subject: [PATCH 2/4] Go fmt viper.go --- viper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/viper.go b/viper.go index 92366d6..963861a 100644 --- a/viper.go +++ b/viper.go @@ -753,7 +753,7 @@ func defaultDecoderConfig(output interface{}) *mapstructure.DecoderConfig { Metadata: nil, Result: output, WeaklyTypedInput: true, - DecodeHook: mapstructure.ComposeDecodeHookFunc( + DecodeHook: mapstructure.ComposeDecodeHookFunc( mapstructure.StringToTimeDurationHookFunc(), mapstructure.StringToSliceHookFunc(","), ), From 8ef37cbca71638bf32f3d5e194117d4cb46da163 Mon Sep 17 00:00:00 2001 From: Kamil Wargula Date: Fri, 20 Oct 2017 12:22:28 +0200 Subject: [PATCH 3/4] Fix incorrect name of function in README.md Change `SetEnvReplacer` to `SetEnvKeyReplacer` --- README.md | 4 ++-- viper_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 848d92d..64bf474 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,7 @@ with ENV: * `AutomaticEnv()` * `BindEnv(string...) : error` * `SetEnvPrefix(string)` - * `SetEnvReplacer(string...) *strings.Replacer` + * `SetEnvKeyReplacer(string...) *strings.Replacer` _When working with ENV variables, it’s important to recognize that Viper treats ENV variables as case sensitive._ @@ -212,7 +212,7 @@ time a `viper.Get` request is made. It will apply the following rules. It will check for a environment variable with a name matching the key uppercased and prefixed with the `EnvPrefix` if set. -`SetEnvReplacer` allows you to use a `strings.Replacer` object to rewrite Env +`SetEnvKeyReplacer` allows you to use a `strings.Replacer` object to rewrite Env keys to an extent. This is useful if you want to use `-` or something in your `Get()` calls, but want your environmental variables to use `_` delimiters. An example of using it can be found in `viper_test.go`. diff --git a/viper_test.go b/viper_test.go index 774ca11..7050d5a 100644 --- a/viper_test.go +++ b/viper_test.go @@ -417,7 +417,7 @@ func TestAutoEnvWithPrefix(t *testing.T) { assert.Equal(t, "13", Get("bar")) } -func TestSetEnvReplacer(t *testing.T) { +func TestSetEnvKeyReplacer(t *testing.T) { Reset() AutomaticEnv() From 4dddf7c62e16bce5807744018f5b753bfe21bbd2 Mon Sep 17 00:00:00 2001 From: Jeff Lindsay Date: Thu, 9 Nov 2017 14:57:16 -0600 Subject: [PATCH 4/4] Allow exists util function to take afero.Fs so it can be used with non-deafult instances of Viper (#405) Signed-off-by: Jeff Lindsay --- util.go | 5 +++-- viper.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/util.go b/util.go index 3ebada9..c784dad 100644 --- a/util.go +++ b/util.go @@ -24,6 +24,7 @@ import ( "github.com/hashicorp/hcl" "github.com/magiconair/properties" toml "github.com/pelletier/go-toml" + "github.com/spf13/afero" "github.com/spf13/cast" jww "github.com/spf13/jwalterweatherman" "gopkg.in/yaml.v2" @@ -121,8 +122,8 @@ func absPathify(inPath string) string { } // Check if File / Directory Exists -func exists(path string) (bool, error) { - _, err := v.fs.Stat(path) +func exists(fs afero.Fs, path string) (bool, error) { + _, err := fs.Stat(path) if err == nil { return true, nil } diff --git a/viper.go b/viper.go index 963861a..64f006a 100644 --- a/viper.go +++ b/viper.go @@ -1537,7 +1537,7 @@ func (v *Viper) searchInPath(in string) (filename string) { jww.DEBUG.Println("Searching for config in ", in) for _, ext := range SupportedExts { jww.DEBUG.Println("Checking for", filepath.Join(in, v.configName+"."+ext)) - if b, _ := exists(filepath.Join(in, v.configName+"."+ext)); b { + if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b { jww.DEBUG.Println("Found: ", filepath.Join(in, v.configName+"."+ext)) return filepath.Join(in, v.configName+"."+ext) }