Skip to content

Commit

Permalink
Improve partial handling
Browse files Browse the repository at this point in the history
- Add possibility to define each operation to support partials
- Fix possible panic when assigning read selectors or elements
- Improve filter test
- Set defaults for local server feature partial support: read is false, write is true
- Properly set the remotes partial support information
- Model fix
  • Loading branch information
DerAndereAndi committed Jun 2, 2024
1 parent c96bc14 commit 54e0844
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 20 deletions.
2 changes: 2 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ type HeartbeatManagerInterface interface {

type OperationsInterface interface {
Write() bool
WritePartial() bool
Read() bool
ReadPartial() bool
String() string
Information() *model.PossibleOperationsType
}
4 changes: 4 additions & 0 deletions api/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import "github.com/enbility/spine-go/model"
type FunctionDataCmdInterface interface {
FunctionDataInterface
// Get the CmdType data for a read command
//
// Note: partialSelector and elements have to be pointers!
ReadCmdType(partialSelector any, elements any) model.CmdType
// Get the CmdType data for a reply command
ReplyCmdType(partial bool) model.CmdType
// Get the CmdType data for a notify or write command
//
// Note: partialSelector and elements have to be pointers!
NotifyOrWriteCmdType(deleteSelector, partialSelector any, partialWithoutSelector bool, deleteElements any) model.CmdType
}

Expand Down
90 changes: 90 additions & 0 deletions mocks/OperationsInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion model/commandframe_additions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func TestFilterType_Selector_SetDataForFunction(t *testing.T) {
cmd.SetDataForFunction(EEBusTagTypeTypeSelector, FunctionTypeElectricalConnectionDescriptionListData, nil)
assert.Nil(t, cmd.ElectricalConnectionDescriptionListDataSelectors)

var test *ElectricalConnectionDescriptionListDataSelectorsType
test := &ElectricalConnectionDescriptionListDataSelectorsType{
ElectricalConnectionId: util.Ptr(ElectricalConnectionIdType(1)),
}
cmd = FilterType{}
cmd.SetDataForFunction(EEBusTagTypeTypeSelector, FunctionTypeElectricalConnectionDescriptionListData, test)
assert.NotNil(t, cmd.ElectricalConnectionDescriptionListDataSelectors)
Expand Down
4 changes: 2 additions & 2 deletions model/deviceconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ type DeviceConfigurationKeyValueDescriptionListDataType struct {
}

type DeviceConfigurationKeyValueDescriptionListDataSelectorsType struct {
KeyId *DeviceConfigurationKeyIdType `json:"keyId,omitempty"`
KeyName *string `json:"keyName,omitempty"`
KeyId *DeviceConfigurationKeyIdType `json:"keyId,omitempty"`
KeyName *DeviceConfigurationKeyNameType `json:"keyName,omitempty"`
}

type DeviceConfigurationKeyValueConstraintsDataType struct {
Expand Down
2 changes: 1 addition & 1 deletion spine/feature_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *FeatureLocal) AddFunctionType(function model.FunctionType, read, write
if r.operations[function] != nil {
return
}
r.operations[function] = NewOperations(read, write)
r.operations[function] = NewOperations(read, false, write, write)

if r.role == model.RoleTypeServer &&
r.ftype == model.FeatureTypeTypeDeviceDiagnosis &&
Expand Down
7 changes: 6 additions & 1 deletion spine/feature_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ func (r *FeatureRemote) SetOperations(functions []model.FunctionPropertyType) {
if sf.PossibleOperations == nil {
continue
}
r.operations[*sf.Function] = NewOperations(sf.PossibleOperations.Read != nil, sf.PossibleOperations.Write != nil)
r.operations[*sf.Function] = NewOperations(
sf.PossibleOperations.Read != nil,
sf.PossibleOperations.Read != nil && sf.PossibleOperations.Read.Partial != nil,
sf.PossibleOperations.Write != nil,
sf.PossibleOperations.Write != nil && sf.PossibleOperations.Write.Partial != nil,
)
}
}

Expand Down
8 changes: 4 additions & 4 deletions spine/function_data_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ func filtersForSelectorsElements(functionType model.FunctionType, filters []mode
if partialSelector != nil || readElements != nil {
filter := model.FilterType{CmdControl: &model.CmdControlType{Partial: &model.ElementTagType{}}}
if partialSelector != nil {
filter = addSelectorToFilter(filter, functionType, &partialSelector)
filter = addSelectorToFilter(filter, functionType, partialSelector)
}
if readElements != nil {
filter = addElementToFilter(filter, functionType, &readElements)
filter = addElementToFilter(filter, functionType, readElements)
}
filters = append(filters, filter)
}
Expand All @@ -92,15 +92,15 @@ func filterEmptyPartial() []model.FilterType {
return []model.FilterType{*model.NewFilterTypePartial()}
}

func addSelectorToFilter[T any](filter model.FilterType, function model.FunctionType, data *T) model.FilterType {
func addSelectorToFilter(filter model.FilterType, function model.FunctionType, data any) model.FilterType {
result := filter

result.SetDataForFunction(model.EEBusTagTypeTypeSelector, function, data)

return result
}

func addElementToFilter[T any](filter model.FilterType, function model.FunctionType, data *T) model.FilterType {
func addElementToFilter(filter model.FilterType, function model.FunctionType, data any) model.FilterType {
result := filter

result.SetDataForFunction(model.EEbusTagTypeTypeElements, function, data)
Expand Down
5 changes: 4 additions & 1 deletion spine/function_data_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func (suite *FctDataCmdSuite) TestFunctionDataCmd_ReadCmd() {
assert.Nil(suite.T(), readCmd.Function)

partialS := model.NewFilterTypePartial()
readCmd = suite.sut.ReadCmdType(partialS, nil)
partialE := &model.DeviceClassificationManufacturerDataElementsType{
DeviceName: util.Ptr(model.ElementTagType{}),
}
readCmd = suite.sut.ReadCmdType(partialS, partialE)
assert.NotNil(suite.T(), readCmd.DeviceClassificationManufacturerData)
assert.Nil(suite.T(), readCmd.DeviceClassificationManufacturerData.DeviceName)
assert.NotNil(suite.T(), readCmd.Function)
Expand Down
32 changes: 25 additions & 7 deletions spine/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,36 @@ import (
)

type Operations struct {
read, write bool
read, write bool
readPartial, writePartial bool
}

var _ api.OperationsInterface = (*Operations)(nil)

func NewOperations(read, write bool) *Operations {
func NewOperations(read, readPartial, write, writePartial bool) *Operations {
return &Operations{
read: read,
write: write,
read: read,
readPartial: readPartial,
write: write,
writePartial: writePartial,
}
}

func (r *Operations) Read() bool {
return r.read
}

func (r *Operations) ReadPartial() bool {
return r.readPartial
}

func (r *Operations) Write() bool {
return r.write
}

func (r *Operations) WritePartial() bool {
return r.writePartial
}

func (r *Operations) String() string {
switch {
case r.read && !r.write:
Expand All @@ -41,10 +51,18 @@ func (r *Operations) Information() *model.PossibleOperationsType {
res := new(model.PossibleOperationsType)
if r.read {
res.Read = &model.PossibleOperationsReadType{}
if r.readPartial {
res.Read = &model.PossibleOperationsReadType{
Partial: &model.ElementTagType{},
}
}
}
if r.write {
res.Write = &model.PossibleOperationsWriteType{
Partial: &model.ElementTagType{},
res.Write = &model.PossibleOperationsWriteType{}
if r.writePartial {
res.Write = &model.PossibleOperationsWriteType{
Partial: &model.ElementTagType{},
}
}
}

Expand Down
33 changes: 30 additions & 3 deletions spine/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestOperations(t *testing.T) {
operations := NewOperations(true, false)
operations := NewOperations(true, true, false, false)
assert.NotNil(t, operations)

text := operations.String()
Expand All @@ -16,7 +16,16 @@ func TestOperations(t *testing.T) {
data := operations.Information()
assert.NotNil(t, data)

operations2 := NewOperations(true, true)
result := operations.Read()
assert.True(t, result)
result = operations.ReadPartial()
assert.True(t, result)
result = operations.Write()
assert.False(t, result)
result = operations.WritePartial()
assert.False(t, result)

operations2 := NewOperations(true, false, true, true)
assert.NotNil(t, operations2)

text = operations2.String()
Expand All @@ -25,12 +34,30 @@ func TestOperations(t *testing.T) {
data = operations2.Information()
assert.NotNil(t, data)

operations3 := NewOperations(false, false)
result = operations2.Read()
assert.True(t, result)
result = operations2.ReadPartial()
assert.False(t, result)
result = operations2.Write()
assert.True(t, result)
result = operations2.WritePartial()
assert.True(t, result)

operations3 := NewOperations(false, false, false, false)
assert.NotNil(t, operations3)

text = operations3.String()
assert.NotEqual(t, 0, len(text))

data = operations3.Information()
assert.NotNil(t, data)

result = operations3.Read()
assert.False(t, result)
result = operations3.ReadPartial()
assert.False(t, result)
result = operations3.Write()
assert.False(t, result)
result = operations3.WritePartial()
assert.False(t, result)
}

0 comments on commit 54e0844

Please sign in to comment.