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

viper.Sub() not extracting nested default values #747

Open
edermi opened this issue Aug 14, 2019 · 2 comments · May be fixed by #1439
Open

viper.Sub() not extracting nested default values #747

edermi opened this issue Aug 14, 2019 · 2 comments · May be fixed by #1439

Comments

@edermi
Copy link

edermi commented Aug 14, 2019

I noticed an issue where viper.Sub() fails to extract default values of a nested key. I attached demo code to reproduce the issue using latest viper and Go 1.12.6 under x64_linux.

main.go:

package main

import (
	"fmt"

	"github.com/davecgh/go-spew/spew"
	"github.com/spf13/viper"
)

func main() {
	viper.SetDefault("config.value2.internal", 3)
	viper.SetConfigFile("./demo.yaml")
	err := viper.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("Fatal error config file: %s ", err))
	}
	// Until now, everything works as expected
	fmt.Printf("config.value1: %d\n", viper.GetInt("config.value1"))
	fmt.Printf("config.value2: %d\n", viper.GetInt("config.value2"))
	fmt.Printf("config.value2.internal: %d\n", viper.GetInt("config.value2.internal"))

	// Extract the config subtree
	v := viper.Sub("config")
	// This doesn't work if value2 node is missing in configuration
	fmt.Printf("value2.internal: %d\n", v.GetInt("value2.internal"))

	spew.Dump(v.AllSettings())
}

demo.yaml (works as inteded, but defaults are useless since everything is explicitly set):

config:
  value1: 1
  value2: 
    internal: 3

demo.yaml (broken):

config:
  value1: 1
  #value2: 
  #  internal: 3

Output for the broken example:

config.value1: 1
config.value2: 0
config.value2.internal: 3
value2.internal: 0
(map[string]interface {}) (len=1) {
 (string) (len=6) "value1": (int) 1
}

Whereas I would expect to have value2.internal: 3

The issue seems somehow related / similar to issue #71 as well as PR #195

@aquincum
Copy link

pretty impressive this hasn't been fixed in 3 years and it seems like it doesn't impact many...

setrofim added a commit to setrofim/viper that referenced this issue Sep 28, 2022
When fetching a configuration sub-tree (either via Get() that results
in a map, or through Sub()), ensure that any defaults for keys under
that tree are propagated into the returned result.

Fixes spf13#747

Signed-off-by: setrofim <setrofim@gmail.com>
@setrofim setrofim linked a pull request Sep 28, 2022 that will close this issue
@setrofim
Copy link

I ran into related issue when Get()'ing a sub-tree that defaults specified for some of its keys. The issue is that when a value is specified for a path in the config, it effectively shadows the corresponding path in the defaults. But if the nested deeper path is not present in the set value, path resolution falls back to looking in the defaults. This results in potentially inconsistent behaviour when accessing a configuration point via its full path results in one value (the default), while accessing it via a relative path from a sub-tree results an that type's null value (nil, 0, "", etc).

Potential fix in #1439

(note: this technically changes existing expected behaviour -- see the updated overrides_test.go; however, given this issue, and the referenced previous discussions, merging nested keys from defaults seems like desirable behaviour?)

setrofim added a commit to veraison/services that referenced this issue Sep 29, 2022
Viper currently has a bug where defaults do not get properly propagated
when extracting a configuration sub-tree. See

	spf13/viper#747

A pull request has been opened with a potential fix:

	spf13/viper#1439

Until the fix (or some alternative) gets merged, switch to using the
forked version.
setrofim added a commit to veraison/services that referenced this issue Sep 29, 2022
Viper currently has a bug where defaults do not get properly propagated
when extracting a configuration sub-tree. See

	spf13/viper#747

A pull request has been opened with a potential fix:

	spf13/viper#1439

Until the fix (or some alternative) gets merged, switch to using the
forked version.
setrofim added a commit to veraison/services that referenced this issue Sep 29, 2022
Viper currently has a bug where defaults do not get properly propagated
when extracting a configuration sub-tree. See

	spf13/viper#747

A pull request has been opened with a potential fix:

	spf13/viper#1439

Until the fix (or some alternative) gets merged, switch to using the
forked version.
setrofim added a commit to setrofim/viper that referenced this issue Sep 29, 2022
When fetching a configuration sub-tree (either via Get() that results
in a map, or through Sub()), ensure that any defaults for keys under
that tree are propagated into the returned result.

Fixes spf13#747

Signed-off-by: setrofim <setrofim@gmail.com>
setrofim added a commit to setrofim/viper that referenced this issue Sep 29, 2022
When fetching a configuration sub-tree (either via Get() that results
in a map, or through Sub()), ensure that any defaults for keys under
that tree are propagated into the returned result.

Fixes spf13#747

