From c3d6c3b553a2b1ca474f6726b36d8adec9744a3e Mon Sep 17 00:00:00 2001 From: saji Date: Fri, 30 Jun 2023 07:18:15 -0500 Subject: [PATCH] use custom error and panic on invariant violation --- skylab/skylab.go | 12 ++++++------ skylab/skylab_gen.go | 11 +++++------ skylab/templates/golang.go.tmpl | 9 ++++----- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/skylab/skylab.go b/skylab/skylab.go index 41e457b..a76b4ea 100644 --- a/skylab/skylab.go +++ b/skylab/skylab.go @@ -41,11 +41,11 @@ func float32FromBytes(b []byte, bigEndian bool) (f float32) { // Packet is any Skylab-generated packet. type Packet interface { - MarshalPacket() ([]byte, error) - UnmarshalPacket(p []byte) error - CANId() (uint32, error) - Size() uint - String() string + Marshaler + Unmarshaler + Ider + Sizer + fmt.Stringer // to get the name } // Marshaler is a packet that can be marshalled into bytes. @@ -164,7 +164,7 @@ func (e *BusEvent) UnmarshalMsg(b []byte) ([]byte, error) { // generator since we can use the switch/case thing since it's the fastest type UnknownIdError struct { - id uint64 + id uint32 } func (e *UnknownIdError) Error() string { diff --git a/skylab/skylab_gen.go b/skylab/skylab_gen.go index 9b5fb12..894d990 100644 --- a/skylab/skylab_gen.go +++ b/skylab/skylab_gen.go @@ -1,9 +1,8 @@ -// generated by gen_skylab.go at 2023-06-28 08:15:54.687487803 -0500 CDT m=+0.002619032 DO NOT EDIT! +// generated by gen_skylab.go at 2023-06-30 07:17:44.496220436 -0500 CDT m=+0.002737106 DO NOT EDIT! package skylab import ( - "errors" "encoding/binary" "encoding/json" ) @@ -236,7 +235,7 @@ var idMap = map[uint32]bool{ // If the CAN ID is unknown, it will return an error. func FromCanFrame(id uint32, data []byte) (Packet, error) { if !idMap[id] { - return nil, &UnknownIdError{ uint64(id) } + return nil, &UnknownIdError{ id } } switch id { case 0x10: @@ -576,13 +575,13 @@ func FromCanFrame(id uint32, data []byte) (Packet, error) { return res, nil } - return nil, errors.New("failed to match Id, something is really wrong") + panic("This should never happen. CAN ID didn't match but was in ID map") } func FromJson (id uint32, raw []byte) (Packet, error) { if !idMap[id] { - return nil, errors.New("unknown id") + return nil, &UnknownIdError{ id } } switch id { case 0x10: @@ -922,7 +921,7 @@ func FromJson (id uint32, raw []byte) (Packet, error) { return res, err } - return nil, errors.New("failed to match id") + panic("This should never happen. CAN ID didn't match but was in ID map") } diff --git a/skylab/templates/golang.go.tmpl b/skylab/templates/golang.go.tmpl index 5f781c9..5254aa1 100644 --- a/skylab/templates/golang.go.tmpl +++ b/skylab/templates/golang.go.tmpl @@ -84,7 +84,6 @@ func (p *{{$structName}}) String() string { package skylab import ( - "errors" "encoding/binary" "encoding/json" ) @@ -114,7 +113,7 @@ var idMap = map[uint32]bool{ // If the CAN ID is unknown, it will return an error. func FromCanFrame(id uint32, data []byte) (Packet, error) { if !idMap[id] { - return nil, &UnknownIdError{ uint64(id) } + return nil, &UnknownIdError{ id } } switch id { {{- range $p := .Packets }} @@ -133,13 +132,13 @@ func FromCanFrame(id uint32, data []byte) (Packet, error) { {{- end}} } - return nil, errors.New("failed to match Id, something is really wrong") + panic("This should never happen. CAN ID didn't match but was in ID map") } func FromJson (id uint32, raw []byte) (Packet, error) { if !idMap[id] { - return nil, errors.New("unknown id") + return nil, &UnknownIdError{ id } } switch id { {{- range $p := .Packets }} @@ -158,7 +157,7 @@ func FromJson (id uint32, raw []byte) (Packet, error) { {{- end }} } - return nil, errors.New("failed to match id") + panic("This should never happen. CAN ID didn't match but was in ID map") } {{range .Packets -}}