Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ func (d *Decoder) UsePreallocateValues(on bool) {
}
}

// UseUIntStructKeys enables support for decoding uint struct tag keys to their corresponding struct field.
func (d *Decoder) UseUIntStructKeys(on bool) {
if on {
d.flags |= useUIntStructKeysFlag
} else {
d.flags &= ^useUIntStructKeysFlag
}
}

// DisableAllocLimit enables fully allocating slices/maps when the size is known
func (d *Decoder) DisableAllocLimit(on bool) {
if on {
Expand Down
28 changes: 25 additions & 3 deletions decode_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"reflect"
"strconv"

"github.com/vmihailenco/msgpack/v5/msgpcode"
)
Expand Down Expand Up @@ -332,9 +333,30 @@ func (d *Decoder) decodeStruct(v reflect.Value, n int) error {

fields := structs.Fields(v.Type(), d.structTag)
for i := 0; i < n; i++ {
name, err := d.decodeStringTemp()
if err != nil {
return err
var name string
var err error

// Try to decode the key as a uint if useUIntStructKeysFlag is set
if d.flags&useUIntStructKeysFlag != 0 {
code, err := d.PeekCode()
if err != nil {
return err
}
if msgpcode.IsUint(code) {
intKey, err := d.DecodeUint()
if err != nil {
return err
}
name = strconv.FormatUint(uint64(intKey), 10)
}
}

// if useUIntStructKeysFlag is not set or if the current key is not a uint, fallback to a string key
if name == "" {
name, err = d.decodeStringTemp()
if err != nil {
return err
}
}

if f := fields.Map[name]; f != nil {
Expand Down
10 changes: 10 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
useCompactFloatsFlag
useInternedStringsFlag
omitEmptyFlag
useUIntStructKeysFlag
)

type writer interface {
Expand Down Expand Up @@ -196,6 +197,15 @@ func (e *Encoder) UseInternedStrings(on bool) {
}
}

// UseUIntStructKeys causes the Encoder to encode struct fields that have a valid uint tag key as uints.
func (e *Encoder) UseUIntStructKeys(on bool) {
if on {
e.flags |= useUIntStructKeysFlag
} else {
e.flags &= ^useUIntStructKeysFlag
}
}

func (e *Encoder) Encode(v interface{}) error {
switch v := v.(type) {
case nil:
Expand Down
19 changes: 17 additions & 2 deletions encode_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math"
"reflect"
"sort"
"strconv"

"github.com/vmihailenco/msgpack/v5/msgpcode"
)
Expand Down Expand Up @@ -201,8 +202,22 @@ func encodeStructValue(e *Encoder, strct reflect.Value) error {
}

for _, f := range fields {
if err := e.EncodeString(f.name); err != nil {
return err
uintKeyEncoded := false
if e.flags&useUIntStructKeysFlag != 0 {
// Try to encode the key as a uint if useUIntStructKeysFlag is set
uintKey, err := strconv.ParseUint(f.name, 10, 0)
if err == nil {
if err := e.EncodeUint(uintKey); err != nil {
return err
}
uintKeyEncoded = true
}
}
// If useUIntStructKeysFlag is not set or if the key was not encoded as a uint, encode it as a string
if !uintKeyEncoded {
if err := e.EncodeString(f.name); err != nil {
return err
}
}
if err := f.EncodeValue(e, strct); err != nil {
return err
Expand Down
35 changes: 35 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,41 @@ func ExampleMarshal() {
// Output: bar
}

func ExampleMarshal_intKeys() {
type Item struct {
Foo string `msgpack:"100"`
Bar string `msgpack:"200"`
Baz string `msgpack:"not_int_key"`
}

buf := new(bytes.Buffer)
enc := msgpack.NewEncoder(buf)
enc.UseUIntStructKeys(true)
err := enc.Encode(&Item{
Foo: "foo",
Bar: "bar",
Baz: "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)
}
fmt.Println(item.Foo)
fmt.Println(item.Bar)
fmt.Println(item.Baz)
// Output:
// foo
// bar
// baz
}

func ExampleMarshal_mapStringInterface() {
in := map[string]interface{}{"foo": 1, "hello": "world"}
b, err := msgpack.Marshal(in)
Expand Down
4 changes: 4 additions & 0 deletions msgpcode/msgpcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ func IsFixedExt(c byte) bool {
func IsExt(c byte) bool {
return IsFixedExt(c) || c == Ext8 || c == Ext16 || c == Ext32
}

func IsUint(c byte) bool {
return c <= PosFixedNumHigh || c == Uint8 || c == Uint16 || c == Uint32 || c == Uint64
}