diff --git a/plc4go/internal/cbus/Field.go b/plc4go/internal/cbus/Field.go index a2cd08a4b60..d8a220dc819 100644 --- a/plc4go/internal/cbus/Field.go +++ b/plc4go/internal/cbus/Field.go @@ -255,67 +255,78 @@ type unitInfoField struct { /////////////////////////////////////// /////////////////////////////////////// -func (m statusField) GetAddressString() string { +func (s statusField) GetAddressString() string { statusRequestType := "" - switch m.statusRequestType { + switch s.statusRequestType { case StatusRequestTypeBinaryState: statusRequestType = "binary" case StatusRequestTypeLevel: statusRequestType = "level" - statusRequestType += fmt.Sprintf("=0x%x", *m.startingGroupAddressLabel) + statusRequestType += fmt.Sprintf("=0x%x", *s.startingGroupAddressLabel) } - return fmt.Sprintf("status/%s/%s", statusRequestType, m.application) + return fmt.Sprintf("status/%s/%s", statusRequestType, s.application) } -func (m statusField) GetStatusRequestType() StatusRequestType { - return m.statusRequestType +func (s statusField) GetStatusRequestType() StatusRequestType { + return s.statusRequestType } -func (m statusField) GetStartingGroupAddressLabel() *byte { - return m.startingGroupAddressLabel +func (s statusField) GetStartingGroupAddressLabel() *byte { + return s.startingGroupAddressLabel } -func (m statusField) GetApplication() readWriteModel.ApplicationIdContainer { - return m.application +func (s statusField) GetApplication() readWriteModel.ApplicationIdContainer { + return s.application } -func (m statusField) GetTypeName() string { +func (s statusField) GetTypeName() string { return STATUS.GetName() } -func (m statusField) GetQuantity() uint16 { - return m.numElements +func (s statusField) GetQuantity() uint16 { + return s.numElements } -func (m statusField) Serialize(writeBuffer utils.WriteBuffer) error { - if err := writeBuffer.PushContext(m.fieldType.GetName()); err != nil { +func (s statusField) Serialize(writeBuffer utils.WriteBuffer) error { + if err := writeBuffer.PushContext(s.fieldType.GetName()); err != nil { return err } - if err := writeBuffer.WriteUint8("statusRequestType", 8, uint8(m.statusRequestType), utils.WithAdditionalStringRepresentation(m.statusRequestType.String())); err != nil { + if err := writeBuffer.WriteUint8("statusRequestType", 8, uint8(s.statusRequestType), utils.WithAdditionalStringRepresentation(s.statusRequestType.String())); err != nil { return err } - if m.startingGroupAddressLabel != nil { - if err := writeBuffer.WriteUint8("startingGroupAddressLabel", 8, *m.startingGroupAddressLabel); err != nil { + if s.startingGroupAddressLabel != nil { + if err := writeBuffer.WriteUint8("startingGroupAddressLabel", 8, *s.startingGroupAddressLabel); err != nil { return err } } - if err := writeBuffer.WriteUint8("application", 8, uint8(m.application), utils.WithAdditionalStringRepresentation(m.application.String())); err != nil { + if err := writeBuffer.WriteUint8("application", 8, uint8(s.application), utils.WithAdditionalStringRepresentation(s.application.String())); err != nil { return err } - if err := writeBuffer.PopContext(m.fieldType.GetName()); err != nil { + if err := writeBuffer.PopContext(s.fieldType.GetName()); err != nil { return err } return nil } +func (s statusField) String() string { + writeBuffer := utils.NewWriteBufferBoxBasedWithOptions(true, true) + if err := writeBuffer.WriteSerializable(s); err != nil { + return err.Error() + } + return writeBuffer.GetBox().String() +} + func (c calField) GetUnitAddress() readWriteModel.UnitAddress { return c.unitAddress } func (c calField) Serialize(writeBuffer utils.WriteBuffer) error { - return c.unitAddress.Serialize(writeBuffer) + if unitAddress := c.unitAddress; unitAddress != nil { + return c.unitAddress.Serialize(writeBuffer) + } + return nil } func (c calField) String() string { diff --git a/plc4go/internal/cbus/FieldRender_test.go b/plc4go/internal/cbus/FieldRender_test.go new file mode 100644 index 00000000000..3e7df24df6a --- /dev/null +++ b/plc4go/internal/cbus/FieldRender_test.go @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package cbus + +import ( + "fmt" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNonPanickingStrings(t *testing.T) { + suts := []fmt.Stringer{ + &statusField{}, + &calField{}, + &calRecallField{}, + &calIdentifyField{}, + &calGetstatusField{}, + &salField{}, + &salMonitorField{}, + &mmiMonitorField{}, + &unitInfoField{}, + } + for _, sut := range suts { + t.Run(fmt.Sprintf("%T", sut), func(t *testing.T) { + assert.NotEmptyf(t, sut.String(), "string should at least return type informations") + }) + } +}