mirror of https://github.com/spf13/viper.git
Add string replacer interface and env key replacer option
This commit is contained in:
parent
6fcf985c5a
commit
4ad4c8df70
|
@ -263,6 +263,9 @@ 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
|
`Get()` calls, but want your environmental variables to use `_` delimiters. An
|
||||||
example of using it can be found in `viper_test.go`.
|
example of using it can be found in `viper_test.go`.
|
||||||
|
|
||||||
|
Alternatively, you can use `EnvKeyReplacer` with `NewWithOptions` factory function.
|
||||||
|
Unlike `SetEnvKeyReplacer`, it accepts a `StringReplacer` interface allowing you to write custom string replacing logic.
|
||||||
|
|
||||||
By default empty environment variables are considered unset and will fall back to
|
By default empty environment variables are considered unset and will fall back to
|
||||||
the next configuration source. To treat empty environment variables as set, use
|
the next configuration source. To treat empty environment variables as set, use
|
||||||
the `AllowEmptyEnv` method.
|
the `AllowEmptyEnv` method.
|
||||||
|
|
15
viper.go
15
viper.go
|
@ -197,7 +197,7 @@ type Viper struct {
|
||||||
envPrefix string
|
envPrefix string
|
||||||
|
|
||||||
automaticEnvApplied bool
|
automaticEnvApplied bool
|
||||||
envKeyReplacer *strings.Replacer
|
envKeyReplacer StringReplacer
|
||||||
allowEmptyEnv bool
|
allowEmptyEnv bool
|
||||||
|
|
||||||
config map[string]interface{}
|
config map[string]interface{}
|
||||||
|
@ -257,6 +257,19 @@ func KeyDelimiter(d string) Option {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringReplacer applies a set of replacements to a string.
|
||||||
|
type StringReplacer interface {
|
||||||
|
// Replace returns a copy of s with all replacements performed.
|
||||||
|
Replace(s string) string
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnvKeyReplacer sets a replacer used for mapping environment variables to internal keys.
|
||||||
|
func EnvKeyReplacer(r StringReplacer) Option {
|
||||||
|
return optionFunc(func(v *Viper) {
|
||||||
|
v.envKeyReplacer = r
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// NewWithOptions creates a new Viper instance.
|
// NewWithOptions creates a new Viper instance.
|
||||||
func NewWithOptions(opts ...Option) *Viper {
|
func NewWithOptions(opts ...Option) *Viper {
|
||||||
v := New()
|
v := New()
|
||||||
|
|
|
@ -549,6 +549,15 @@ func TestSetEnvKeyReplacer(t *testing.T) {
|
||||||
assert.Equal(t, "30s", Get("refresh-interval"))
|
assert.Equal(t, "30s", Get("refresh-interval"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEnvKeyReplacer(t *testing.T) {
|
||||||
|
v := NewWithOptions(EnvKeyReplacer(strings.NewReplacer("-", "_")))
|
||||||
|
|
||||||
|
v.AutomaticEnv()
|
||||||
|
_ = os.Setenv("REFRESH_INTERVAL", "30s")
|
||||||
|
|
||||||
|
assert.Equal(t, "30s", v.Get("refresh-interval"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestAllKeys(t *testing.T) {
|
func TestAllKeys(t *testing.T) {
|
||||||
initConfigs()
|
initConfigs()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue