Skip to content

Commit

Permalink
[release-18.0] viper: register dynamic config with both disk and live (
Browse files Browse the repository at this point in the history
…#14453) (#14455)

Signed-off-by: deepthi <deepthi@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
  • Loading branch information
vitess-bot[bot] authored Nov 3, 2023
1 parent a84153a commit c83534b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
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())

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

0 comments on commit c83534b

Please sign in to comment.