Fix watch register.

This commit is contained in:
cyprinus 2023-06-04 12:09:51 +08:00
parent eebc2f0d0b
commit 395c6b780d

View file

@ -508,7 +508,9 @@ 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(newConfig map[string]interface{}) map[string]interface{} { func (v *Viper) updateRegisteredConfig(newConfig map[string]interface{}) (result map[string]interface{}) {
result = make(map[string]interface{})
for key, config := range v.registered { for key, config := range v.registered {
oldValue := v.Get(key) oldValue := v.Get(key)
newValue := newConfig[key] newValue := newConfig[key]
@ -537,7 +539,7 @@ func (v *Viper) updateRegisteredConfig(newConfig map[string]interface{}) map[str
} }
// Validation // Validation
if !config.Validator(config.Schema) { if config.Validator != nil && !config.Validator(config.Schema) {
newConfig[key] = oldValue newConfig[key] = oldValue
config.OnUpdateFailed(&Event{ config.OnUpdateFailed(&Event{
old: oldValue, old: oldValue,
@ -547,7 +549,7 @@ func (v *Viper) updateRegisteredConfig(newConfig map[string]interface{}) map[str
} }
// Success // Success
newConfig[key] = config.Schema result[key] = config.Schema
if config.OnUpdate != nil { if config.OnUpdate != nil {
config.OnUpdate(&Event{ config.OnUpdate(&Event{
new: config.Schema, new: config.Schema,
@ -555,7 +557,7 @@ func (v *Viper) updateRegisteredConfig(newConfig map[string]interface{}) map[str
}) })
} }
} }
return newConfig return result
} }
// SetConfigFile explicitly defines the path, name and extension of the config file. // SetConfigFile explicitly defines the path, name and extension of the config file.