Skip to content

Instantly share code, notes, and snippets.

@ColtonProvias
Created September 25, 2017 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ColtonProvias/c5a020bb8ba42942689d114488568354 to your computer and use it in GitHub Desktop.
Save ColtonProvias/c5a020bb8ba42942689d114488568354 to your computer and use it in GitHub Desktop.
msgp generation bug
//go:generate msgp
//msgp:tuple Sample
//msgp:shim uuid.UUID as:[]byte using:uuidToBytes/bytesToUUID
package serialization
import (
"github.com/satori/go.uuid"
)
type Sample struct {
ID uuid.UUID
SomeValue bool
}
func uuidToBytes(id uuid.UUID) []byte {
return id.Bytes()
}
func bytesToUUID(in []byte) uuid.UUID {
return uuid.FromBytesOrNil(in)
}
package serialization
// NOTE: THIS FILE WAS PRODUCED BY THE
// MSGP CODE GENERATION TOOL (github.com/tinylib/msgp)
// DO NOT EDIT
import (
"github.com/tinylib/msgp/msgp"
)
// DecodeMsg implements msgp.Decodable
func (z *Sample) DecodeMsg(dc *msgp.Reader) (err error) {
var zxvk uint32
zxvk, err = dc.ReadArrayHeader()
if err != nil {
return
}
if zxvk != 2 {
err = msgp.ArrayError{Wanted: 2, Got: zxvk}
return
}
{
var zbzg []byte
zbzg, err = dc.ReadBytes([]byte(z.ID))
if err != nil {
return
}
z.ID = bytesToUUID(zbzg)
}
z.SomeValue, err = dc.ReadBool()
if err != nil {
return
}
return
}
// EncodeMsg implements msgp.Encodable
func (z *Sample) EncodeMsg(en *msgp.Writer) (err error) {
// array header, size 2
err = en.Append(0x92)
if err != nil {
return err
}
err = en.WriteBytes(uuidToBytes(z.ID))
if err != nil {
return
}
err = en.WriteBool(z.SomeValue)
if err != nil {
return
}
return
}
// MarshalMsg implements msgp.Marshaler
func (z *Sample) MarshalMsg(b []byte) (o []byte, err error) {
o = msgp.Require(b, z.Msgsize())
// array header, size 2
o = append(o, 0x92)
o = msgp.AppendBytes(o, uuidToBytes(z.ID))
o = msgp.AppendBool(o, z.SomeValue)
return
}
// UnmarshalMsg implements msgp.Unmarshaler
func (z *Sample) UnmarshalMsg(bts []byte) (o []byte, err error) {
var zbai uint32
zbai, bts, err = msgp.ReadArrayHeaderBytes(bts)
if err != nil {
return
}
if zbai != 2 {
err = msgp.ArrayError{Wanted: 2, Got: zbai}
return
}
{
var zcmr []byte
zcmr, bts, err = msgp.ReadBytesBytes(bts, uuidToBytes(z.ID))
if err != nil {
return
}
z.ID = bytesToUUID(zcmr)
}
z.SomeValue, bts, err = msgp.ReadBoolBytes(bts)
if err != nil {
return
}
o = bts
return
}
// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
func (z *Sample) Msgsize() (s int) {
s = 1 + msgp.BytesPrefixSize + len(uuidToBytes(z.ID)) + msgp.BoolSize
return
}
package serialization
// NOTE: THIS FILE WAS PRODUCED BY THE
// MSGP CODE GENERATION TOOL (github.com/tinylib/msgp)
// DO NOT EDIT
import (
"bytes"
"testing"
"github.com/tinylib/msgp/msgp"
)
func TestMarshalUnmarshalSample(t *testing.T) {
v := Sample{}
bts, err := v.MarshalMsg(nil)
if err != nil {
t.Fatal(err)
}
left, err := v.UnmarshalMsg(bts)
if err != nil {
t.Fatal(err)
}
if len(left) > 0 {
t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left)
}
left, err = msgp.Skip(bts)
if err != nil {
t.Fatal(err)
}
if len(left) > 0 {
t.Errorf("%d bytes left over after Skip(): %q", len(left), left)
}
}
func BenchmarkMarshalMsgSample(b *testing.B) {
v := Sample{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
v.MarshalMsg(nil)
}
}
func BenchmarkAppendMsgSample(b *testing.B) {
v := Sample{}
bts := make([]byte, 0, v.Msgsize())
bts, _ = v.MarshalMsg(bts[0:0])
b.SetBytes(int64(len(bts)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
bts, _ = v.MarshalMsg(bts[0:0])
}
}
func BenchmarkUnmarshalSample(b *testing.B) {
v := Sample{}
bts, _ := v.MarshalMsg(nil)
b.ReportAllocs()
b.SetBytes(int64(len(bts)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := v.UnmarshalMsg(bts)
if err != nil {
b.Fatal(err)
}
}
}
func TestEncodeDecodeSample(t *testing.T) {
v := Sample{}
var buf bytes.Buffer
msgp.Encode(&buf, &v)
m := v.Msgsize()
if buf.Len() > m {
t.Logf("WARNING: Msgsize() for %v is inaccurate", v)
}
vn := Sample{}
err := msgp.Decode(&buf, &vn)
if err != nil {
t.Error(err)
}
buf.Reset()
msgp.Encode(&buf, &v)
err = msgp.NewReader(&buf).Skip()
if err != nil {
t.Error(err)
}
}
func BenchmarkEncodeSample(b *testing.B) {
v := Sample{}
var buf bytes.Buffer
msgp.Encode(&buf, &v)
b.SetBytes(int64(buf.Len()))
en := msgp.NewWriter(msgp.Nowhere)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
v.EncodeMsg(en)
}
en.Flush()
}
func BenchmarkDecodeSample(b *testing.B) {
v := Sample{}
var buf bytes.Buffer
msgp.Encode(&buf, &v)
b.SetBytes(int64(buf.Len()))
rd := msgp.NewEndlessReader(buf.Bytes(), b)
dc := msgp.NewReader(rd)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := v.DecodeMsg(dc)
if err != nil {
b.Fatal(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment