Skip to content

Commit

Permalink
fix(config): problem that always append values to default list (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzz2017 authored Jun 9, 2023
1 parent 9c632aa commit e1d0d8a
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func ParamParser(to reflect.Value, section *config_parser.Section, ignoreType []
case reflect.Slice:
// Field is not interface{}, we can decode.
values := strings.Split(itemVal.Val, ",")
if len(values) > 0 && !field.Set {
// Clear default value to avoid appending values to it.
field.Val.Set(reflect.Zero(field.Val.Type()))
}
for _, value := range values {
vPointerNew := reflect.New(field.Val.Type().Elem())
if !common.FuzzyDecode(vPointerNew.Interface(), value) {
Expand Down Expand Up @@ -186,19 +190,19 @@ func SectionParser(to reflect.Value, section *config_parser.Section) error {
case reflect.Struct:
// "to" is a section list (sections in section).
/**
to {
field1 {
...
}
field2 {
...
}
}
should be parsed to:
to []struct {
Name string `mapstructure: "_"`
...
}
to {
field1 {
...
}
field2 {
...
}
}
should be parsed to:
to []struct {
Name string `mapstructure: "_"`
...
}
*/
// The struct should contain Name.
nameStructField, ok := elemType.FieldByName("Name")
Expand Down

0 comments on commit e1d0d8a

Please sign in to comment.