mirror of
https://github.com/spf13/viper
synced 2025-05-06 20:27:17 +00:00
Fix: find() looks into "overrides" first
overrides (elements added with Set()) should have highest priority, checked first (was after PFlags). From README.md: > Viper uses the following precedence order. Each item takes precedence > over the item below it: > • explicit call to Set > • flag > • env > • config > • key/value store > • default + fix TestBindPFlags() to use a new Viper instance (otherwise, "port" was defined in an earlier test function with Set() and took precedence)
This commit is contained in:
parent
a60edc6a52
commit
86fffbd28c
2 changed files with 14 additions and 5 deletions
14
viper.go
14
viper.go
|
@ -759,7 +759,13 @@ func (v *Viper) find(key string) interface{} {
|
||||||
// if the requested key is an alias, then return the proper key
|
// if the requested key is an alias, then return the proper key
|
||||||
key = v.realKey(key)
|
key = v.realKey(key)
|
||||||
|
|
||||||
// PFlag Override first
|
// Set() override first
|
||||||
|
val, exists = v.override[key]
|
||||||
|
if exists {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
// PFlag override next
|
||||||
flag, exists := v.pflags[key]
|
flag, exists := v.pflags[key]
|
||||||
if exists && flag.HasChanged() {
|
if exists && flag.HasChanged() {
|
||||||
switch flag.ValueType() {
|
switch flag.ValueType() {
|
||||||
|
@ -787,7 +793,6 @@ func (v *Viper) find(key string) interface{} {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
envkey, exists := v.env[key]
|
envkey, exists := v.env[key]
|
||||||
if exists {
|
if exists {
|
||||||
if val = v.getEnv(envkey); val != "" {
|
if val = v.getEnv(envkey); val != "" {
|
||||||
|
@ -795,12 +800,13 @@ func (v *Viper) find(key string) interface{} {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Config file next
|
||||||
val, exists = v.config[key]
|
val, exists = v.config[key]
|
||||||
if exists {
|
if exists {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test for nested config parameter
|
// test for nested config parameter
|
||||||
if strings.Contains(key, v.keyDelim) {
|
if strings.Contains(key, v.keyDelim) {
|
||||||
path := strings.Split(key, v.keyDelim)
|
path := strings.Split(key, v.keyDelim)
|
||||||
|
|
||||||
|
@ -815,11 +821,13 @@ func (v *Viper) find(key string) interface{} {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// K/V store next
|
||||||
val, exists = v.kvstore[key]
|
val, exists = v.kvstore[key]
|
||||||
if exists {
|
if exists {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Default as last chance
|
||||||
val, exists = v.defaults[key]
|
val, exists = v.defaults[key]
|
||||||
if exists {
|
if exists {
|
||||||
return val
|
return val
|
||||||
|
|
|
@ -479,6 +479,7 @@ func TestUnmarshal(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBindPFlags(t *testing.T) {
|
func TestBindPFlags(t *testing.T) {
|
||||||
|
v := New() // create independent Viper object
|
||||||
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||||
|
|
||||||
var testValues = map[string]*string{
|
var testValues = map[string]*string{
|
||||||
|
@ -497,7 +498,7 @@ func TestBindPFlags(t *testing.T) {
|
||||||
testValues[name] = flagSet.String(name, "", "test")
|
testValues[name] = flagSet.String(name, "", "test")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := BindPFlags(flagSet)
|
err := v.BindPFlags(flagSet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error binding flag set, %v", err)
|
t.Fatalf("error binding flag set, %v", err)
|
||||||
}
|
}
|
||||||
|
@ -508,7 +509,7 @@ func TestBindPFlags(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
for name, expected := range mutatedTestValues {
|
for name, expected := range mutatedTestValues {
|
||||||
assert.Equal(t, expected, Get(name))
|
assert.Equal(t, expected, v.Get(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue