mirror of https://github.com/spf13/viper.git
feat: new settings for viper, edit processing of an aliases
This commit is contained in:
parent
8f34134e70
commit
e5316b8387
42
viper.go
42
viper.go
|
@ -206,6 +206,8 @@ type Viper struct {
|
||||||
automaticEnvApplied bool
|
automaticEnvApplied bool
|
||||||
envKeyReplacer StringReplacer
|
envKeyReplacer StringReplacer
|
||||||
allowEmptyEnv bool
|
allowEmptyEnv bool
|
||||||
|
aliasesFirstly bool
|
||||||
|
aliasesStepByStep bool
|
||||||
|
|
||||||
parents []string
|
parents []string
|
||||||
config map[string]any
|
config map[string]any
|
||||||
|
@ -241,6 +243,8 @@ func New() *Viper {
|
||||||
v.pflags = make(map[string]FlagValue)
|
v.pflags = make(map[string]FlagValue)
|
||||||
v.env = make(map[string][]string)
|
v.env = make(map[string][]string)
|
||||||
v.aliases = make(map[string]string)
|
v.aliases = make(map[string]string)
|
||||||
|
v.aliasesFirstly = true
|
||||||
|
v.aliasesStepByStep = false
|
||||||
v.typeByDefValue = false
|
v.typeByDefValue = false
|
||||||
v.logger = slog.New(&discardHandler{})
|
v.logger = slog.New(&discardHandler{})
|
||||||
|
|
||||||
|
@ -539,6 +543,25 @@ func (v *Viper) mergeWithEnvPrefix(in string) string {
|
||||||
return strings.ToUpper(in)
|
return strings.ToUpper(in)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AliasesFirstly changes the order of processing aliases.
|
||||||
|
// If TRUE is passed, values will be searched by alias.
|
||||||
|
// If FALSE is passed, the standard key is processed first,
|
||||||
|
// and if the value is not found, an alias search will be performed.
|
||||||
|
func AliasesFirstly(firstly bool) { v.AliasesFirstly(firstly) }
|
||||||
|
|
||||||
|
func (v *Viper) AliasesFirstly(firstly bool) {
|
||||||
|
v.aliasesFirstly = firstly
|
||||||
|
}
|
||||||
|
|
||||||
|
// AliasesStepByStep changes the processing of nested aliases.
|
||||||
|
// If TRUE is passed, the values will be searched at each nesting level of the alias.
|
||||||
|
// If FALSE is passed, the values will be searched at the last nesting level of the aliases
|
||||||
|
func AliasesStepByStep(enable bool) { v.AliasesStepByStep(enable) }
|
||||||
|
|
||||||
|
func (v *Viper) AliasesStepByStep(enable bool) {
|
||||||
|
v.aliasesStepByStep = enable
|
||||||
|
}
|
||||||
|
|
||||||
// AllowEmptyEnv tells Viper to consider set,
|
// AllowEmptyEnv tells Viper to consider set,
|
||||||
// but empty environment variables as valid values instead of falling back.
|
// but empty environment variables as valid values instead of falling back.
|
||||||
// For backward compatibility reasons this is false by default.
|
// For backward compatibility reasons this is false by default.
|
||||||
|
@ -1294,8 +1317,8 @@ func (v *Viper) MustBindEnv(input ...string) {
|
||||||
// Note: this assumes a lower-cased key given.
|
// Note: this assumes a lower-cased key given.
|
||||||
func (v *Viper) find(lcaseKey string, flagDefault bool) any {
|
func (v *Viper) find(lcaseKey string, flagDefault bool) any {
|
||||||
var (
|
var (
|
||||||
|
lastKey string
|
||||||
val any
|
val any
|
||||||
exists bool
|
|
||||||
path = strings.Split(lcaseKey, v.keyDelim)
|
path = strings.Split(lcaseKey, v.keyDelim)
|
||||||
nested = len(path) > 1
|
nested = len(path) > 1
|
||||||
)
|
)
|
||||||
|
@ -1305,10 +1328,14 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) any {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for lastKey != lcaseKey {
|
||||||
|
if v.aliasesFirstly {
|
||||||
// if the requested key is an alias, then return the proper key
|
// if the requested key is an alias, then return the proper key
|
||||||
|
lastKey = lcaseKey
|
||||||
lcaseKey = v.realKey(lcaseKey)
|
lcaseKey = v.realKey(lcaseKey)
|
||||||
path = strings.Split(lcaseKey, v.keyDelim)
|
path = strings.Split(lcaseKey, v.keyDelim)
|
||||||
nested = len(path) > 1
|
nested = len(path) > 1
|
||||||
|
}
|
||||||
|
|
||||||
// Set() override first
|
// Set() override first
|
||||||
val = v.searchMap(v.override, path)
|
val = v.searchMap(v.override, path)
|
||||||
|
@ -1438,6 +1465,15 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) any {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// last item, no need to check shadowing
|
// last item, no need to check shadowing
|
||||||
|
|
||||||
|
if !v.aliasesFirstly {
|
||||||
|
// if the requested key is an alias, then return the proper key
|
||||||
|
lastKey = lcaseKey
|
||||||
|
lcaseKey = v.realKey(lcaseKey)
|
||||||
|
path = strings.Split(lcaseKey, v.keyDelim)
|
||||||
|
nested = len(path) > 1
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -1571,7 +1607,9 @@ func (v *Viper) realKey(key string) string {
|
||||||
newkey, exists := v.aliases[key]
|
newkey, exists := v.aliases[key]
|
||||||
if exists {
|
if exists {
|
||||||
v.logger.Debug("key is an alias", "alias", key, "to", newkey)
|
v.logger.Debug("key is an alias", "alias", key, "to", newkey)
|
||||||
|
if v.aliasesStepByStep {
|
||||||
|
return newkey
|
||||||
|
}
|
||||||
return v.realKey(newkey)
|
return v.realKey(newkey)
|
||||||
}
|
}
|
||||||
return key
|
return key
|
||||||
|
|
Loading…
Reference in New Issue