amazon-ion / ion-go

A Go implementation of Amazon Ion.
https://amazon-ion.github.io/ion-docs/
Apache License 2.0
175 stars 31 forks source link

Fix Unmarshal for map of slices #172

Closed bancek closed 3 years ago

bancek commented 3 years ago

If the map value is a pointer type (e.g. []string), Unmarshal produces invalid data because it overrides the values from previous keys.

For input {headers:{key1:[val1],key2:[val2,val3],key3:[val4]}} Unmarshal produces

&ion.request{Headers:map[string][]string{"key1":[]string{"val4"}, "key2":[]string{"val4", "val3"}, "key3":[]string{"val4"}}}

instead of

&ion.request{Headers:map[string][]string{"key1":[]string{"val1"}, "key2":[]string{"val2", "val3"}, "key3":[]string{"val4"}}}

This change also fixes another bug from v1.0.1 where fieldName.Text becomes nil after calling d.decodeTo(subv) because fieldName is a pointer and the value changes during reading other fields and structures. This bug has been fixed in in v1.1.0 but it might be safer to save the field name before calling decodeTo.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.