use custom error and panic on invariant violation

This commit is contained in:
saji 2023-06-30 07:18:15 -05:00
parent 5ceaa7bf9d
commit c3d6c3b553
3 changed files with 15 additions and 17 deletions

View file

@ -41,11 +41,11 @@ func float32FromBytes(b []byte, bigEndian bool) (f float32) {
// Packet is any Skylab-generated packet. // Packet is any Skylab-generated packet.
type Packet interface { type Packet interface {
MarshalPacket() ([]byte, error) Marshaler
UnmarshalPacket(p []byte) error Unmarshaler
CANId() (uint32, error) Ider
Size() uint Sizer
String() string fmt.Stringer // to get the name
} }
// Marshaler is a packet that can be marshalled into bytes. // 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 // generator since we can use the switch/case thing since it's the fastest
type UnknownIdError struct { type UnknownIdError struct {
id uint64 id uint32
} }
func (e *UnknownIdError) Error() string { func (e *UnknownIdError) Error() string {

View file

@ -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 package skylab
import ( import (
"errors"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
) )
@ -236,7 +235,7 @@ var idMap = map[uint32]bool{
// If the CAN ID is unknown, it will return an error. // If the CAN ID is unknown, it will return an error.
func FromCanFrame(id uint32, data []byte) (Packet, error) { func FromCanFrame(id uint32, data []byte) (Packet, error) {
if !idMap[id] { if !idMap[id] {
return nil, &UnknownIdError{ uint64(id) } return nil, &UnknownIdError{ id }
} }
switch id { switch id {
case 0x10: case 0x10:
@ -576,13 +575,13 @@ func FromCanFrame(id uint32, data []byte) (Packet, error) {
return res, nil 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) { func FromJson (id uint32, raw []byte) (Packet, error) {
if !idMap[id] { if !idMap[id] {
return nil, errors.New("unknown id") return nil, &UnknownIdError{ id }
} }
switch id { switch id {
case 0x10: case 0x10:
@ -922,7 +921,7 @@ func FromJson (id uint32, raw []byte) (Packet, error) {
return res, err 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")
} }

View file

@ -84,7 +84,6 @@ func (p *{{$structName}}) String() string {
package skylab package skylab
import ( import (
"errors"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
) )
@ -114,7 +113,7 @@ var idMap = map[uint32]bool{
// If the CAN ID is unknown, it will return an error. // If the CAN ID is unknown, it will return an error.
func FromCanFrame(id uint32, data []byte) (Packet, error) { func FromCanFrame(id uint32, data []byte) (Packet, error) {
if !idMap[id] { if !idMap[id] {
return nil, &UnknownIdError{ uint64(id) } return nil, &UnknownIdError{ id }
} }
switch id { switch id {
{{- range $p := .Packets }} {{- range $p := .Packets }}
@ -133,13 +132,13 @@ func FromCanFrame(id uint32, data []byte) (Packet, error) {
{{- end}} {{- 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) { func FromJson (id uint32, raw []byte) (Packet, error) {
if !idMap[id] { if !idMap[id] {
return nil, errors.New("unknown id") return nil, &UnknownIdError{ id }
} }
switch id { switch id {
{{- range $p := .Packets }} {{- range $p := .Packets }}
@ -158,7 +157,7 @@ func FromJson (id uint32, raw []byte) (Packet, error) {
{{- end }} {{- 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 -}} {{range .Packets -}}