Skip to content

Not able to use non-UTF-8 strings as keys #266

@limbulvetr

Description

@limbulvetr

Hello there. I've been integrating my C++ project into golang. I'm unable to generate decoder and unmarshaler because the project uses one-byte keys in msgpack to shorten the payload.

type ValueSegment struct {
	Offset int    `msg:"\x0b"`
	...
}

According to spec, strings such as "\x0b" are allowed as keys.

tools/imports fails to format the generated code because it contains illegal characters which should have been escaped. The fix is trivial.

--- a/src/github.com/tinylib/msgp/gen/decode.go
+++ b/src/github.com/tinylib/msgp/gen/decode.go
@@ -102,7 +102,7 @@ func (d *decodeGen) structAsMap(s *Struct) {
 	d.p.print("\nswitch msgp.UnsafeString(field) {")
 	for i := range s.Fields {
 		d.ctx.PushString(s.Fields[i].FieldName)
-		d.p.printf("\ncase \"%s\":", s.Fields[i].FieldTag)
+		d.p.printf("\ncase %q:", s.Fields[i].FieldTag)
 		next(d, s.Fields[i].FieldElem)
 		d.ctx.Pop()
 		if !d.p.ok() {

--- a/src/github.com/tinylib/msgp/gen/unmarshal.go
+++ b/src/github.com/tinylib/msgp/gen/unmarshal.go
@@ -105,7 +105,7 @@ func (u *unmarshalGen) mapstruct(s *Struct) {
 		if !u.p.ok() {
 			return
 		}
-		u.p.printf("\ncase \"%s\":", s.Fields[i].FieldTag)
+		u.p.printf("\ncase %q:", s.Fields[i].FieldTag)
 		u.ctx.PushString(s.Fields[i].FieldName)
 		next(u, s.Fields[i].FieldElem)
 		u.ctx.Pop()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions