Skip to content

Commit

Permalink
feat: Add new Properties field to ProvisionWatcher DTO and Model
Browse files Browse the repository at this point in the history
Add an extendable field into ProvisionWatcher DTO and Model: Properties map[string]any
This is useful when certain device services requires extra information from ProvisionWatcher.  For example, assume a device service would like to generate the device name in certain format during auto discovery, the user can define a property "DeviceNameTemplate" with value as the template of device name in the ProvisionWatcher, so that the device service can pick up such property to generate the device name.

fixes #772

Signed-off-by: Jude Hung <[email protected]>
  • Loading branch information
judehung committed Feb 17, 2023
1 parent 1bb5704 commit 4695302
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dtos/provisionwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ProvisionWatcher struct {
ServiceName string `json:"serviceName" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
AdminState string `json:"adminState" validate:"oneof='LOCKED' 'UNLOCKED'"`
AutoEvents []AutoEvent `json:"autoEvents,omitempty" validate:"dive"`
Properties map[string]any `json:"properties,omitempty"`
}

// UpdateProvisionWatcher and its properties are defined in the APIv2 specification:
Expand All @@ -36,6 +37,7 @@ type UpdateProvisionWatcher struct {
ServiceName *string `json:"serviceName" validate:"omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
AdminState *string `json:"adminState" validate:"omitempty,oneof='LOCKED' 'UNLOCKED'"`
AutoEvents []AutoEvent `json:"autoEvents" validate:"dive"`
Properties map[string]any `json:"properties"`
}

// ToProvisionWatcherModel transforms the ProvisionWatcher DTO to the ProvisionWatcher model
Expand All @@ -51,6 +53,7 @@ func ToProvisionWatcherModel(dto ProvisionWatcher) models.ProvisionWatcher {
ServiceName: dto.ServiceName,
AdminState: models.AdminState(dto.AdminState),
AutoEvents: ToAutoEventModels(dto.AutoEvents),
Properties: dto.Properties,
}
}

Expand All @@ -67,6 +70,7 @@ func FromProvisionWatcherModelToDTO(pw models.ProvisionWatcher) ProvisionWatcher
ServiceName: pw.ServiceName,
AdminState: string(pw.AdminState),
AutoEvents: FromAutoEventModelsToDTOs(pw.AutoEvents),
Properties: pw.Properties,
}
}

Expand All @@ -83,6 +87,7 @@ func FromProvisionWatcherModelToUpdateDTO(pw models.ProvisionWatcher) UpdateProv
Labels: pw.Labels,
Identifiers: pw.Identifiers,
BlockingIdentifiers: pw.BlockingIdentifiers,
Properties: pw.Properties,
}
return dto
}
1 change: 1 addition & 0 deletions dtos/provisionwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ func TestFromProvisionWatcherModelToUpdateDTO(t *testing.T) {
assert.Equal(t, model.ProfileName, *dto.ProfileName)
assert.Equal(t, model.ServiceName, *dto.ServiceName)
assert.EqualValues(t, model.AdminState, *dto.AdminState)
assert.Equal(t, model.Properties, dto.Properties)
}
3 changes: 3 additions & 0 deletions dtos/requests/provisionwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ func ReplaceProvisionWatcherModelFieldsWithDTO(pw *models.ProvisionWatcher, patc
if patch.AutoEvents != nil {
pw.AutoEvents = dtos.ToAutoEventModels(patch.AutoEvents)
}
if patch.Properties != nil {
pw.Properties = patch.Properties
}
}

func NewAddProvisionWatcherRequest(dto dtos.ProvisionWatcher) AddProvisionWatcherRequest {
Expand Down
1 change: 1 addition & 0 deletions dtos/requests/provisionwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ func TestUpdateProvisionWatcherRequest_UnmarshalJSON_NilField(t *testing.T) {
assert.Nil(t, req.ProvisionWatcher.ProfileName)
assert.Nil(t, req.ProvisionWatcher.AdminState)
assert.Nil(t, req.ProvisionWatcher.AutoEvents)
assert.Nil(t, req.ProvisionWatcher.Properties)
}

func TestUpdateProvisionWatcherRequest_UnmarshalJSON_EmptySlice(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions models/provisionwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ type ProvisionWatcher struct {
ServiceName string
AdminState AdminState
AutoEvents []AutoEvent
Properties map[string]any
}

0 comments on commit 4695302

Please sign in to comment.