From 056876e01f8e4d2ca5e5ee657346f37650332b1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20G=C3=BCnzler?= Date: Tue, 17 Oct 2017 14:54:28 +0200 Subject: [PATCH] Add SetKeyDelimiter Allow changing the Key Delimiter for accessing nested values. Fixes #170 --- viper.go | 7 +++++++ viper_test.go | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/viper.go b/viper.go index 963861a..62ea1e6 100644 --- a/viper.go +++ b/viper.go @@ -1560,6 +1560,13 @@ func (v *Viper) findConfigFile() (string, error) { return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)} } +func SetKeyDelimiter(in string) { v.SetKeyDelimiter(in) } +func (v *Viper) SetKeyDelimiter(in string) { + if in != "" { + v.keyDelim = in + } +} + // Debug prints all configuration registries for debugging // purposes. func Debug() { v.Debug() } diff --git a/viper_test.go b/viper_test.go index 774ca11..3a3a7b4 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1181,6 +1181,11 @@ func doTestCaseInsensitive(t *testing.T, typ, config string) { assert.Equal(t, 4, cast.ToInt(Get("ef.lm.no"))) assert.Equal(t, 5, cast.ToInt(Get("ef.lm.p.q"))) + SetKeyDelimiter("::") + assert.Equal(t, 2, cast.ToInt(Get("ef::gh"))) + assert.Equal(t, 3, cast.ToInt(Get("ef::ijk"))) + assert.Equal(t, 4, cast.ToInt(Get("ef::lm::no"))) + assert.Equal(t, 5, cast.ToInt(Get("ef::lm::p::q"))) } func BenchmarkGetBool(b *testing.B) {