Skip to content

Commit

Permalink
Add JSON Marshalling support to device and entity (#45)
Browse files Browse the repository at this point in the history
Both DeviceInterface and EntityInterface implementations (e.g.
DeviceRemoteInterface and EntityRemoteInterface) are used as arguments
in API calls and return values.

When these API calls are used via interprocess communication protocols
based on JSON, then the easiest way is to convert them into their
addresses to be identified.

This change adds support for this need.
  • Loading branch information
DerAndereAndi authored Nov 11, 2024
2 parents c2d2b16 + 8aa7206 commit fb7dc01
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 2 deletions.
26 changes: 25 additions & 1 deletion spine/device.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package spine

import "github.com/enbility/spine-go/model"
import (
"encoding/json"

"github.com/enbility/spine-go/model"
)

type Device struct {
address *model.AddressDeviceType
Expand Down Expand Up @@ -33,6 +37,26 @@ func (r *Device) Address() *model.AddressDeviceType {
return r.address
}

// Add support for JSON Marshalling
//
// Instances of EntityInterface are used as arguments and return values in various API calls,
// therefor it is helpfull to be able to marshal them to JSON and thus make the API calls
// usable with various communication interfaces
func (r *Device) MarshalJSON() ([]byte, error) {
var tempAddress string

if r.address != nil {
tempAddress = string(*r.address)
}

bytes, err := json.Marshal(tempAddress)
if err != nil {
return nil, err
}

return bytes, nil
}

func (r *Device) DeviceType() *model.DeviceTypeType {
return r.dType
}
Expand Down
35 changes: 35 additions & 0 deletions spine/device_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package spine

import (
"encoding/json"
"testing"

"github.com/enbility/spine-go/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

func TestDeviceSuite(t *testing.T) {
suite.Run(t, new(DeviceTestSuite))
}

type DeviceTestSuite struct {
suite.Suite
}

func (s *DeviceTestSuite) Test_Device() {
deviceAddress := model.AddressDeviceType("test")
device := NewDevice(&deviceAddress, nil, nil)

value, err := json.Marshal(device)
assert.Nil(s.T(), err)
assert.NotNil(s.T(), value)
assert.Equal(s.T(), `"test"`, string(value))

device = NewDevice(nil, nil, nil)

value, err = json.Marshal(device)
assert.Nil(s.T(), err)
assert.NotNil(s.T(), value)
assert.Equal(s.T(), `""`, string(value))
}
30 changes: 29 additions & 1 deletion spine/entity.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spine

import (
"encoding/json"
"sync"

"github.com/ahmetb/go-linq/v3"
Expand Down Expand Up @@ -32,7 +33,7 @@ func NewEntity(eType model.EntityTypeType, deviceAddress *model.AddressDeviceTyp
Entity: entityAddress,
},
}
if entityAddress[0] == 0 {
if entityAddress != nil && entityAddress[0] == 0 {
// Entity 0 Feature addresses start with 0
entity.fIdGenerator = newFeatureIdGenerator(0)
} else {
Expand All @@ -47,6 +48,33 @@ func (r *Entity) Address() *model.EntityAddressType {
return r.address
}

// Add support for JSON Marshalling
//
// Instances of EntityInterface are used as arguments and return values in various API calls,
// therefor it is helpfull to be able to marshal them to JSON and thus make the API calls
// usable with various communication interfaces
func (r *Entity) MarshalJSON() ([]byte, error) {
// we do not want to omit address fields, if they are nil
// and field names should not be lowercased
type tempAddressType struct {
Device model.AddressDeviceType
Entity []model.AddressEntityType
}
var tempAddress tempAddressType

if r.address.Device != nil {
tempAddress.Device = *r.address.Device
}
tempAddress.Entity = r.address.Entity

bytes, err := json.Marshal(tempAddress)
if err != nil {
return nil, err
}

return bytes, nil
}

func (r *Entity) EntityType() model.EntityTypeType {
return r.eType
}
Expand Down
42 changes: 42 additions & 0 deletions spine/entity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package spine

import (
"encoding/json"
"testing"

"github.com/enbility/spine-go/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

func TestEntitySuite(t *testing.T) {
suite.Run(t, new(EntityTestSuite))
}

type EntityTestSuite struct {
suite.Suite
}

func (s *EntityTestSuite) Test_Entity() {
deviceAddress := model.AddressDeviceType("test")
entity := NewEntity(model.EntityTypeTypeCEM, &deviceAddress, NewAddressEntityType([]uint{1, 1}))

value, err := json.Marshal(entity)
assert.Nil(s.T(), err)
assert.NotNil(s.T(), value)
assert.Equal(s.T(), `{"Device":"test","Entity":[1,1]}`, string(value))

entity = NewEntity(model.EntityTypeTypeCEM, &deviceAddress, nil)

value, err = json.Marshal(entity)
assert.Nil(s.T(), err)
assert.NotNil(s.T(), value)
assert.Equal(s.T(), `{"Device":"test","Entity":null}`, string(value))

entity = NewEntity(model.EntityTypeTypeCEM, nil, nil)

value, err = json.Marshal(entity)
assert.Nil(s.T(), err)
assert.NotNil(s.T(), value)
assert.Equal(s.T(), `{"Device":"","Entity":null}`, string(value))
}

0 comments on commit fb7dc01

Please sign in to comment.