Fix watch register.

This commit is contained in:
cyprinus 2023-06-04 12:03:01 +08:00
parent f04ffd899d
commit eebc2f0d0b

View file

@ -508,12 +508,13 @@ func (v *Viper) WatchConfig() {
} }
// updateRegisteredConfig validate the registered config items in the new config, notify user with the hook functions. // updateRegisteredConfig validate the registered config items in the new config, notify user with the hook functions.
func (v *Viper) updateRegisteredConfig(newConfigViper *Viper) { func (v *Viper) updateRegisteredConfig(newConfig map[string]interface{}) map[string]interface{} {
for key, config := range v.registered { for key, config := range v.registered {
oldValue := v.Get(key) oldValue := v.Get(key)
newValue := newConfigViper.Get(key) newValue := newConfig[key]
// Check exist // Check exist
if newValue == nil && !config.CanBeNil { if newValue == nil && !config.CanBeNil {
newConfig[key] = oldValue
if config.OnUpdateFailed != nil { if config.OnUpdateFailed != nil {
config.OnUpdateFailed(&Event{ config.OnUpdateFailed(&Event{
old: oldValue, old: oldValue,
@ -527,6 +528,7 @@ func (v *Viper) updateRegisteredConfig(newConfigViper *Viper) {
newValueJson, _ := js.Marshal(newValue) newValueJson, _ := js.Marshal(newValue)
err := js.Unmarshal(newValueJson, config.Schema) err := js.Unmarshal(newValueJson, config.Schema)
if err != nil { if err != nil {
newConfig[key] = oldValue
config.OnUpdateFailed(&Event{ config.OnUpdateFailed(&Event{
old: oldValue, old: oldValue,
new: nil, new: nil,
@ -536,6 +538,7 @@ func (v *Viper) updateRegisteredConfig(newConfigViper *Viper) {
// Validation // Validation
if !config.Validator(config.Schema) { if !config.Validator(config.Schema) {
newConfig[key] = oldValue
config.OnUpdateFailed(&Event{ config.OnUpdateFailed(&Event{
old: oldValue, old: oldValue,
new: nil, new: nil,
@ -544,7 +547,7 @@ func (v *Viper) updateRegisteredConfig(newConfigViper *Viper) {
} }
// Success // Success
v.Set(key, config.Schema) newConfig[key] = config.Schema
if config.OnUpdate != nil { if config.OnUpdate != nil {
config.OnUpdate(&Event{ config.OnUpdate(&Event{
new: config.Schema, new: config.Schema,
@ -552,6 +555,7 @@ func (v *Viper) updateRegisteredConfig(newConfigViper *Viper) {
}) })
} }
} }
return newConfig
} }
// SetConfigFile explicitly defines the path, name and extension of the config file. // SetConfigFile explicitly defines the path, name and extension of the config file.
@ -1619,13 +1623,11 @@ func (v *Viper) ReadInConfig() error {
} }
config := make(map[string]interface{}) config := make(map[string]interface{})
tempViper := New() err = v.unmarshalReader(bytes.NewReader(file), config)
tempViper.configType = v.getConfigType()
err = tempViper.unmarshalReader(bytes.NewReader(file), config)
if err != nil { if err != nil {
return err return err
} }
v.updateRegisteredConfig(tempViper) config = v.updateRegisteredConfig(config)
v.config = config v.config = config
return nil return nil
@ -1980,10 +1982,9 @@ func (v *Viper) getRemoteConfig(provider RemoteProvider) (map[string]interface{}
if err != nil { if err != nil {
return nil, err return nil, err
} }
tempViper := New() kvStore := make(map[string]interface{})
tempViper.configType = v.getConfigType() err = v.unmarshalReader(reader, kvStore)
err = tempViper.unmarshalReader(reader, tempViper.kvstore) v.kvstore = v.updateRegisteredConfig(kvStore)
v.updateRegisteredConfig(tempViper)
return v.kvstore, err return v.kvstore, err
} }
@ -1999,11 +2000,10 @@ func (v *Viper) watchKeyValueConfigOnChannel() error {
go func(rc <-chan *RemoteResponse) { go func(rc <-chan *RemoteResponse) {
for { for {
b := <-rc b := <-rc
tempViper := New()
tempViper.configType = v.getConfigType()
reader := bytes.NewReader(b.Value) reader := bytes.NewReader(b.Value)
tempViper.unmarshalReader(reader, tempViper.kvstore) kvStore := make(map[string]interface{})
v.updateRegisteredConfig(tempViper) v.unmarshalReader(reader, kvStore)
v.kvstore = v.updateRegisteredConfig(kvStore)
} }
}(respc) }(respc)
return nil return nil