chore: fix decode default value to interface

This commit is contained in:
mzz2017 2023-03-03 11:09:51 +08:00
parent 94f3ba4ee6
commit 6359215cb4

View File

@ -67,7 +67,13 @@ func ParamParser(to reflect.Value, section *config_parser.Section, ignoreType []
// Fill in default value before parsing section. // Fill in default value before parsing section.
defaultValue, ok := structField.Tag.Lookup("default") defaultValue, ok := structField.Tag.Lookup("default")
if ok { if ok {
if !common.FuzzyDecode(field.Addr().Interface(), defaultValue) { // Can we assign?
if field.Kind() == reflect.Interface ||
field.Type() == reflect.TypeOf(defaultValue) {
field.Set(reflect.ValueOf(defaultValue))
// Can we fuzzy decode?
} else if !common.FuzzyDecode(field.Addr().Interface(), defaultValue) {
return fmt.Errorf(`failed to decode default value of "%v"`, structField.Name) return fmt.Errorf(`failed to decode default value of "%v"`, structField.Name)
} }
} }