Skip to content

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.

1 participant