Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 173c4de

Browse files
author
Pavel Ivanov
committed
fix: DecodeHook support for decoding map from struct
1 parent 4ec29e7 commit 173c4de

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

mapstructure.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,6 @@ func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val re
673673
// Next get the actual value of this field and verify it is assignable
674674
// to the map value.
675675
v := dataVal.Field(i)
676-
if !v.Type().AssignableTo(valMap.Type().Elem()) {
677-
return fmt.Errorf("cannot assign type '%s' to map value field of type '%s'", v.Type(), valMap.Type().Elem())
678-
}
679676

680677
tagValue := f.Tag.Get(d.config.TagName)
681678
tagParts := strings.Split(tagValue, ",")
@@ -727,7 +724,13 @@ func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val re
727724
}
728725

729726
default:
730-
valMap.SetMapIndex(reflect.ValueOf(keyName), v)
727+
currentVal := reflect.Indirect(reflect.New(valMap.Type().Elem()))
728+
err := d.decode(keyName, v.Interface(), currentVal)
729+
if err != nil {
730+
return err
731+
}
732+
733+
valMap.SetMapIndex(reflect.ValueOf(keyName), currentVal)
731734
}
732735
}
733736

0 commit comments

Comments
 (0)