Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
creasty committed Apr 11, 2022
1 parent f8acd9d commit 53f3754
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,26 +171,22 @@ func setField(field reflect.Value, defaultVal string) error {
case reflect.Map:
for _, e := range field.MapKeys() {
var v = field.MapIndex(e)
var baseVal interface{}

switch v.Kind() {
case reflect.Ptr:
baseVal = v.Elem().Interface()
default:
baseVal = v.Interface()
}
ref := reflect.New(reflect.TypeOf(baseVal))
ref.Elem().Set(reflect.ValueOf(baseVal))
err := Set(ref.Interface())
if err == nil || err == errInvalidType {
var newVal reflect.Value
if v.Kind() == reflect.Ptr {
newVal = reflect.ValueOf(ref.Interface())
} else {
newVal = reflect.ValueOf(ref.Elem().Interface())
switch v.Elem().Kind() {
case reflect.Struct, reflect.Slice, reflect.Map:
if err := setField(v.Elem(), ""); err != nil {
return err
}
}
field.SetMapIndex(e, newVal)
} else {
return err
case reflect.Struct, reflect.Slice, reflect.Map:
ref := reflect.New(v.Type())
ref.Elem().Set(v)
if err := setField(ref.Elem(), ""); err != nil {
return err
}
field.SetMapIndex(e, ref.Elem().Convert(v.Type()))
}
}
}
Expand Down

0 comments on commit 53f3754

Please sign in to comment.