Signed-off-by: setrofim <setrofim@gmail.com>
setrofim added a commit to setrofim/viper that referenced this issue Sep 29, 2022
When fetching a configuration sub-tree (either via Get() that results
in a map, or through Sub()), ensure that any defaults for keys under
that tree are propagated into the returned result.

Fixes spf13#747

Signed-off-by: setrofim <setrofim@gmail.com>
setrofim added a commit to veraison/services that referenced this issue Sep 30, 2022
Viper currently has a bug where defaults do not get properly propagated
when extracting a configuration sub-tree. See

	spf13/viper#747

A pull request has been opened with a potential fix:

	spf13/viper#1439

Until the fix (or some alternative) gets merged, switch to using the
forked version.
setrofim added a commit to veraison/services that referenced this issue Sep 30, 2022
Viper currently has a bug where defaults do not get properly propagated
when extracting a configuration sub-tree. See

	spf13/viper#747

A pull request has been opened with a potential fix:

	spf13/viper#1439

Until the fix (or some alternative) gets merged, switch to using the
forked version.

Signed-off-by: Sergei Trofimov <sergei.trofimov@arm.com>
setrofim added a commit to veraison/services that referenced this issue Sep 30, 2022
Viper currently has a bug where defaults do not get properly propagated
when extracting a configuration sub-tree. See

	spf13/viper#747

A pull request has been opened with a potential fix:

	spf13/viper#1439

Until the fix (or some alternative) gets merged, switch to using the
forked version.

Signed-off-by: Sergei Trofimov <sergei.trofimov@arm.com>
setrofim added a commit to veraison/services that referenced this issue Oct 1, 2022
Viper currently has a bug where defaults do not get properly propagated
when extracting a configuration sub-tree. See

	spf13/viper#747

A pull request has been opened with a potential fix:

	spf13/viper#1439

Until the fix (or some alternative) gets merged, switch to using the
forked version.

Signed-off-by: Sergei Trofimov <sergei.trofimov@arm.com>
setrofim added a commit to veraison/services that referenced this issue Oct 1, 2022
Viper currently has a bug where defaults do not get properly propagated
when extracting a configuration sub-tree. See

	spf13/viper#747

A pull request has been opened with a potential fix:

	spf13/viper#1439

Until the fix (or some alternative) gets merged, switch to using the
forked version.

Signed-off-by: Sergei Trofimov <sergei.trofimov@arm.com>
setrofim added a commit to veraison/services that referenced this issue Oct 3, 2022
Viper currently has a bug where defaults do not get properly propagated
when extracting a configuration sub-tree. See

	spf13/viper#747

A pull request has been opened with a potential fix:

	spf13/viper#1439

Until the fix (or some alternative) gets merged, switch to using the
forked version.

Signed-off-by: Sergei Trofimov <sergei.trofimov@arm.com>
setrofim added a commit to veraison/services that referenced this issue Oct 3, 2022
Viper currently has a bug where defaults do not get properly propagated
when extracting a configuration sub-tree. See

	spf13/viper#747

A pull request has been opened with a potential fix:

	spf13/viper#1439

Until the fix (or some alternative) gets merged, switch to using the
forked version.

Signed-off-by: Sergei Trofimov <sergei.trofimov@arm.com>
setrofim added a commit to veraison/services that referenced this issue Oct 3, 2022
Viper currently has a bug where defaults do not get properly propagated
when extracting a configuration sub-tree. See

	spf13/viper#747

A pull request has been opened with a potential fix:

	spf13/viper#1439

Until the fix (or some alternative) gets merged, switch to using the
forked version.

Signed-off-by: Sergei Trofimov <sergei.trofimov@arm.com>
setrofim added a commit to setrofim/viper that referenced this issue Oct 3, 2022
When fetching a configuration sub-tree (either via Get() that results
in a map, or through Sub()), ensure that any defaults for keys under
that tree are propagated into the returned result.

Fixes spf13#747

Signed-off-by: setrofim <setrofim@gmail.com>
setrofim added a commit to setrofim/viper that referenced this issue Oct 3, 2022
When fetching a configuration sub-tree (either via Get() that results
in a map, or through Sub()), ensure that any defaults for keys under
that tree are propagated into the returned result.

Fixes spf13#747

Signed-off-by: setrofim <setrofim@gmail.com>
SabreenKaur pushed a commit to veraison/services that referenced this issue Oct 7, 2022
Viper currently has a bug where defaults do not get properly propagated
when extracting a configuration sub-tree. See

	spf13/viper#747

A pull request has been opened with a potential fix:

	spf13/viper#1439

Until the fix (or some alternative) gets merged, switch to using the
forked version.

Signed-off-by: Sergei Trofimov <sergei.trofimov@arm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants