-
Notifications
You must be signed in to change notification settings - Fork 681
Decode does not zero the destination variable before decoding the value into it. #74
Comments
Yes this is done on purpose so that you can call Decode multiple times to "build up" a result. Arrays/slices in particular this may not make sense though. |
Thanks. I will remember to zero them before the new iteration. |
I do think the slice/array should be zeroed though (hence me not closing this issue). I'm thinking about it more though. |
Yes, I agree. In the case some input is helpful: I think currently it replaces the existing elements in the slice with new elements. This works as long as the new slice is the same length or bigger than the existing slice. This, however, results in a problem when the new slice is smaller than the existing slice, since some of (but not all) previous elements are left in the slice. |
Is there a solution/workaround yet? I'd need slices to be zeroed before values are written into (but only if this field should be updated/values are set). |
Any more thoughts on this? In my opinion probably everyone expects an array/slice to be zeroed before update... Partial update of an array seems to be a very rare edge case. |
I agree that arrays/slices should be fully replaced when decoding... I think this would be expected behavior for most users, which could make them unknowingly introduce bugs into their systems (as I did). What are some arguments for keeping things the way they are? I'm willing to put up a PR for this. |
mapstructure library doesn't override full slice during unmarshalling. Origin issue: mitchellh/mapstructure#74 (comment) To address this we zeroes every slice before unmarshalling unless user provided slice is nil.
mapstructure library doesn't override full slice during unmarshalling. Origin issue: mitchellh/mapstructure#74 (comment) To address this we zeroes every slice before unmarshalling unless user provided slice is nil.
mapstructure library doesn't override full slice during unmarshalling. Origin issue: mitchellh/mapstructure#74 (comment) To address this we zeroes every slice before unmarshalling unless user provided slice is nil.
mapstructure library doesn't override full slice during unmarshalling. Origin issue: mitchellh/mapstructure#74 (comment) To address this we zeroes every slice before unmarshalling unless user provided slice is nil.
mapstructure library doesn't override full slice during unmarshalling. Origin issue: mitchellh/mapstructure#74 (comment) To address this we empty every slice before unmarshalling unless user provided slice is nil.
😂 I find myself falling into the same hole as well. We rely on the feature to implement a bunch of PATCH APIs and they worked nicely for years until I tried to reduce one of the fields with the I wonder what is the use case of "Merging List"? How about
|
I realized today that
Decode
does not zero the destination variable before decoding the value into it. Is this expected; or is this a bug?Example:
In the above example
updatedDec
is not zeroed across iterations.updatedDec
contains an array. If that array has more elements than the same array in the new value, then some of the earlier values remain in there after mapstrucure.Decode.The text was updated successfully, but these errors were encountered: