Skip to content

Commit

Permalink
map keys
Browse files Browse the repository at this point in the history
  • Loading branch information
iansuvak committed Aug 31, 2023
1 parent 3052bcf commit b376f86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion gen/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ func (u *unmarshalGen) mapstruct(s *Struct) {
u.p.printf("\n}")

u.p.printf("\nfor %s > 0 {", sz)
u.p.printf("\n%s--; field, bts, err = msgp.ReadMapKeyZC(bts)", sz)
u.p.printf("\n%s--", sz)
u.p.printf("\n if validate {")
u.p.printf("\nfield, bts, err = msgp.ReadMapKeyZCCanonical(bts)")
u.p.printf("\n} else {")
u.p.printf("\nfield, bts, err = msgp.ReadMapKeyZC(bts)")
u.p.printf("\n}")
u.p.wrapErrCheck(u.ctx.ArgsStr())
u.p.print("\nswitch string(field) {")
for i := range s.Fields {
Expand Down
17 changes: 17 additions & 0 deletions msgp/read_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ func ReadMapKeyZC(b []byte) ([]byte, []byte, error) {
return o, x, nil
}

// ReadMapKeyZCCanonical attempts to read a map key
// from 'b' and returns the key bytes and the remaining bytes
// Possible errors:
// - ErrShortBytes (too few bytes)
// - TypeError{} (not a str or bin)
func ReadMapKeyZCCanonical(b []byte) ([]byte, []byte, error) {
o, x, err := ReadStringZCCanonical(b)
if err != nil {
if tperr, ok := err.(TypeError); ok && tperr.Encoded == BinType {
return nil, b, ErrNonCanonical("map key encoded as bin instead of str")
}
return nil, b, err
}
return o, x, nil
}

// ReadArrayHeaderBytes attempts to read
// the array header size off of 'b' and return
// the size and remaining bytes.
Expand Down Expand Up @@ -1691,6 +1707,7 @@ func ReadStringZCCanonical(b []byte) (v []byte, o []byte, err error) {
b = b[5:]

default:
// still doing a go-codec compat check for a more accurate error message but this is definitely non-canonical
// go-codec compat: decode bin types into string
v, o, err = readBytesBytes(b, nil, true, false)
if err != nil {
Expand Down

0 comments on commit b376f86

Please sign in to comment.