Skip to content

Commit b318cbb

Browse files
committed
fix(plc4go/cbus): fixed npe while rendering fields
1 parent 1a54d16 commit b318cbb

File tree

2 files changed

+77
-21
lines changed

2 files changed

+77
-21
lines changed

plc4go/internal/cbus/Field.go

+32-21
Original file line numberDiff line numberDiff line change
@@ -255,67 +255,78 @@ type unitInfoField struct {
255255
///////////////////////////////////////
256256
///////////////////////////////////////
257257

258-
func (m statusField) GetAddressString() string {
258+
func (s statusField) GetAddressString() string {
259259
statusRequestType := ""
260-
switch m.statusRequestType {
260+
switch s.statusRequestType {
261261
case StatusRequestTypeBinaryState:
262262
statusRequestType = "binary"
263263
case StatusRequestTypeLevel:
264264
statusRequestType = "level"
265-
statusRequestType += fmt.Sprintf("=0x%x", *m.startingGroupAddressLabel)
265+
statusRequestType += fmt.Sprintf("=0x%x", *s.startingGroupAddressLabel)
266266
}
267-
return fmt.Sprintf("status/%s/%s", statusRequestType, m.application)
267+
return fmt.Sprintf("status/%s/%s", statusRequestType, s.application)
268268
}
269269

270-
func (m statusField) GetStatusRequestType() StatusRequestType {
271-
return m.statusRequestType
270+
func (s statusField) GetStatusRequestType() StatusRequestType {
271+
return s.statusRequestType
272272
}
273273

274-
func (m statusField) GetStartingGroupAddressLabel() *byte {
275-
return m.startingGroupAddressLabel
274+
func (s statusField) GetStartingGroupAddressLabel() *byte {
275+
return s.startingGroupAddressLabel
276276
}
277277

278-
func (m statusField) GetApplication() readWriteModel.ApplicationIdContainer {
279-
return m.application
278+
func (s statusField) GetApplication() readWriteModel.ApplicationIdContainer {
279+
return s.application
280280
}
281281

282-
func (m statusField) GetTypeName() string {
282+
func (s statusField) GetTypeName() string {
283283
return STATUS.GetName()
284284
}
285285

286-
func (m statusField) GetQuantity() uint16 {
287-
return m.numElements
286+
func (s statusField) GetQuantity() uint16 {
287+
return s.numElements
288288
}
289289

290-
func (m statusField) Serialize(writeBuffer utils.WriteBuffer) error {
291-
if err := writeBuffer.PushContext(m.fieldType.GetName()); err != nil {
290+
func (s statusField) Serialize(writeBuffer utils.WriteBuffer) error {
291+
if err := writeBuffer.PushContext(s.fieldType.GetName()); err != nil {
292292
return err
293293
}
294294

295-
if err := writeBuffer.WriteUint8("statusRequestType", 8, uint8(m.statusRequestType), utils.WithAdditionalStringRepresentation(m.statusRequestType.String())); err != nil {
295+
if err := writeBuffer.WriteUint8("statusRequestType", 8, uint8(s.statusRequestType), utils.WithAdditionalStringRepresentation(s.statusRequestType.String())); err != nil {
296296
return err
297297
}
298-
if m.startingGroupAddressLabel != nil {
299-
if err := writeBuffer.WriteUint8("startingGroupAddressLabel", 8, *m.startingGroupAddressLabel); err != nil {
298+
if s.startingGroupAddressLabel != nil {
299+
if err := writeBuffer.WriteUint8("startingGroupAddressLabel", 8, *s.startingGroupAddressLabel); err != nil {
300300
return err
301301
}
302302
}
303-
if err := writeBuffer.WriteUint8("application", 8, uint8(m.application), utils.WithAdditionalStringRepresentation(m.application.String())); err != nil {
303+
if err := writeBuffer.WriteUint8("application", 8, uint8(s.application), utils.WithAdditionalStringRepresentation(s.application.String())); err != nil {
304304
return err
305305
}
306306

307-
if err := writeBuffer.PopContext(m.fieldType.GetName()); err != nil {
307+
if err := writeBuffer.PopContext(s.fieldType.GetName()); err != nil {
308308
return err
309309
}
310310
return nil
311311
}
312312

313+
func (s statusField) String() string {
314+
writeBuffer := utils.NewWriteBufferBoxBasedWithOptions(true, true)
315+
if err := writeBuffer.WriteSerializable(s); err != nil {
316+
return err.Error()
317+
}
318+
return writeBuffer.GetBox().String()
319+
}
320+
313321
func (c calField) GetUnitAddress() readWriteModel.UnitAddress {
314322
return c.unitAddress
315323
}
316324

317325
func (c calField) Serialize(writeBuffer utils.WriteBuffer) error {
318-
return c.unitAddress.Serialize(writeBuffer)
326+
if unitAddress := c.unitAddress; unitAddress != nil {
327+
return c.unitAddress.Serialize(writeBuffer)
328+
}
329+
return nil
319330
}
320331

321332
func (c calField) String() string {
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package cbus
21+
22+
import (
23+
"fmt"
24+
"github.com/stretchr/testify/assert"
25+
"testing"
26+
)
27+
28+
func TestNonPanickingStrings(t *testing.T) {
29+
suts := []fmt.Stringer{
30+
&statusField{},
31+
&calField{},
32+
&calRecallField{},
33+
&calIdentifyField{},
34+
&calGetstatusField{},
35+
&salField{},
36+
&salMonitorField{},
37+
&mmiMonitorField{},
38+
&unitInfoField{},
39+
}
40+
for _, sut := range suts {
41+
t.Run(fmt.Sprintf("%T", sut), func(t *testing.T) {
42+
assert.NotEmptyf(t, sut.String(), "string should at least return type informations")
43+
})
44+
}
45+
}

0 commit comments

Comments
 (0)