Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 #372

Open
element-of-surprise opened this issue Apr 20, 2024 · 0 comments

Comments

@element-of-surprise
Copy link

element-of-surprise commented Apr 20, 2024

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 .

@element-of-surprise element-of-surprise changed the title Can encode a struct{map[int]Struct}, but cannot decode. Get invalid code=1 decoding string/bytes length, only in v5, works in v4 Can encode a map[int][whatever type], but cannot decode in the any type. Get invalid code=1 decoding string/bytes length, only in v5, works in v4 Apr 21, 2024
@element-of-surprise element-of-surprise changed the title Can encode a map[int][whatever type], but cannot decode in the any type. Get invalid code=1 decoding string/bytes length, only in v5, works in v4 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 Apr 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant