Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON Marshalling support to device and entity #45

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
Loading