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
PR #673 broke my code. The nested structures don't get the default values correctly #764
Comments
@flw-cn Thanks for opening this PR. It seems that the linked PR indeed introduced a regression. |
It looks like this only affects flags? The new |
I explained in #673 why that's the case. Defaults and explicit sets are also affected. Basically everything that's not a nested structure (config files, key value stores) |
I'm not seeing the same behaviour for package viper
import (
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"testing"
)
type Config struct {
Foo struct {
Bar string
}
}
func Test673_PFlags(t *testing.T) {
v := New()
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
flags.String("foo.bar", "cobra_flag", "")
assert.NoError(t, v.BindPFlags(flags))
config := &Config{}
assert.NoError(t, v.Unmarshal(config))
assert.Equal(t, "cobra_flag", config.Foo.Bar)
}
func Test673_Default(t *testing.T) {
v := New()
config := &Config{}
v.SetDefault("foo.bar", "default")
assert.NoError(t, v.Unmarshal(config))
assert.Equal(t, "default", config.Foo.Bar)
}
func Test673_Set(t *testing.T) {
v := New()
config := &Config{}
v.SetDefault("foo.bar", "default")
v.Set("foo.bar", "override")
assert.NoError(t, v.Unmarshal(config))
assert.Equal(t, "override", config.Foo.Bar)
} Currently for me, the |
I wrote a short example to illustrate the problem.
The above code I expected to get the output is:
This has worked very well in the past. But since PR #673, it can't work properly, it will output:
I checked the contents of the PR #673 , I can't fully understand the changes. But I believe the changes blocked my previous usage.
How should I modify my code to fit the new version of viper?
The text was updated successfully, but these errors were encountered: