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

Problem with multiple msgp:shim variables. #168

Closed
jchv opened this issue Dec 3, 2016 · 1 comment · Fixed by #169
Closed

Problem with multiple msgp:shim variables. #168

jchv opened this issue Dec 3, 2016 · 1 comment · Fixed by #169

Comments

@jchv
Copy link

jchv commented Dec 3, 2016

Consider the following:

//go:generate msgp
//msgp:shim SpecialID as:[]byte using:toBytes/fromBytes

package main

import (
	"bytes"

	"github.com/tinylib/msgp/msgp"
)

type SpecialID string
type TestObj struct{ ID1, ID2 SpecialID }

func toBytes(id SpecialID) []byte   { return []byte(string(id)) }
func fromBytes(id []byte) SpecialID { return SpecialID(string(id)) }

func main() {
	buf := bytes.Buffer{}
	test := TestObj{}

	msgp.Encode(&buf, &TestObj{ID1: "1", ID2: "2"})
	msgp.Decode(&buf, &test)

	println(test.ID1 + ":" + test.ID2)
}

The output should be 1:2, but instead... it's :2.

This seems to be a result of an interesting, but very consistent bug in the generated code:

// MarshalMsg implements msgp.Marshaler
func (z *TestObj) MarshalMsg(b []byte) (o []byte, err error) {
	o = msgp.Require(b, z.Msgsize())
	// map header, size 2
	// string "ID1"
	o = append(o, 0x82, 0xa3, 0x49, 0x44, 0x31)
	o = msgp.AppendBytes(o, toBytes(z.ID2))
	// string "ID2"
	o = append(o, 0xa3, 0x49, 0x44, 0x32)
	o = msgp.AppendBytes(o, toBytes(z.ID2))
	return
}

(Needless to say, when I noticed that IDs were missing, it took me a very long time to even realize something was wrong.)

I haven't narrowed down what is going on, I just figured I'd file a bug report after a few solid hours of trying to figure out where my bug was in the first place :)

@philhofer
Copy link
Member

Ouch, that's a subtle one! Thanks for the bug report. #169 should fix this; let me know if you have any other issues.

Thanks!

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