You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
msgpack successfully unmarshals a type implementing both encoding.TextUnmarshaler and encoding.BinaryUnmashaler using the former interface when the data is of a str format, and is stored in text representation.
Current Behavior
msgpack fails to unmarshal the data in aforementioned conditions, because it's using the latter interface which does not expect text data.
This is because the library prefers BinaryUnmarshaler unconditionally:
Choose the preferred unmarshalling interface based on MessagePack format (MessagePack has distinct str and bin formats), preferring TextUnmarshaler when it's an str.
Steps to Reproduce
This example might look arbitrary out of context, but this situation easily arises when round-tripping the data through JSON and/or other libraries (especially in dynamically typed programming languages).
This library currently always prefers to use encoding.BinaryUnmashaler
when it's implemented by the target type.
This might lead to problems when the type also has a text
representation implemented as encoding.TextUnmarshaler.
Consider netip.Addr from stdlib as example, which implements both.
This library won't be able decode MessagePack containing a string
"192.0.2.1" into *netip.Addr because it will attempt to use
encoding.BinaryUnmashaler which doesn't expect text representation.
Fortunately, MessagePack has distinct string and binary types, so we can
check the source data type before choosing the interface to use.
This commit changes the behaviour of decoder as follows. When
1) target Go data type implements both BinaryUnmashaler and
TextUnmarshaler
2) source MessagePack data type is a string
TextUnmarshaler will be preferred over BinaryUnmashaler.
This feature is gated behind a Decoder option, because it is potentially
backward-incompatible change.
See vmihailenco#370
Expected Behavior
msgpack
successfully unmarshals a type implementing bothencoding.TextUnmarshaler
andencoding.BinaryUnmashaler
using the former interface when the data is of astr
format, and is stored in text representation.Current Behavior
msgpack
fails to unmarshal the data in aforementioned conditions, because it's using the latter interface which does not expect text data.This is because the library prefers
BinaryUnmarshaler
unconditionally:msgpack/decode_value.go
Lines 73 to 78 in 19c91df
Possible Solution
Choose the preferred unmarshalling interface based on MessagePack format (MessagePack has distinct
str
andbin
formats), preferringTextUnmarshaler
when it's anstr
.Steps to Reproduce
This example might look arbitrary out of context, but this situation easily arises when round-tripping the data through JSON and/or other libraries (especially in dynamically typed programming languages).
https://play.golang.com/p/3WFWbPVKDkp
The text was updated successfully, but these errors were encountered: