diff --git a/spine/model/commandframe_additions.go b/spine/model/commandframe_additions.go index b2b43630..a8fcaf7b 100644 --- a/spine/model/commandframe_additions.go +++ b/spine/model/commandframe_additions.go @@ -61,6 +61,10 @@ func (f *FilterData) SelectorMatch(item any) bool { // Get the field for a given functionType func (f *FilterType) SetDataForFunction(tagType EEBusTagTypeType, fct FunctionType, data any) { + if data == nil || reflect.ValueOf(data).Kind() != reflect.Ptr { + return + } + v := reflect.ValueOf(*f) dv := reflect.ValueOf(f).Elem() for i := 0; i < v.NumField(); i++ { @@ -99,7 +103,7 @@ func (f *FilterType) SetDataForFunction(tagType EEBusTagTypeType, fct FunctionTy break } - if data == nil || (reflect.ValueOf(data).Kind() == reflect.Ptr && reflect.ValueOf(data).IsNil()) { + if reflect.ValueOf(data).IsNil() { typ := reflect.TypeOf(data).Elem() ff.Set(reflect.New(typ)) return @@ -177,6 +181,10 @@ type CmdData struct { // Get the field for a given functionType func (cmd *CmdType) SetDataForFunction(fct FunctionType, data any) { + if data == nil || reflect.ValueOf(data).Kind() != reflect.Ptr { + return + } + v := reflect.ValueOf(*cmd) dv := reflect.ValueOf(cmd).Elem() for i := 0; i < v.NumField(); i++ { @@ -208,7 +216,7 @@ func (cmd *CmdType) SetDataForFunction(fct FunctionType, data any) { break } - if data == nil || (reflect.ValueOf(data).Kind() == reflect.Ptr && reflect.ValueOf(data).IsNil()) { + if reflect.ValueOf(data).IsNil() { typ := reflect.TypeOf(data).Elem() ff.Set(reflect.New(typ)) return diff --git a/spine/model/commandframe_additions_test.go b/spine/model/commandframe_additions_test.go index 60241907..ca423335 100644 --- a/spine/model/commandframe_additions_test.go +++ b/spine/model/commandframe_additions_test.go @@ -4,9 +4,75 @@ import ( "testing" "github.com/enbility/eebus-go/spine/model" + "github.com/enbility/eebus-go/util" "github.com/stretchr/testify/assert" ) +func TestFilterType_Selector_Data(t *testing.T) { + data := &model.ElectricalConnectionDescriptionListDataSelectorsType{ + ElectricalConnectionId: util.Ptr(model.ElectricalConnectionIdType(1)), + ScopeType: util.Ptr(model.ScopeTypeTypeACPower), + } + + sut := &model.FilterType{ + ElectricalConnectionDescriptionListDataSelectors: data, + } + + // Act + cmdData, err := sut.Data() + assert.Nil(t, err) + assert.NotNil(t, cmdData) + assert.Equal(t, model.FunctionTypeElectricalConnectionDescriptionListData, *cmdData.Function) + assert.Equal(t, data, cmdData.Selector) +} + +func TestFilterType_Selector_SetDataForFunction(t *testing.T) { + cmd := model.FilterType{} + cmd.SetDataForFunction(model.EEBusTagTypeTypeSelector, model.FunctionTypeElectricalConnectionDescriptionListData, &model.ElectricalConnectionDescriptionListDataSelectorsType{}) + assert.NotNil(t, cmd.ElectricalConnectionDescriptionListDataSelectors) + + cmd = model.FilterType{} + cmd.SetDataForFunction(model.EEBusTagTypeTypeSelector, model.FunctionTypeElectricalConnectionDescriptionListData, nil) + assert.Nil(t, cmd.ElectricalConnectionDescriptionListDataSelectors) + + var test *model.ElectricalConnectionDescriptionListDataSelectorsType + cmd = model.FilterType{} + cmd.SetDataForFunction(model.EEBusTagTypeTypeSelector, model.FunctionTypeElectricalConnectionDescriptionListData, test) + assert.NotNil(t, cmd.ElectricalConnectionDescriptionListDataSelectors) +} + +func TestFilterType_Elements_Data(t *testing.T) { + data := &model.ElectricalConnectionDescriptionDataElementsType{ + ElectricalConnectionId: util.Ptr(model.ElementTagType{}), + } + + sut := &model.FilterType{ + ElectricalConnectionDescriptionDataElements: data, + } + + // Act + cmdData, err := sut.Data() + assert.Nil(t, err) + assert.NotNil(t, cmdData) + assert.Equal(t, model.FunctionTypeElectricalConnectionDescriptionListData, *cmdData.Function) + assert.Equal(t, data, cmdData.Elements) +} + +func TestFilterType_Elements_SetDataForFunction(t *testing.T) { + cmd := model.FilterType{} + cmd.SetDataForFunction(model.EEbusTagTypeTypeElements, model.FunctionTypeElectricalConnectionDescriptionListData, &model.ElectricalConnectionDescriptionDataElementsType{}) + assert.NotNil(t, cmd.ElectricalConnectionDescriptionDataElements) + + cmd = model.FilterType{} + cmd.SetDataForFunction(model.EEbusTagTypeTypeElements, model.FunctionTypeElectricalConnectionDescriptionListData, nil) + assert.Nil(t, cmd.ElectricalConnectionDescriptionDataElements) + + var test *model.ElectricalConnectionDescriptionDataElementsType + cmd = model.FilterType{} + cmd.SetDataForFunction(model.EEbusTagTypeTypeElements, model.FunctionTypeElectricalConnectionDescriptionListData, test) + assert.NotNil(t, cmd.ElectricalConnectionDescriptionDataElements) +} + func TestCmdType_Data(t *testing.T) { data := &model.NodeManagementDetailedDiscoveryDataType{ SpecificationVersionList: &model.NodeManagementSpecificationVersionListType{[]model.SpecificationVersionDataType{model.SpecificationVersionDataType("dummy")}}, @@ -25,6 +91,21 @@ func TestCmdType_Data(t *testing.T) { assert.Equal(t, data, cmdData.Value) } +func TestCmdType_SetDataForFunction(t *testing.T) { + cmd := model.CmdType{} + cmd.SetDataForFunction(model.FunctionTypeElectricalConnectionDescriptionListData, &model.ElectricalConnectionDescriptionListDataType{}) + assert.NotNil(t, cmd.ElectricalConnectionDescriptionListData) + + cmd = model.CmdType{} + cmd.SetDataForFunction(model.FunctionTypeElectricalConnectionDescriptionListData, nil) + assert.Nil(t, cmd.ElectricalConnectionDescriptionListData) + + var test *model.ElectricalConnectionDescriptionListDataType + cmd = model.CmdType{} + cmd.SetDataForFunction(model.FunctionTypeElectricalConnectionDescriptionListData, test) + assert.NotNil(t, cmd.ElectricalConnectionDescriptionListData) +} + func TestCmdType_ExtractFilter_NoFilter(t *testing.T) { sut := &model.CmdType{ NodeManagementDetailedDiscoveryData: &model.NodeManagementDetailedDiscoveryDataType{},