Skip to content

Commit

Permalink
fix(plc4go/cbus): fixed npe while rendering fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Sep 29, 2022
1 parent 1a54d16 commit b318cbb
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 21 deletions.
53 changes: 32 additions & 21 deletions plc4go/internal/cbus/Field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
45 changes: 45 additions & 0 deletions plc4go/internal/cbus/FieldRender_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
}
}

0 comments on commit b318cbb

Please sign in to comment.