From f55aacba8a678293d8a0dd9c40ec5975b5e407c8 Mon Sep 17 00:00:00 2001 From: searKing <471030698@qq.com> Date: Thu, 3 Nov 2022 16:56:12 +0800 Subject: [PATCH] fix: unmarshalReader to an empty kvstore, just as ReadConfig. for example, avoid kvstore not changed if read an empty yaml --- viper.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/viper.go b/viper.go index 5f76cc0..067f370 100644 --- a/viper.go +++ b/viper.go @@ -1903,8 +1903,13 @@ func (v *Viper) getRemoteConfig(provider RemoteProvider) (map[string]interface{} if err != nil { return nil, err } - err = v.unmarshalReader(reader, v.kvstore) - return v.kvstore, err + kvstore := make(map[string]interface{}) + err = v.unmarshalReader(reader, kvstore) + if err != nil { + return nil, err + } + v.kvstore = kvstore + return v.kvstore, nil } // Retrieve the first found remote configuration. @@ -1920,7 +1925,13 @@ func (v *Viper) watchKeyValueConfigOnChannel() error { for { b := <-rc reader := bytes.NewReader(b.Value) - v.unmarshalReader(reader, v.kvstore) + kvstore := make(map[string]interface{}) + err := v.unmarshalReader(reader, kvstore) + if err != nil { + v.logger.Warn(fmt.Errorf("watch remote config: %w", err).Error()) + continue + } + v.kvstore = kvstore } }(respc) return nil @@ -1952,8 +1963,13 @@ func (v *Viper) watchRemoteConfig(provider RemoteProvider) (map[string]interface if err != nil { return nil, err } - err = v.unmarshalReader(reader, v.kvstore) - return v.kvstore, err + kvstore := make(map[string]interface{}) + err = v.unmarshalReader(reader, kvstore) + if err != nil { + return nil, err + } + v.kvstore = kvstore + return v.kvstore, nil } // AllKeys returns all keys holding a value, regardless of where they are set.