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: register dynamic config with both disk and live #14453

Merged
merged 1 commit into from
Nov 3, 2023
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
27 changes: 23 additions & 4 deletions go/viperutil/internal/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,29 @@ func (v *Viper) loadFromDisk() {

// begin implementation of registry.Bindable for sync.Viper

func (v *Viper) BindEnv(vars ...string) error { return v.disk.BindEnv(vars...) }
func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error { return v.disk.BindPFlag(key, flag) }
func (v *Viper) RegisterAlias(alias string, key string) { v.disk.RegisterAlias(alias, key) }
func (v *Viper) SetDefault(key string, value any) { v.disk.SetDefault(key, value) }
func (v *Viper) BindEnv(vars ...string) error {
if err := v.disk.BindEnv(vars...); err != nil {
return err
}
return v.live.BindEnv(vars...)
}

func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error {
if err := v.disk.BindPFlag(key, flag); err != nil {
return err
}
return v.live.BindPFlag(key, flag)
}

func (v *Viper) RegisterAlias(alias string, key string) {
v.disk.RegisterAlias(alias, key)
v.live.RegisterAlias(alias, key)
}

func (v *Viper) SetDefault(key string, value any) {
v.disk.SetDefault(key, value)
v.live.SetDefault(key, value)
}

// end implementation of registry.Bindable for sync.Viper

Expand Down
5 changes: 4 additions & 1 deletion go/viperutil/internal/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ func TestWatchConfig(t *testing.T) {

sv := vipersync.New()
A := viperutil.Configure("a", viperutil.Options[int]{Dynamic: true})
B := viperutil.Configure("b", viperutil.Options[int]{Dynamic: true})
B := viperutil.Configure("b", viperutil.Options[int]{FlagName: "b", Dynamic: true, Default: 5})

// Check that default values are actually used
require.Equal(t, B.Get(), B.Default())
Comment on lines +96 to +99
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed that this fails on main:

$ go test -timeout 30s -run ^TestWatchConfig$ vitess.io/vitess/go/viperutil/internal/sync

--- FAIL: TestWatchConfig (0.00s)
    /Users/matt/git/vitess/go/viperutil/internal/sync/sync_test.go:99: 
        	Error Trace:	/Users/matt/git/vitess/go/viperutil/internal/sync/sync_test.go:99
        	Error:      	Not equal: 
        	            	expected: 0
        	            	actual  : 5
        	Test:       	TestWatchConfig

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it did. I started with the test (created by @aquarapid) and went from there.


A.(*value.Dynamic[int]).Base.BoundGetFunc = vipersync.AdaptGetter("a", func(v *viper.Viper) func(key string) int {
return v.GetInt
Expand Down
Loading