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

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

Closed
limbulvetr opened this issue Mar 2, 2020 · 1 comment · Fixed by #379
Closed

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

limbulvetr opened this issue Mar 2, 2020 · 1 comment · Fixed by #379

Comments

@limbulvetr
Copy link

limbulvetr commented Mar 2, 2020

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()
@philhofer
Copy link
Member

philhofer commented Mar 2, 2020 via email

klauspost added a commit to klauspost/msgp that referenced this issue Oct 29, 2024
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 a pull request may close this issue.

2 participants