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

Allow encoding/decoding uint struct tag keys #361

Open
wants to merge 1 commit into
base: v5
Choose a base branch
from

Conversation

pierre-borckmans
Copy link

@pierre-borckmans pierre-borckmans commented Oct 27, 2023

This PR adds support for encoding/decoding structures like:

type Item struct {
	Field100 string `msgpack:"100"`
	Field200 string `msgpack:"200"`
	Foo string `msgpack:"not_int_key"`
}

where some of the msgpack keys are actually of type uint.

A new flag is exposed to control this behaviour:

buf := new(bytes.Buffer)
enc := msgpack.NewEncoder(buf)
enc.UseUIntStructKeys(true)
err := enc.Encode(&Item{
	Field100: "foo",
	Field200: "bar",
	Foo: "baz",
})
if err != nil {
	panic(err)
}

var Item item
dec := msgpack.NewDecoder(buf)
dec.UseUIntStructKeys(true)
err = dec.Decode(&item)
if err != nil {
	panic(err)
}

The reason I need this is because I have a device sending msgpack objects over usb that are encoded in this way (with uint keys).

I added an example to cover this use case.
I tried to add docs but couldn't find where the sources for https://msgpack.uptrace.dev/guide are.

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

Successfully merging this pull request may close these issues.

None yet

1 participant