Skip to content

Commit 56c0075

Browse files
committed
fix(cantool)!: CopyFrom accepts canrunner.Node Rx messages
When updating the signature for CopyFrom we accidentally broke a workflow where receive messages in a `canrunner.Node` and then copy the data from them using `CopyFrom`. This commit restores CopyFrom to accept a "message reader" type, but extends that type to also require that values implement `Frame() can.Frame`. All messages generated by cantool already implements `Frame() can.Frame` so only mocks should be affected. BREAKING CHANGE: This restores old signature of CopyFrom to accept a message reader instead of a message instance to copy from.
1 parent 36c7f19 commit 56c0075

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

internal/generate/example_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,13 @@ func TestExample_Node_NoEmptyMessages(t *testing.T) {
356356
assert.NilError(t, g.Wait())
357357
}
358358

359+
func TestExample_Node_CopyFromRx(_ *testing.T) {
360+
motor := examplecan.NewMOTOR("udp", "239.255.1.1")
361+
362+
// Verify that motor.Rx().MotorCommand() is a valid argument to CopyFrom.
363+
_ = examplecan.NewMotorCommand().CopyFrom(motor.Rx().MotorCommand())
364+
}
365+
359366
func requireVCAN0(t *testing.T) {
360367
t.Helper()
361368
if _, err := net.InterfaceByName("vcan0"); err != nil {

internal/generate/file.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ func SignalCustomType(f *File, m *descriptor.Message, s *descriptor.Signal) {
180180
func MessageType(f *File, m *descriptor.Message) {
181181
f.P("// ", messageReaderInterface(m), " provides read access to a ", m.Name, " message.")
182182
f.P("type ", messageReaderInterface(m), " interface {")
183+
f.P("can.FrameMarshaler")
183184
for _, s := range m.Signals {
184185
if hasPhysicalRepresentation(s) {
185186
f.P("// ", s.Name, " returns the physical value of the ", s.Name, " signal.")
@@ -196,7 +197,7 @@ func MessageType(f *File, m *descriptor.Message) {
196197
f.P("// ", messageWriterInterface(m), " provides write access to a ", m.Name, " message.")
197198
f.P("type ", messageWriterInterface(m), " interface {")
198199
f.P("// CopyFrom copies all values from ", messageStruct(m), ".")
199-
f.P("CopyFrom(*", messageStruct(m), ") *", messageStruct(m))
200+
f.P("CopyFrom(", messageReaderInterface(m), ") *", messageStruct(m))
200201
for _, s := range m.Signals {
201202
if hasPhysicalRepresentation(s) {
202203
f.P("// Set", s.Name, " sets the physical value of the ", s.Name, " signal.")
@@ -235,8 +236,9 @@ func MessageType(f *File, m *descriptor.Message) {
235236
}
236237
f.P("}")
237238
f.P()
238-
f.P("func (m *", messageStruct(m), ") CopyFrom(o *", messageStruct(m), ") *", messageStruct(m), "{")
239-
f.P("_ = m.UnmarshalFrame(o.Frame())")
239+
f.P("func (m *", messageStruct(m), ") CopyFrom(o ", messageReaderInterface(m), ") *", messageStruct(m), "{")
240+
f.P("f, _ := o.MarshalFrame()")
241+
f.P("_ = m.UnmarshalFrame(f)")
240242
f.P("return m")
241243
f.P("}")
242244
f.P()

testdata/gen/go/example/example.dbc.go

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ var (
3636
// Generated code. DO NOT EDIT.
3737
// EmptyMessageReader provides read access to a EmptyMessage message.
3838
type EmptyMessageReader interface {
39+
can.FrameMarshaler
3940
}
4041

4142
// EmptyMessageWriter provides write access to a EmptyMessage message.
4243
type EmptyMessageWriter interface {
4344
// CopyFrom copies all values from EmptyMessage.
44-
CopyFrom(*EmptyMessage) *EmptyMessage
45+
CopyFrom(EmptyMessageReader) *EmptyMessage
4546
}
4647

4748
type EmptyMessage struct {
@@ -56,8 +57,9 @@ func NewEmptyMessage() *EmptyMessage {
5657
func (m *EmptyMessage) Reset() {
5758
}
5859

59-
func (m *EmptyMessage) CopyFrom(o *EmptyMessage) *EmptyMessage {
60-
_ = m.UnmarshalFrame(o.Frame())
60+
func (m *EmptyMessage) CopyFrom(o EmptyMessageReader) *EmptyMessage {
61+
f, _ := o.MarshalFrame()
62+
_ = m.UnmarshalFrame(f)
6163
return m
6264
}
6365

@@ -109,14 +111,15 @@ func (m *EmptyMessage) UnmarshalFrame(f can.Frame) error {
109111

110112
// DriverHeartbeatReader provides read access to a DriverHeartbeat message.
111113
type DriverHeartbeatReader interface {
114+
can.FrameMarshaler
112115
// Command returns the value of the Command signal.
113116
Command() DriverHeartbeat_Command
114117
}
115118

116119
// DriverHeartbeatWriter provides write access to a DriverHeartbeat message.
117120
type DriverHeartbeatWriter interface {
118121
// CopyFrom copies all values from DriverHeartbeat.
119-
CopyFrom(*DriverHeartbeat) *DriverHeartbeat
122+
CopyFrom(DriverHeartbeatReader) *DriverHeartbeat
120123
// SetCommand sets the value of the Command signal.
121124
SetCommand(DriverHeartbeat_Command) *DriverHeartbeat
122125
}
@@ -135,8 +138,9 @@ func (m *DriverHeartbeat) Reset() {
135138
m.xxx_Command = 0
136139
}
137140

138-
func (m *DriverHeartbeat) CopyFrom(o *DriverHeartbeat) *DriverHeartbeat {
139-
_ = m.UnmarshalFrame(o.Frame())
141+
func (m *DriverHeartbeat) CopyFrom(o DriverHeartbeatReader) *DriverHeartbeat {
142+
f, _ := o.MarshalFrame()
143+
_ = m.UnmarshalFrame(f)
140144
return m
141145
}
142146

@@ -225,6 +229,7 @@ func (m *DriverHeartbeat) UnmarshalFrame(f can.Frame) error {
225229

226230
// MotorCommandReader provides read access to a MotorCommand message.
227231
type MotorCommandReader interface {
232+
can.FrameMarshaler
228233
// Steer returns the physical value of the Steer signal.
229234
Steer() float64
230235
// RawSteer returns the raw (encoded) value of the Steer signal.
@@ -238,7 +243,7 @@ type MotorCommandReader interface {
238243
// MotorCommandWriter provides write access to a MotorCommand message.
239244
type MotorCommandWriter interface {
240245
// CopyFrom copies all values from MotorCommand.
241-
CopyFrom(*MotorCommand) *MotorCommand
246+
CopyFrom(MotorCommandReader) *MotorCommand
242247
// SetSteer sets the physical value of the Steer signal.
243248
SetSteer(float64) *MotorCommand
244249
// SetRawSteer sets the raw (encoded) value of the Steer signal.
@@ -265,8 +270,9 @@ func (m *MotorCommand) Reset() {
265270
m.xxx_Drive = 0
266271
}
267272

268-
func (m *MotorCommand) CopyFrom(o *MotorCommand) *MotorCommand {
269-
_ = m.UnmarshalFrame(o.Frame())
273+
func (m *MotorCommand) CopyFrom(o MotorCommandReader) *MotorCommand {
274+
f, _ := o.MarshalFrame()
275+
_ = m.UnmarshalFrame(f)
270276
return m
271277
}
272278

@@ -358,6 +364,7 @@ func (m *MotorCommand) UnmarshalFrame(f can.Frame) error {
358364

359365
// SensorSonarsReader provides read access to a SensorSonars message.
360366
type SensorSonarsReader interface {
367+
can.FrameMarshaler
361368
// Mux returns the value of the Mux signal.
362369
Mux() uint8
363370
// ErrCount returns the value of the ErrCount signal.
@@ -399,7 +406,7 @@ type SensorSonarsReader interface {
399406
// SensorSonarsWriter provides write access to a SensorSonars message.
400407
type SensorSonarsWriter interface {
401408
// CopyFrom copies all values from SensorSonars.
402-
CopyFrom(*SensorSonars) *SensorSonars
409+
CopyFrom(SensorSonarsReader) *SensorSonars
403410
// SetMux sets the value of the Mux signal.
404411
SetMux(uint8) *SensorSonars
405412
// SetErrCount sets the value of the ErrCount signal.
@@ -470,8 +477,9 @@ func (m *SensorSonars) Reset() {
470477
m.xxx_NoFiltRear = 0
471478
}
472479

473-
func (m *SensorSonars) CopyFrom(o *SensorSonars) *SensorSonars {
474-
_ = m.UnmarshalFrame(o.Frame())
480+
func (m *SensorSonars) CopyFrom(o SensorSonarsReader) *SensorSonars {
481+
f, _ := o.MarshalFrame()
482+
_ = m.UnmarshalFrame(f)
475483
return m
476484
}
477485

@@ -737,6 +745,7 @@ func (m *SensorSonars) UnmarshalFrame(f can.Frame) error {
737745

738746
// MotorStatusReader provides read access to a MotorStatus message.
739747
type MotorStatusReader interface {
748+
can.FrameMarshaler
740749
// WheelError returns the value of the WheelError signal.
741750
WheelError() bool
742751
// SpeedKph returns the physical value of the SpeedKph signal.
@@ -748,7 +757,7 @@ type MotorStatusReader interface {
748757
// MotorStatusWriter provides write access to a MotorStatus message.
749758
type MotorStatusWriter interface {
750759
// CopyFrom copies all values from MotorStatus.
751-
CopyFrom(*MotorStatus) *MotorStatus
760+
CopyFrom(MotorStatusReader) *MotorStatus
752761
// SetWheelError sets the value of the WheelError signal.
753762
SetWheelError(bool) *MotorStatus
754763
// SetSpeedKph sets the physical value of the SpeedKph signal.
@@ -773,8 +782,9 @@ func (m *MotorStatus) Reset() {
773782
m.xxx_SpeedKph = 0
774783
}
775784

776-
func (m *MotorStatus) CopyFrom(o *MotorStatus) *MotorStatus {
777-
_ = m.UnmarshalFrame(o.Frame())
785+
func (m *MotorStatus) CopyFrom(o MotorStatusReader) *MotorStatus {
786+
f, _ := o.MarshalFrame()
787+
_ = m.UnmarshalFrame(f)
778788
return m
779789
}
780790

@@ -857,6 +867,7 @@ func (m *MotorStatus) UnmarshalFrame(f can.Frame) error {
857867

858868
// IODebugReader provides read access to a IODebug message.
859869
type IODebugReader interface {
870+
can.FrameMarshaler
860871
// TestUnsigned returns the value of the TestUnsigned signal.
861872
TestUnsigned() uint8
862873
// TestEnum returns the value of the TestEnum signal.
@@ -878,7 +889,7 @@ type IODebugReader interface {
878889
// IODebugWriter provides write access to a IODebug message.
879890
type IODebugWriter interface {
880891
// CopyFrom copies all values from IODebug.
881-
CopyFrom(*IODebug) *IODebug
892+
CopyFrom(IODebugReader) *IODebug
882893
// SetTestUnsigned sets the value of the TestUnsigned signal.
883894
SetTestUnsigned(uint8) *IODebug
884895
// SetTestEnum sets the value of the TestEnum signal.
@@ -921,8 +932,9 @@ func (m *IODebug) Reset() {
921932
m.xxx_TestScaledEnum = 0
922933
}
923934

924-
func (m *IODebug) CopyFrom(o *IODebug) *IODebug {
925-
_ = m.UnmarshalFrame(o.Frame())
935+
func (m *IODebug) CopyFrom(o IODebugReader) *IODebug {
936+
f, _ := o.MarshalFrame()
937+
_ = m.UnmarshalFrame(f)
926938
return m
927939
}
928940

@@ -1123,6 +1135,7 @@ func (m *IODebug) UnmarshalFrame(f can.Frame) error {
11231135

11241136
// IOFloat32Reader provides read access to a IOFloat32 message.
11251137
type IOFloat32Reader interface {
1138+
can.FrameMarshaler
11261139
// Float32ValueNoRange returns the value of the Float32ValueNoRange signal.
11271140
Float32ValueNoRange() float32
11281141
// Float32WithRange returns the physical value of the Float32WithRange signal.
@@ -1134,7 +1147,7 @@ type IOFloat32Reader interface {
11341147
// IOFloat32Writer provides write access to a IOFloat32 message.
11351148
type IOFloat32Writer interface {
11361149
// CopyFrom copies all values from IOFloat32.
1137-
CopyFrom(*IOFloat32) *IOFloat32
1150+
CopyFrom(IOFloat32Reader) *IOFloat32
11381151
// SetFloat32ValueNoRange sets the value of the Float32ValueNoRange signal.
11391152
SetFloat32ValueNoRange(float32) *IOFloat32
11401153
// SetFloat32WithRange sets the physical value of the Float32WithRange signal.
@@ -1159,8 +1172,9 @@ func (m *IOFloat32) Reset() {
11591172
m.xxx_Float32WithRange = 0
11601173
}
11611174

1162-
func (m *IOFloat32) CopyFrom(o *IOFloat32) *IOFloat32 {
1163-
_ = m.UnmarshalFrame(o.Frame())
1175+
func (m *IOFloat32) CopyFrom(o IOFloat32Reader) *IOFloat32 {
1176+
f, _ := o.MarshalFrame()
1177+
_ = m.UnmarshalFrame(f)
11641178
return m
11651179
}
11661180

0 commit comments

Comments
 (0)