From 395c6b780dbd975f94338936292fd3b78acd94f5 Mon Sep 17 00:00:00 2001 From: cyprinus Date: Sun, 4 Jun 2023 12:09:51 +0800 Subject: [PATCH] Fix watch register. --- viper.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/viper.go b/viper.go index 901e638..32a6b03 100644 --- a/viper.go +++ b/viper.go @@ -508,7 +508,9 @@ func (v *Viper) WatchConfig() { } // 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 { oldValue := v.Get(key) newValue := newConfig[key] @@ -537,7 +539,7 @@ func (v *Viper) updateRegisteredConfig(newConfig map[string]interface{}) map[str } // Validation - if !config.Validator(config.Schema) { + if config.Validator != nil && !config.Validator(config.Schema) { newConfig[key] = oldValue config.OnUpdateFailed(&Event{ old: oldValue, @@ -547,7 +549,7 @@ func (v *Viper) updateRegisteredConfig(newConfig map[string]interface{}) map[str } // Success - newConfig[key] = config.Schema + result[key] = config.Schema if config.OnUpdate != nil { config.OnUpdate(&Event{ 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.