Skip to content

Commit

Permalink
fix: generate - handle enum descriptions with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
d4l3k authored and alethenorio committed Jun 18, 2021
1 parent b8fe9a7 commit c35b453
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions internal/generate/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestCompile_ExampleDBC(t *testing.T) {
{Value: 0, Description: "None"},
{Value: 1, Description: "Sync"},
{Value: 2, Description: "Reboot"},
{Value: 3, Description: "Headlights On"},
},
},
},
Expand Down
14 changes: 11 additions & 3 deletions internal/generate/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/format"
"go/types"
"path"
"regexp"
"strings"

"github.com/shurcooL/go-goon"
Expand Down Expand Up @@ -121,20 +122,27 @@ func Imports(f *File) {
f.P("// Generated code. DO NOT EDIT.")
}

var nonAlphaNumericRegexp = regexp.MustCompile("[^a-zA-Z0-9]+")

func slugifyString(s string) string {
return nonAlphaNumericRegexp.ReplaceAllString(s, "")
}

func SignalCustomType(f *File, m *descriptor.Message, s *descriptor.Signal) {
f.P("// ", signalType(m, s), " models the ", s.Name, " signal of the ", m.Name, " message.")
f.P("type ", signalType(m, s), " ", signalPrimitiveType(s))
f.P()
f.P("// Value descriptions for the ", s.Name, " signal of the ", m.Name, " message.")
f.P("const (")
for _, vd := range s.ValueDescriptions {
desc := slugifyString(vd.Description)
switch {
case s.Length == 1 && vd.Value == 1:
f.P(signalType(m, s), "_", vd.Description, " ", signalType(m, s), " = true")
f.P(signalType(m, s), "_", desc, " ", signalType(m, s), " = true")
case s.Length == 1 && vd.Value == 0:
f.P(signalType(m, s), "_", vd.Description, " ", signalType(m, s), " = false")
f.P(signalType(m, s), "_", desc, " ", signalType(m, s), " = false")
default:
f.P(signalType(m, s), "_", vd.Description, " ", signalType(m, s), " = ", vd.Value)
f.P(signalType(m, s), "_", desc, " ", signalType(m, s), " = ", vd.Value)
}
}
f.P(")")
Expand Down
2 changes: 1 addition & 1 deletion testdata/dbc/example/example.dbc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ BA_ "FieldType" SG_ 100 Command "Command";
BA_ "FieldType" SG_ 500 TestEnum "TestEnum";
BA_ "GenSigStartValue" SG_ 500 TestEnum 2;

VAL_ 100 Command 2 "Reboot" 1 "Sync" 0 "None" ;
VAL_ 100 Command 3 "Headlights On" 2 "Reboot" 1 "Sync" 0 "None" ;
VAL_ 500 TestEnum 2 "Two" 1 "One" ;
VAL_ 500 TestScaledEnum 3 "Six" 2 "Four" 1 "Two" 0 "Zero" ;
VAL_ 500 TestBoolEnum 1 "One" 0 "Zero" ;
11 changes: 8 additions & 3 deletions testdata/dbc/example/example.dbc.golden
Original file line number Diff line number Diff line change
Expand Up @@ -817,19 +817,24 @@
MessageID: (dbc.MessageID) 100,
SignalName: (dbc.Identifier) (len=7) "Command",
EnvironmentVariableName: (dbc.Identifier) "",
ValueDescriptions: ([]dbc.ValueDescriptionDef) (len=3) {
ValueDescriptions: ([]dbc.ValueDescriptionDef) (len=4) {
(dbc.ValueDescriptionDef) {
Pos: (scanner.Position) ../../testdata/dbc/example/example.dbc:76:18,
Value: (float64) 3,
Description: (string) (len=13) "Headlights On"
},
(dbc.ValueDescriptionDef) {
Pos: (scanner.Position) ../../testdata/dbc/example/example.dbc:76:36,
Value: (float64) 2,
Description: (string) (len=6) "Reboot"
},
(dbc.ValueDescriptionDef) {
Pos: (scanner.Position) ../../testdata/dbc/example/example.dbc:76:29,
Pos: (scanner.Position) ../../testdata/dbc/example/example.dbc:76:47,
Value: (float64) 1,
Description: (string) (len=4) "Sync"
},
(dbc.ValueDescriptionDef) {
Pos: (scanner.Position) ../../testdata/dbc/example/example.dbc:76:38,
Pos: (scanner.Position) ../../testdata/dbc/example/example.dbc:76:56,
Value: (float64) 0,
Description: (string) (len=4) "None"
}
Expand Down
13 changes: 10 additions & 3 deletions testdata/gen/go/example/example.dbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ type DriverHeartbeat_Command uint8

// Value descriptions for the Command signal of the DriverHeartbeat message.
const (
DriverHeartbeat_Command_None DriverHeartbeat_Command = 0
DriverHeartbeat_Command_Sync DriverHeartbeat_Command = 1
DriverHeartbeat_Command_Reboot DriverHeartbeat_Command = 2
DriverHeartbeat_Command_None DriverHeartbeat_Command = 0
DriverHeartbeat_Command_Sync DriverHeartbeat_Command = 1
DriverHeartbeat_Command_Reboot DriverHeartbeat_Command = 2
DriverHeartbeat_Command_HeadlightsOn DriverHeartbeat_Command = 3
)

func (v DriverHeartbeat_Command) String() string {
Expand All @@ -176,6 +177,8 @@ func (v DriverHeartbeat_Command) String() string {
return "Sync"
case 2:
return "Reboot"
case 3:
return "Headlights On"
default:
return fmt.Sprintf("DriverHeartbeat_Command(%d)", v)
}
Expand Down Expand Up @@ -2493,6 +2496,10 @@ var d = (*descriptor.Database)(&descriptor.Database{
Value: (int)(2),
Description: (string)("Reboot"),
}),
(*descriptor.ValueDescription)(&descriptor.ValueDescription{
Value: (int)(3),
Description: (string)("Headlights On"),
}),
}),
ReceiverNodes: ([]string)([]string{
(string)("SENSOR"),
Expand Down

0 comments on commit c35b453

Please sign in to comment.