mirror of
https://github.com/spf13/viper
synced 2025-05-10 22:27:18 +00:00
SetEnvKeyReplacer(): replace concrete type "strings.Replacer" with its interface
This commit is contained in:
parent
62edee3196
commit
a9236e1fbf
2 changed files with 13 additions and 7 deletions
|
@ -185,7 +185,7 @@ with ENV:
|
||||||
* `AutomaticEnv()`
|
* `AutomaticEnv()`
|
||||||
* `BindEnv(string...) : error`
|
* `BindEnv(string...) : error`
|
||||||
* `SetEnvPrefix(string)`
|
* `SetEnvPrefix(string)`
|
||||||
* `SetEnvKeyReplacer(string...) *strings.Replacer`
|
* `SetEnvKeyReplacer(string...) *replacer`
|
||||||
|
|
||||||
_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._
|
||||||
|
@ -212,8 +212,9 @@ 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 `replacer` interface to rewrite Env
|
||||||
keys to an extent. This is useful if you want to use `-` or something in your
|
keys to an extent. The most popular implementation of the interface is
|
||||||
|
`strings.Replacer`. 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`.
|
||||||
|
|
||||||
|
|
13
viper.go
13
viper.go
|
@ -186,7 +186,7 @@ type Viper struct {
|
||||||
envPrefix string
|
envPrefix string
|
||||||
|
|
||||||
automaticEnvApplied bool
|
automaticEnvApplied bool
|
||||||
envKeyReplacer *strings.Replacer
|
envKeyReplacer replacer
|
||||||
|
|
||||||
config map[string]interface{}
|
config map[string]interface{}
|
||||||
override map[string]interface{}
|
override map[string]interface{}
|
||||||
|
@ -1084,11 +1084,16 @@ func (v *Viper) AutomaticEnv() {
|
||||||
v.automaticEnvApplied = true
|
v.automaticEnvApplied = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetEnvKeyReplacer sets the strings.Replacer on the viper object
|
// replacer is the interface that a replacement algorithm needs to implement.
|
||||||
|
type replacer interface {
|
||||||
|
Replace(s string) string
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetEnvKeyReplacer sets the replacer 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 replacer) { v.SetEnvKeyReplacer(r) }
|
||||||
func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) {
|
func (v *Viper) SetEnvKeyReplacer(r replacer) {
|
||||||
v.envKeyReplacer = r
|
v.envKeyReplacer = r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue