msgpack
msgpack copied to clipboard
Can encode a map[int][whatever type], but cannot decode into the any type. Get invalid code=1 decoding string/bytes length, only in v5, works in v4
Was doing some decoding on a weird message that requires using any
. Ran into an issue where I was able to encode the message but not decode it. Tracking it down, I was able to isolate it into code that I have running on the go playground:
https://go.dev/play/p/GbriQkE8RqN
I've then been able to isolate it further:
https://go.dev/play/p/FAhH3EckyEi
The essence is, if you encode:
map[int]string{"hello"}
You cannot decode out to the any
type:
var out any
if err := msgpack.Unmarshal(b, &out); err != nil {
panic(err)
}
This results in:
panic: msgpack: invalid code=1 decoding string/bytes length
This will work if you decode to a non-any
type (such as Msg
). So it is some type of interaction there on the encoder or decoder. It will also work if you change the key type to a non-integer type, like map[string]string
.
If you decode into a concrete type, then this works as expected.
Expected Behavior
If you switch to v4 instead, you can see the correct behavior.
Current Behavior
It returns an error: invalid code=1 decoding string/bytes length
Possible Solution
I'm sure there is something in the decoder that isn't reflecting right or something in the encoder not encoding correctly.
Steps to Reproduce
https://go.dev/play/p/FAhH3EckyEi
Context (Environment)
Trying to test an encoding on some msgpack being sent on a net.Conn .