mapstructure icon indicating copy to clipboard operation
mapstructure copied to clipboard

Platform-specific behavior when decoding a float value into a uint field

Open leventov opened this issue 4 years ago • 1 comments

https://github.com/mitchellh/mapstructure/blob/4664f9ee512b4dfa71b501c4960d469c205b2cf1/mapstructure.go#L657

Should be val.SetUint(uint64(int64(f))), because https://golang.org/ref/spec#Conversions says:

In all non-constant conversions involving floating-point or complex values, if the result type cannot represent the value the conversion succeeds but the result value is implementation-dependent.

This violates the principle of least astonishment (at least) when trying to convert small negative floats to uint fields. (The way the values are converted are actually different, e. g. on ARM vs. Mac.

leventov avatar Mar 07 '21 22:03 leventov

I'm good with this. Is there a test case that can be written (even if it only passes/fails on certain platforms/architectures) that we can have to prevent regressing this?

mitchellh avatar Apr 20 '22 22:04 mitchellh