mitchellh / mapstructure

Go library for decoding generic map values into native Go structures and vice versa.
https://gist.github.com/mitchellh/90029601268e59a29e64e55bab1c5bdc
MIT License
7.93k stars 677 forks source link

Fix Encoding Struct Back to Map Bug (#279) #307

Open vaguecoder opened 2 years ago

vaguecoder commented 2 years ago

Motivation

Issue #279 reported by @kszafran

Changes

Check in decode struct -> map for extra fields encoded with remain tag, and flatten in resultant map. Add corresponding tests:

  1. Unit test to decode struct (with remain-field) to map.
  2. Unit test to check invalid remain-field type check error.
  3. Example test to decode map -> struct -> map.
  4. Benchmark test to decode map -> struct -> map.

Summary

As extra fields are added to the remain-field while decoding map as structure, the reverse should work as well. i.e., while decoding structure as map, the extra fields in remain-field should get flattened in previous level. For code example, refer #279.

Related Issues

Closes #279

sagikazarmark commented 11 months ago

Pulled in at https://github.com/go-viper/mapstructure/pull/8