From 24110e809c9d599fddaaf2f8cdebe14cdf84b2f1 Mon Sep 17 00:00:00 2001 From: Deepthi Sigireddi Date: Fri, 3 Nov 2023 09:52:09 -0700 Subject: [PATCH] viper: register dynamic config with both disk and live (#14453) Signed-off-by: deepthi --- go/viperutil/internal/sync/sync.go | 27 +++++++++++++++++++++---- go/viperutil/internal/sync/sync_test.go | 5 ++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/go/viperutil/internal/sync/sync.go b/go/viperutil/internal/sync/sync.go index a5d35c504cb..6608569d86c 100644 --- a/go/viperutil/internal/sync/sync.go +++ b/go/viperutil/internal/sync/sync.go @@ -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 diff --git a/go/viperutil/internal/sync/sync_test.go b/go/viperutil/internal/sync/sync_test.go index 5b1c5541896..6b8efa1b105 100644 --- a/go/viperutil/internal/sync/sync_test.go +++ b/go/viperutil/internal/sync/sync_test.go @@ -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