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

feat: Add new Properties field to Device DTO and Model #800

Merged
merged 1 commit into from
Feb 17, 2023
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
5 changes: 5 additions & 0 deletions dtos/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -110,6 +114,7 @@ func FromDeviceModelToUpdateDTO(d models.Device) UpdateDevice {
Labels: d.Labels,
Notify: &d.Notify,
Tags: d.Tags,
Properties: d.Properties,
}
return dto
}
1 change: 1 addition & 0 deletions dtos/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 6 additions & 0 deletions dtos/requests/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions dtos/requests/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions models/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down