From 737801e6935d23b649aaa07b14dbadd14a2a7cd3 Mon Sep 17 00:00:00 2001 From: saji Date: Sat, 20 May 2023 14:53:49 -0500 Subject: [PATCH] add can frame messagepack --- frame.go | 1 + frame_gen.go | 456 ++++++++++++++++++++++++++++++++++++++++++++++ frame_gen_test.go | 349 +++++++++++++++++++++++++++++++++++ frame_kind.go | 4 +- 4 files changed, 808 insertions(+), 2 deletions(-) create mode 100644 frame_gen.go create mode 100644 frame_gen_test.go diff --git a/frame.go b/frame.go index 71a845b..58a94cd 100644 --- a/frame.go +++ b/frame.go @@ -20,6 +20,7 @@ type Frame struct { Kind Kind } +//go:generate msgp type CANFrame interface { Id() uint32 Data() []byte diff --git a/frame_gen.go b/frame_gen.go new file mode 100644 index 0000000..26e20ad --- /dev/null +++ b/frame_gen.go @@ -0,0 +1,456 @@ +package gotelem + +// Code generated by github.com/tinylib/msgp DO NOT EDIT. + +import ( + "github.com/tinylib/msgp/msgp" +) + +// DecodeMsg implements msgp.Decodable +func (z *CanFilter) DecodeMsg(dc *msgp.Reader) (err error) { + var field []byte + _ = field + var zb0001 uint32 + zb0001, err = dc.ReadMapHeader() + if err != nil { + err = msgp.WrapError(err) + return + } + for zb0001 > 0 { + zb0001-- + field, err = dc.ReadMapKeyPtr() + if err != nil { + err = msgp.WrapError(err) + return + } + switch msgp.UnsafeString(field) { + case "Id": + z.Id, err = dc.ReadUint32() + if err != nil { + err = msgp.WrapError(err, "Id") + return + } + case "Mask": + z.Mask, err = dc.ReadUint32() + if err != nil { + err = msgp.WrapError(err, "Mask") + return + } + case "Inverted": + z.Inverted, err = dc.ReadBool() + if err != nil { + err = msgp.WrapError(err, "Inverted") + return + } + default: + err = dc.Skip() + if err != nil { + err = msgp.WrapError(err) + return + } + } + } + return +} + +// EncodeMsg implements msgp.Encodable +func (z CanFilter) EncodeMsg(en *msgp.Writer) (err error) { + // map header, size 3 + // write "Id" + err = en.Append(0x83, 0xa2, 0x49, 0x64) + if err != nil { + return + } + err = en.WriteUint32(z.Id) + if err != nil { + err = msgp.WrapError(err, "Id") + return + } + // write "Mask" + err = en.Append(0xa4, 0x4d, 0x61, 0x73, 0x6b) + if err != nil { + return + } + err = en.WriteUint32(z.Mask) + if err != nil { + err = msgp.WrapError(err, "Mask") + return + } + // write "Inverted" + err = en.Append(0xa8, 0x49, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x65, 0x64) + if err != nil { + return + } + err = en.WriteBool(z.Inverted) + if err != nil { + err = msgp.WrapError(err, "Inverted") + return + } + return +} + +// MarshalMsg implements msgp.Marshaler +func (z CanFilter) MarshalMsg(b []byte) (o []byte, err error) { + o = msgp.Require(b, z.Msgsize()) + // map header, size 3 + // string "Id" + o = append(o, 0x83, 0xa2, 0x49, 0x64) + o = msgp.AppendUint32(o, z.Id) + // string "Mask" + o = append(o, 0xa4, 0x4d, 0x61, 0x73, 0x6b) + o = msgp.AppendUint32(o, z.Mask) + // string "Inverted" + o = append(o, 0xa8, 0x49, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x65, 0x64) + o = msgp.AppendBool(o, z.Inverted) + return +} + +// UnmarshalMsg implements msgp.Unmarshaler +func (z *CanFilter) UnmarshalMsg(bts []byte) (o []byte, err error) { + var field []byte + _ = field + var zb0001 uint32 + zb0001, bts, err = msgp.ReadMapHeaderBytes(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + for zb0001 > 0 { + zb0001-- + field, bts, err = msgp.ReadMapKeyZC(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + switch msgp.UnsafeString(field) { + case "Id": + z.Id, bts, err = msgp.ReadUint32Bytes(bts) + if err != nil { + err = msgp.WrapError(err, "Id") + return + } + case "Mask": + z.Mask, bts, err = msgp.ReadUint32Bytes(bts) + if err != nil { + err = msgp.WrapError(err, "Mask") + return + } + case "Inverted": + z.Inverted, bts, err = msgp.ReadBoolBytes(bts) + if err != nil { + err = msgp.WrapError(err, "Inverted") + return + } + default: + bts, err = msgp.Skip(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + } + } + o = bts + return +} + +// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message +func (z CanFilter) Msgsize() (s int) { + s = 1 + 3 + msgp.Uint32Size + 5 + msgp.Uint32Size + 9 + msgp.BoolSize + return +} + +// DecodeMsg implements msgp.Decodable +func (z *CanWriter) DecodeMsg(dc *msgp.Reader) (err error) { + var field []byte + _ = field + var zb0001 uint32 + zb0001, err = dc.ReadMapHeader() + if err != nil { + err = msgp.WrapError(err) + return + } + for zb0001 > 0 { + zb0001-- + field, err = dc.ReadMapKeyPtr() + if err != nil { + err = msgp.WrapError(err) + return + } + switch msgp.UnsafeString(field) { + default: + err = dc.Skip() + if err != nil { + err = msgp.WrapError(err) + return + } + } + } + return +} + +// EncodeMsg implements msgp.Encodable +func (z CanWriter) EncodeMsg(en *msgp.Writer) (err error) { + // map header, size 0 + err = en.Append(0x80) + if err != nil { + return + } + return +} + +// MarshalMsg implements msgp.Marshaler +func (z CanWriter) MarshalMsg(b []byte) (o []byte, err error) { + o = msgp.Require(b, z.Msgsize()) + // map header, size 0 + o = append(o, 0x80) + return +} + +// UnmarshalMsg implements msgp.Unmarshaler +func (z *CanWriter) UnmarshalMsg(bts []byte) (o []byte, err error) { + var field []byte + _ = field + var zb0001 uint32 + zb0001, bts, err = msgp.ReadMapHeaderBytes(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + for zb0001 > 0 { + zb0001-- + field, bts, err = msgp.ReadMapKeyZC(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + switch msgp.UnsafeString(field) { + default: + bts, err = msgp.Skip(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + } + } + o = bts + return +} + +// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message +func (z CanWriter) Msgsize() (s int) { + s = 1 + return +} + +// DecodeMsg implements msgp.Decodable +func (z *Frame) DecodeMsg(dc *msgp.Reader) (err error) { + var field []byte + _ = field + var zb0001 uint32 + zb0001, err = dc.ReadMapHeader() + if err != nil { + err = msgp.WrapError(err) + return + } + for zb0001 > 0 { + zb0001-- + field, err = dc.ReadMapKeyPtr() + if err != nil { + err = msgp.WrapError(err) + return + } + switch msgp.UnsafeString(field) { + case "Id": + z.Id, err = dc.ReadUint32() + if err != nil { + err = msgp.WrapError(err, "Id") + return + } + case "Data": + z.Data, err = dc.ReadBytes(z.Data) + if err != nil { + err = msgp.WrapError(err, "Data") + return + } + case "Kind": + { + var zb0002 uint8 + zb0002, err = dc.ReadUint8() + if err != nil { + err = msgp.WrapError(err, "Kind") + return + } + z.Kind = Kind(zb0002) + } + default: + err = dc.Skip() + if err != nil { + err = msgp.WrapError(err) + return + } + } + } + return +} + +// EncodeMsg implements msgp.Encodable +func (z *Frame) EncodeMsg(en *msgp.Writer) (err error) { + // map header, size 3 + // write "Id" + err = en.Append(0x83, 0xa2, 0x49, 0x64) + if err != nil { + return + } + err = en.WriteUint32(z.Id) + if err != nil { + err = msgp.WrapError(err, "Id") + return + } + // write "Data" + err = en.Append(0xa4, 0x44, 0x61, 0x74, 0x61) + if err != nil { + return + } + err = en.WriteBytes(z.Data) + if err != nil { + err = msgp.WrapError(err, "Data") + return + } + // write "Kind" + err = en.Append(0xa4, 0x4b, 0x69, 0x6e, 0x64) + if err != nil { + return + } + err = en.WriteUint8(uint8(z.Kind)) + if err != nil { + err = msgp.WrapError(err, "Kind") + return + } + return +} + +// MarshalMsg implements msgp.Marshaler +func (z *Frame) MarshalMsg(b []byte) (o []byte, err error) { + o = msgp.Require(b, z.Msgsize()) + // map header, size 3 + // string "Id" + o = append(o, 0x83, 0xa2, 0x49, 0x64) + o = msgp.AppendUint32(o, z.Id) + // string "Data" + o = append(o, 0xa4, 0x44, 0x61, 0x74, 0x61) + o = msgp.AppendBytes(o, z.Data) + // string "Kind" + o = append(o, 0xa4, 0x4b, 0x69, 0x6e, 0x64) + o = msgp.AppendUint8(o, uint8(z.Kind)) + return +} + +// UnmarshalMsg implements msgp.Unmarshaler +func (z *Frame) UnmarshalMsg(bts []byte) (o []byte, err error) { + var field []byte + _ = field + var zb0001 uint32 + zb0001, bts, err = msgp.ReadMapHeaderBytes(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + for zb0001 > 0 { + zb0001-- + field, bts, err = msgp.ReadMapKeyZC(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + switch msgp.UnsafeString(field) { + case "Id": + z.Id, bts, err = msgp.ReadUint32Bytes(bts) + if err != nil { + err = msgp.WrapError(err, "Id") + return + } + case "Data": + z.Data, bts, err = msgp.ReadBytesBytes(bts, z.Data) + if err != nil { + err = msgp.WrapError(err, "Data") + return + } + case "Kind": + { + var zb0002 uint8 + zb0002, bts, err = msgp.ReadUint8Bytes(bts) + if err != nil { + err = msgp.WrapError(err, "Kind") + return + } + z.Kind = Kind(zb0002) + } + default: + bts, err = msgp.Skip(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + } + } + o = bts + return +} + +// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message +func (z *Frame) Msgsize() (s int) { + s = 1 + 3 + msgp.Uint32Size + 5 + msgp.BytesPrefixSize + len(z.Data) + 5 + msgp.Uint8Size + return +} + +// DecodeMsg implements msgp.Decodable +func (z *Kind) DecodeMsg(dc *msgp.Reader) (err error) { + { + var zb0001 uint8 + zb0001, err = dc.ReadUint8() + if err != nil { + err = msgp.WrapError(err) + return + } + (*z) = Kind(zb0001) + } + return +} + +// EncodeMsg implements msgp.Encodable +func (z Kind) EncodeMsg(en *msgp.Writer) (err error) { + err = en.WriteUint8(uint8(z)) + if err != nil { + err = msgp.WrapError(err) + return + } + return +} + +// MarshalMsg implements msgp.Marshaler +func (z Kind) MarshalMsg(b []byte) (o []byte, err error) { + o = msgp.Require(b, z.Msgsize()) + o = msgp.AppendUint8(o, uint8(z)) + return +} + +// UnmarshalMsg implements msgp.Unmarshaler +func (z *Kind) UnmarshalMsg(bts []byte) (o []byte, err error) { + { + var zb0001 uint8 + zb0001, bts, err = msgp.ReadUint8Bytes(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + (*z) = Kind(zb0001) + } + o = bts + return +} + +// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message +func (z Kind) Msgsize() (s int) { + s = msgp.Uint8Size + return +} diff --git a/frame_gen_test.go b/frame_gen_test.go new file mode 100644 index 0000000..6b93c97 --- /dev/null +++ b/frame_gen_test.go @@ -0,0 +1,349 @@ +package gotelem + +// Code generated by github.com/tinylib/msgp DO NOT EDIT. + +import ( + "bytes" + "testing" + + "github.com/tinylib/msgp/msgp" +) + +func TestMarshalUnmarshalCanFilter(t *testing.T) { + v := CanFilter{} + 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 BenchmarkMarshalMsgCanFilter(b *testing.B) { + v := CanFilter{} + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + v.MarshalMsg(nil) + } +} + +func BenchmarkAppendMsgCanFilter(b *testing.B) { + v := CanFilter{} + 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 BenchmarkUnmarshalCanFilter(b *testing.B) { + v := CanFilter{} + 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 TestEncodeDecodeCanFilter(t *testing.T) { + v := CanFilter{} + var buf bytes.Buffer + msgp.Encode(&buf, &v) + + m := v.Msgsize() + if buf.Len() > m { + t.Log("WARNING: TestEncodeDecodeCanFilter Msgsize() is inaccurate") + } + + vn := CanFilter{} + 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 BenchmarkEncodeCanFilter(b *testing.B) { + v := CanFilter{} + 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 BenchmarkDecodeCanFilter(b *testing.B) { + v := CanFilter{} + 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) + } + } +} + +func TestMarshalUnmarshalCanWriter(t *testing.T) { + v := CanWriter{} + 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 BenchmarkMarshalMsgCanWriter(b *testing.B) { + v := CanWriter{} + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + v.MarshalMsg(nil) + } +} + +func BenchmarkAppendMsgCanWriter(b *testing.B) { + v := CanWriter{} + 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 BenchmarkUnmarshalCanWriter(b *testing.B) { + v := CanWriter{} + 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 TestEncodeDecodeCanWriter(t *testing.T) { + v := CanWriter{} + var buf bytes.Buffer + msgp.Encode(&buf, &v) + + m := v.Msgsize() + if buf.Len() > m { + t.Log("WARNING: TestEncodeDecodeCanWriter Msgsize() is inaccurate") + } + + vn := CanWriter{} + 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 BenchmarkEncodeCanWriter(b *testing.B) { + v := CanWriter{} + 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 BenchmarkDecodeCanWriter(b *testing.B) { + v := CanWriter{} + 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) + } + } +} + +func TestMarshalUnmarshalFrame(t *testing.T) { + v := Frame{} + 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 BenchmarkMarshalMsgFrame(b *testing.B) { + v := Frame{} + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + v.MarshalMsg(nil) + } +} + +func BenchmarkAppendMsgFrame(b *testing.B) { + v := Frame{} + 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 BenchmarkUnmarshalFrame(b *testing.B) { + v := Frame{} + 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 TestEncodeDecodeFrame(t *testing.T) { + v := Frame{} + var buf bytes.Buffer + msgp.Encode(&buf, &v) + + m := v.Msgsize() + if buf.Len() > m { + t.Log("WARNING: TestEncodeDecodeFrame Msgsize() is inaccurate") + } + + vn := Frame{} + 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 BenchmarkEncodeFrame(b *testing.B) { + v := Frame{} + 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 BenchmarkDecodeFrame(b *testing.B) { + v := Frame{} + 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) + } + } +} diff --git a/frame_kind.go b/frame_kind.go index 3ab0b3c..4ee7502 100644 --- a/frame_kind.go +++ b/frame_kind.go @@ -14,9 +14,9 @@ func _() { _ = x[CanErrFrame-3] } -const _Kind_name = "SFFEFFRTRERR" +const _Kind_name = "CanSFFFrameCanEFFFrameCanRTRFrameCanErrFrame" -var _Kind_index = [...]uint8{0, 3, 6, 9, 12} +var _Kind_index = [...]uint8{0, 11, 22, 33, 44} func (i Kind) String() string { if i >= Kind(len(_Kind_index)-1) {