From 66fa623c407397a215cdeb8e13e45ddb0e247888 Mon Sep 17 00:00:00 2001 From: Jude Hung Date: Thu, 16 Feb 2023 11:13:05 +0800 Subject: [PATCH] feat: Add new Properties field to Device DTO and Model Add an extendable field into Device DTO and Model: Properties map[string]any This is useful for some devices that requires extra information. For example, a BACnet device may have properties such as DeviceInstance, Firmware, InstanceID, and ObjectName. fixes https://github.com/edgexfoundry/go-mod-core-contracts/issues/769 Signed-off-by: Jude Hung --- dtos/device.go | 5 +++++ dtos/device_test.go | 1 + dtos/requests/device.go | 6 ++++++ dtos/requests/device_test.go | 2 ++ models/device.go | 1 + 5 files changed, 15 insertions(+) diff --git a/dtos/device.go b/dtos/device.go index 8d90a8e3..bf087ce4 100644 --- a/dtos/device.go +++ b/dtos/device.go @@ -27,6 +27,7 @@ type Device struct { AutoEvents []AutoEvent `json:"autoEvents,omitempty" validate:"dive"` Protocols map[string]ProtocolProperties `json:"protocols" validate:"required,gt=0"` Tags map[string]any `json:"tags,omitempty"` + Properties map[string]any `json:"properties,omitempty"` } // UpdateDevice and its properties are defined in the APIv2 specification: @@ -47,6 +48,7 @@ type UpdateDevice struct { Protocols map[string]ProtocolProperties `json:"protocols" validate:"omitempty,gt=0"` Notify *bool `json:"notify"` Tags map[string]any `json:"tags"` + Properties map[string]any `json:"properties"` } // ToDeviceModel transforms the Device DTO to the Device Model @@ -66,6 +68,7 @@ func ToDeviceModel(dto Device) models.Device { d.AutoEvents = ToAutoEventModels(dto.AutoEvents) d.Protocols = ToProtocolModels(dto.Protocols) d.Tags = dto.Tags + d.Properties = dto.Properties return d } @@ -87,6 +90,7 @@ func FromDeviceModelToDTO(d models.Device) Device { dto.AutoEvents = FromAutoEventModelsToDTOs(d.AutoEvents) dto.Protocols = FromProtocolModelsToDTOs(d.Protocols) dto.Tags = d.Tags + dto.Properties = d.Properties return dto } @@ -110,6 +114,7 @@ func FromDeviceModelToUpdateDTO(d models.Device) UpdateDevice { Labels: d.Labels, Notify: &d.Notify, Tags: d.Tags, + Properties: d.Properties, } return dto } diff --git a/dtos/device_test.go b/dtos/device_test.go index dc34ad59..de8bbf28 100644 --- a/dtos/device_test.go +++ b/dtos/device_test.go @@ -27,4 +27,5 @@ func TestFromDeviceModelToUpdateDTO(t *testing.T) { assert.Equal(t, model.ProfileName, *dto.ProfileName) assert.Equal(t, model.Location, dto.Location) assert.Equal(t, model.Tags, dto.Tags) + assert.Equal(t, model.Properties, dto.Properties) } diff --git a/dtos/requests/device.go b/dtos/requests/device.go index 1fec65f3..08814d0a 100644 --- a/dtos/requests/device.go +++ b/dtos/requests/device.go @@ -128,6 +128,12 @@ func ReplaceDeviceModelFieldsWithDTO(device *models.Device, patch dtos.UpdateDev if patch.Notify != nil { device.Notify = *patch.Notify } + if patch.Tags != nil { + device.Tags = patch.Tags + } + if patch.Properties != nil { + device.Properties = patch.Properties + } } func NewAddDeviceRequest(dto dtos.Device) AddDeviceRequest { diff --git a/dtos/requests/device_test.go b/dtos/requests/device_test.go index 8d2e3268..377794da 100644 --- a/dtos/requests/device_test.go +++ b/dtos/requests/device_test.go @@ -387,6 +387,8 @@ func TestUpdateDeviceRequest_UnmarshalJSON_NilField(t *testing.T) { assert.Nil(t, req.Device.AutoEvents) assert.Nil(t, req.Device.Protocols) assert.Nil(t, req.Device.Notify) + assert.Nil(t, req.Device.Tags) + assert.Nil(t, req.Device.Properties) } func TestUpdateDeviceRequest_UnmarshalJSON_EmptySlice(t *testing.T) { diff --git a/models/device.go b/models/device.go index 45583d19..4e4b863a 100644 --- a/models/device.go +++ b/models/device.go @@ -25,6 +25,7 @@ type Device struct { AutoEvents []AutoEvent Notify bool Tags map[string]any + Properties map[string]any } // ProtocolProperties contains the device connection information in key/value pair