Skip to content

Commit

Permalink
viper: register dynamic config with both disk and live (#14453)
Browse files Browse the repository at this point in the history
Signed-off-by: deepthi <[email protected]>
  • Loading branch information
deepthi authored Nov 3, 2023
1 parent 0ea4b09 commit 24110e8
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 24110e8

Please sign in to comment.