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

Commit b5c931f

Browse files
author
Pavel Ivanov
committed
fix: DecodeHook support for decoding map from struct
1 parent 545fefd commit b5c931f

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
@@ -918,9 +918,6 @@ func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val re
918918
// Next get the actual value of this field and verify it is assignable
919919
// to the map value.
920920
v := dataVal.Field(i)
921-
if !v.Type().AssignableTo(valMap.Type().Elem()) {
922-
return fmt.Errorf("cannot assign type '%s' to map value field of type '%s'", v.Type(), valMap.Type().Elem())
923-
}
924921

925922
tagValue := f.Tag.Get(d.config.TagName)
926923
keyName := f.Name
@@ -1004,7 +1001,13 @@ func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val re
10041001
}
10051002

10061003
default:
1007-
valMap.SetMapIndex(reflect.ValueOf(keyName), v)
1004+
currentVal := reflect.Indirect(reflect.New(valMap.Type().Elem()))
1005+
err := d.decode(keyName, v.Interface(), currentVal)
1006+
if err != nil {
1007+
return err
1008+
}
1009+
1010+
valMap.SetMapIndex(reflect.ValueOf(keyName), currentVal)
10081011
}
10091012
}
10101013

0 commit comments

Comments
 (0)