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

[release-17.0] viper: register dynamic config with both disk and live (#14453) #14454

Merged
merged 1 commit into from
Nov 4, 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 @@ -272,10 +272,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 @@ -196,7 +196,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())

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