mirror of
https://github.com/spf13/viper
synced 2025-05-07 12:47:18 +00:00
Add string replacer interface
This commit is contained in:
parent
d104d259b3
commit
d77e7a7fbf
2 changed files with 13 additions and 7 deletions
|
@ -185,8 +185,8 @@ with ENV:
|
||||||
* `AutomaticEnv()`
|
* `AutomaticEnv()`
|
||||||
* `BindEnv(string...) : error`
|
* `BindEnv(string...) : error`
|
||||||
* `SetEnvPrefix(string)`
|
* `SetEnvPrefix(string)`
|
||||||
* `SetEnvKeyReplacer(string...) *strings.Replacer`
|
* `SetEnvKeyReplacer(StringReplacer)`
|
||||||
* `AllowEmptyEnvVar(bool)`
|
* `AllowEmptyEnvVar(bool)`
|
||||||
|
|
||||||
_When working with ENV variables, it’s important to recognize that Viper
|
_When working with ENV variables, it’s important to recognize that Viper
|
||||||
treats ENV variables as case sensitive._
|
treats ENV variables as case sensitive._
|
||||||
|
@ -213,7 +213,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
|
check for a environment variable with a name matching the key uppercased and
|
||||||
prefixed with the `EnvPrefix` if set.
|
prefixed with the `EnvPrefix` if set.
|
||||||
|
|
||||||
`SetEnvKeyReplacer` allows you to use a `strings.Replacer` object to rewrite Env
|
`SetEnvKeyReplacer` allows you to use a `StringReplacer` object to rewrite Env
|
||||||
keys to an extent. This is useful if you want to use `-` or something in your
|
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`.
|
||||||
|
|
14
viper.go
14
viper.go
|
@ -131,6 +131,12 @@ func DecodeHook(hook mapstructure.DecodeHookFunc) DecoderConfigOption {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
// Viper is a prioritized configuration registry. It
|
// Viper is a prioritized configuration registry. It
|
||||||
// maintains a set of configuration sources, fetches
|
// maintains a set of configuration sources, fetches
|
||||||
// values to populate those, and provides them according
|
// values to populate those, and provides them according
|
||||||
|
@ -186,7 +192,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{}
|
||||||
|
@ -1095,11 +1101,11 @@ func (v *Viper) AutomaticEnv() {
|
||||||
v.automaticEnvApplied = true
|
v.automaticEnvApplied = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetEnvKeyReplacer sets the strings.Replacer on the viper object
|
// SetEnvKeyReplacer sets the StringReplacer on the viper object
|
||||||
// Useful for mapping an environmental variable to a key that does
|
// Useful for mapping an environmental variable to a key that does
|
||||||
// not match it.
|
// not match it.
|
||||||
func SetEnvKeyReplacer(r *strings.Replacer) { v.SetEnvKeyReplacer(r) }
|
func SetEnvKeyReplacer(r StringReplacer) { v.SetEnvKeyReplacer(r) }
|
||||||
func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) {
|
func (v *Viper) SetEnvKeyReplacer(r StringReplacer) {
|
||||||
v.envKeyReplacer = r
|
v.envKeyReplacer = r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue