Fix watch register.

This commit is contained in:
cyprinus 2023-06-04 13:09:57 +08:00
parent 395c6b780d
commit 027007221d

View file

@ -527,24 +527,32 @@ func (v *Viper) updateRegisteredConfig(newConfig map[string]interface{}) (result
} }
// Type check & convert // Type check & convert
if config.Schema != nil {
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 newConfig[key] = oldValue
if config.OnUpdateFailed != nil {
config.OnUpdateFailed(&Event{ config.OnUpdateFailed(&Event{
old: oldValue, old: oldValue,
new: nil, new: nil,
}) })
}
continue continue
} }
} else {
config.Schema = newValue
}
// Validation // Validation
if config.Validator != nil && !config.Validator(config.Schema) { if config.Validator != nil && !config.Validator(config.Schema) {
newConfig[key] = oldValue newConfig[key] = oldValue
if config.OnUpdateFailed != nil {
config.OnUpdateFailed(&Event{ config.OnUpdateFailed(&Event{
old: oldValue, old: oldValue,
new: nil, new: nil,
}) })
}
continue continue
} }