fix: unmarshalReader to an empty kvstore, just as ReadConfig. for example, avoid kvstore not changed if read an empty yaml

This commit is contained in:
searKing 2022-11-03 16:56:12 +08:00
parent f1d2c470bf
commit f55aacba8a

View file

@ -1903,8 +1903,13 @@ func (v *Viper) getRemoteConfig(provider RemoteProvider) (map[string]interface{}
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = v.unmarshalReader(reader, v.kvstore) kvstore := make(map[string]interface{})
return v.kvstore, err err = v.unmarshalReader(reader, kvstore)
if err != nil {
return nil, err
}
v.kvstore = kvstore
return v.kvstore, nil
} }
// Retrieve the first found remote configuration. // Retrieve the first found remote configuration.
@ -1920,7 +1925,13 @@ func (v *Viper) watchKeyValueConfigOnChannel() error {
for { for {
b := <-rc b := <-rc
reader := bytes.NewReader(b.Value) 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) }(respc)
return nil return nil
@ -1952,8 +1963,13 @@ func (v *Viper) watchRemoteConfig(provider RemoteProvider) (map[string]interface
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = v.unmarshalReader(reader, v.kvstore) kvstore := make(map[string]interface{})
return v.kvstore, err 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. // AllKeys returns all keys holding a value, regardless of where they are set.