Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Revert "Fix Viper 'UnmarshalKey' behavior to include all sources #37

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,13 +865,27 @@ func UnmarshalKey(key string, rawVal interface{}, opts ...DecoderConfigOption) e
return v.UnmarshalKey(key, rawVal, opts...)
}
func (v *Viper) UnmarshalKey(key string, rawVal interface{}, opts ...DecoderConfigOption) error {
err := decode(v.Get(key), defaultDecoderConfig(rawVal, opts...))
lcaseKey := strings.ToLower(key)

if err != nil {
return err
}
// AllSettings returns settings from every sources merged into one tree
settings := v.AllSettings()

return nil
keyParts := strings.Split(lcaseKey, v.keyDelim)
for i := 0; i < len(keyParts)-1; i++ {
if value, found := settings[keyParts[i]]; found {
if valueMap, ok := value.(map[string]interface{}); ok {
settings = valueMap
continue
}
// if the current value is not a map[string]interface{} we most likely reach a
// leaf and the key/path is wrong
return fmt.Errorf("unknown key %s", lcaseKey)
} else {
return fmt.Errorf("unknown key %s", lcaseKey)
}
}
finalSetting := settings[keyParts[len(keyParts)-1]]
return decode(finalSetting, defaultDecoderConfig(rawVal, opts...))
}

// Unmarshal unmarshals the config into a Struct. Make sure that the tags
Expand Down
42 changes: 42 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,48 @@ func TestParseNested(t *testing.T) {
assert.Equal(t, 200*time.Millisecond, items[0].Nested.Delay)
}

func TestUnmarshalKey(t *testing.T) {
type testStruct struct {
Delay int `mapstructure:"delay" yaml:"delay"`
Port int `mapstructure:"port" yaml:"port"`
Host string `mapstructure:"host" yaml:"host"`
Items []string `mapstructure:"items" yaml:"items"`
}

config := `
level_1:
level_2:
port: 1234
items:
- "test 1"
`
v := New()
v.SetDefault("level_1.level_2.delay", 50)
v.SetDefault("level_1.level_2.port", 9999)
v.SetDefault("level_1.level_2.host", "default")
v.SetDefault("level_1.level_2.items", []string{})

// Use env vars for some settings
v.BindEnv("level_1.level_2.host", "DD_TEST_HOST")
t.Setenv("DD_TEST_HOST", "dd.com")

initConfig(v, "yaml", config)

// manually overwrite some settings
v.Set("level_1.level_2.items", []string{"test_2", "test_3"})

data := testStruct{}
err := v.UnmarshalKey("level_1.level_2", &data)
if err != nil {
t.Fatalf("unable to decode into struct, %v", err)
}

assert.Equal(t, 50, data.Delay) // value from defaults
assert.Equal(t, 1234, data.Port) // value from config
assert.Equal(t, "dd.com", data.Host) // value from env
assert.Equal(t, []string{"test_2", "test_3"}, data.Items) // value from Set()
}

func doTestCaseInsensitive(t *testing.T, typ, config string) {
v := New()
initConfig(v, typ, config)
Expand Down
Loading