Skip to content

Commit

Permalink
Merge pull request #546 from weichou1229/issue-537-3
Browse files Browse the repository at this point in the history
refactor: Remove ApiVersion from update DTOs
  • Loading branch information
cloudxxx8 authored Mar 16, 2021
2 parents 16d295f + 5ea0556 commit f574ca0
Show file tree
Hide file tree
Showing 24 changed files with 50 additions and 100 deletions.
35 changes: 16 additions & 19 deletions v2/dtos/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package dtos

import (
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"
)

Expand All @@ -33,21 +32,20 @@ type Device struct {
// UpdateDevice and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.x#/UpdateDevice
type UpdateDevice struct {
common.Versionable `json:",inline"`
Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"`
Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Description *string `json:"description,omitempty" validate:"omitempty,edgex-dto-none-empty-string"`
AdminState *string `json:"adminState,omitempty" validate:"omitempty,oneof='LOCKED' 'UNLOCKED'"`
OperatingState *string `json:"operatingState,omitempty" validate:"omitempty,oneof='UP' 'DOWN' 'UNKNOWN'"`
LastConnected *int64 `json:"lastConnected,omitempty"`
LastReported *int64 `json:"lastReported,omitempty"`
ServiceName *string `json:"serviceName,omitempty" validate:"omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
ProfileName *string `json:"profileName,omitempty" validate:"omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Labels []string `json:"labels,omitempty"`
Location interface{} `json:"location,omitempty"`
AutoEvents []AutoEvent `json:"autoEvents,omitempty" validate:"dive"`
Protocols map[string]ProtocolProperties `json:"protocols,omitempty" validate:"omitempty,gt=0"`
Notify *bool `json:"notify,omitempty"`
Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"`
Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Description *string `json:"description,omitempty" validate:"omitempty,edgex-dto-none-empty-string"`
AdminState *string `json:"adminState,omitempty" validate:"omitempty,oneof='LOCKED' 'UNLOCKED'"`
OperatingState *string `json:"operatingState,omitempty" validate:"omitempty,oneof='UP' 'DOWN' 'UNKNOWN'"`
LastConnected *int64 `json:"lastConnected,omitempty"`
LastReported *int64 `json:"lastReported,omitempty"`
ServiceName *string `json:"serviceName,omitempty" validate:"omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
ProfileName *string `json:"profileName,omitempty" validate:"omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Labels []string `json:"labels,omitempty"`
Location interface{} `json:"location,omitempty"`
AutoEvents []AutoEvent `json:"autoEvents,omitempty" validate:"dive"`
Protocols map[string]ProtocolProperties `json:"protocols,omitempty" validate:"omitempty,gt=0"`
Notify *bool `json:"notify,omitempty"`
}

// ToDeviceModel transforms the Device DTO to the Device Model
Expand Down Expand Up @@ -91,9 +89,8 @@ func FromDeviceModelToDTO(d models.Device) Device {
// FromDeviceModelToUpdateDTO transforms the Device Model to the UpdateDevice DTO
func FromDeviceModelToUpdateDTO(d models.Device) UpdateDevice {
dto := UpdateDevice{
Versionable: common.NewVersionable(),
Labels: d.Labels,
Notify: &d.Notify,
Labels: d.Labels,
Notify: &d.Notify,
}
if d.Id != "" {
dto.Id = &d.Id
Expand Down
2 changes: 0 additions & 2 deletions v2/dtos/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package dtos
import (
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/stretchr/testify/assert"
Expand All @@ -17,7 +16,6 @@ import (
func TestFromDeviceModelToUpdateDTO(t *testing.T) {
model := models.Device{}
dto := FromDeviceModelToUpdateDTO(model)
assert.Equal(t, v2.ApiVersion, dto.ApiVersion)
assert.Nil(t, dto.Id)
assert.Nil(t, dto.Name)
assert.Nil(t, dto.Description)
Expand Down
15 changes: 6 additions & 9 deletions v2/dtos/deviceservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package dtos

import (
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"
)

Expand All @@ -28,12 +27,11 @@ type DeviceService struct {
// UpdateDeviceService and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.x#/UpdateDeviceService
type UpdateDeviceService struct {
common.Versionable `json:",inline"`
Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"`
Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
BaseAddress *string `json:"baseAddress" validate:"omitempty,uri"`
Labels []string `json:"labels"`
AdminState *string `json:"adminState" validate:"omitempty,oneof='LOCKED' 'UNLOCKED'"`
Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"`
Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
BaseAddress *string `json:"baseAddress" validate:"omitempty,uri"`
Labels []string `json:"labels"`
AdminState *string `json:"adminState" validate:"omitempty,oneof='LOCKED' 'UNLOCKED'"`
}

// ToDeviceServiceModel transforms the DeviceService DTO to the DeviceService Model
Expand Down Expand Up @@ -67,8 +65,7 @@ func FromDeviceServiceModelToDTO(ds models.DeviceService) DeviceService {
// FromDeviceServiceModelToUpdateDTO transforms the DeviceService Model to the UpdateDeviceService DTO
func FromDeviceServiceModelToUpdateDTO(ds models.DeviceService) UpdateDeviceService {
dto := UpdateDeviceService{
Versionable: common.NewVersionable(),
Labels: ds.Labels,
Labels: ds.Labels,
}
if ds.Id != "" {
dto.Id = &ds.Id
Expand Down
2 changes: 0 additions & 2 deletions v2/dtos/deviceservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package dtos
import (
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/stretchr/testify/assert"
Expand All @@ -17,7 +16,6 @@ import (
func TestFromDeviceServiceModelToUpdateDTO(t *testing.T) {
model := models.DeviceService{}
dto := FromDeviceServiceModelToUpdateDTO(model)
assert.Equal(t, v2.ApiVersion, dto.ApiVersion)
assert.Nil(t, dto.Id)
assert.Nil(t, dto.Name)
assert.Nil(t, dto.Labels)
Expand Down
4 changes: 0 additions & 4 deletions v2/dtos/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func TestNewEvent(t *testing.T) {
}

func TestEvent_AddSimpleReading(t *testing.T) {
expectedApiVersion := v2.ApiVersion
expectedDeviceName := TestDeviceName
expectedProfileName := TestDeviceProfileName
expectedSourceName := TestSourceName
Expand All @@ -121,7 +120,6 @@ func TestEvent_AddSimpleReading(t *testing.T) {
require.Equal(t, expectedReadingsCount, len(target.Readings))

for index, actual := range target.Readings {
assert.Equal(t, expectedApiVersion, actual.ApiVersion)
assert.NotEmpty(t, actual.Id)
assert.Equal(t, expectedProfileName, actual.ProfileName)
assert.Equal(t, expectedDeviceName, actual.DeviceName)
Expand All @@ -134,7 +132,6 @@ func TestEvent_AddSimpleReading(t *testing.T) {
}

func TestEvent_AddBinaryReading(t *testing.T) {
expectedApiVersion := v2.ApiVersion
expectedDeviceName := TestDeviceName
expectedProfileName := TestDeviceProfileName
expectedSourceName := TestSourceName
Expand All @@ -149,7 +146,6 @@ func TestEvent_AddBinaryReading(t *testing.T) {

require.Equal(t, expectedReadingsCount, len(target.Readings))
actual := target.Readings[0]
assert.Equal(t, expectedApiVersion, actual.ApiVersion)
assert.NotEmpty(t, actual.Id)
assert.Equal(t, expectedProfileName, actual.ProfileName)
assert.Equal(t, expectedDeviceName, actual.DeviceName)
Expand Down
14 changes: 6 additions & 8 deletions v2/dtos/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package dtos

import (
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"
)

Expand All @@ -26,13 +25,12 @@ type Interval struct {
// UpdateInterval and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/support-scheduler/2.x#/UpdateInterval
type UpdateInterval struct {
common.Versionable `json:",inline"`
Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"`
Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Start *string `json:"start,omitempty" validate:"omitempty,edgex-dto-interval-datetime"`
End *string `json:"end,omitempty" validate:"omitempty,edgex-dto-interval-datetime"`
Frequency *string `json:"frequency,omitempty" validate:"omitempty,edgex-dto-frequency"`
RunOnce *bool `json:"runOnce,omitempty"`
Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"`
Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Start *string `json:"start,omitempty" validate:"omitempty,edgex-dto-interval-datetime"`
End *string `json:"end,omitempty" validate:"omitempty,edgex-dto-interval-datetime"`
Frequency *string `json:"frequency,omitempty" validate:"omitempty,edgex-dto-frequency"`
RunOnce *bool `json:"runOnce,omitempty"`
}

// ToIntervalModel transforms the Interval DTO to the Interval Model
Expand Down
10 changes: 4 additions & 6 deletions v2/dtos/intervalaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package dtos

import (
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/google/uuid"
Expand Down Expand Up @@ -35,11 +34,10 @@ func NewIntervalAction(name string, intervalName string, address Address) Interv
// UpdateIntervalAction and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/support-scheduler/2.x#/UpdateIntervalAction
type UpdateIntervalAction struct {
common.Versionable `json:",inline"`
Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"`
Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
IntervalName *string `json:"intervalName" validate:"edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Address *Address `json:"address,omitempty"`
Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"`
Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
IntervalName *string `json:"intervalName" validate:"edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Address *Address `json:"address,omitempty"`
}

// ToIntervalActionModel transforms the IntervalAction DTO to the IntervalAction Model
Expand Down
3 changes: 0 additions & 3 deletions v2/dtos/provisionwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package dtos

import (
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"
)

Expand All @@ -27,7 +26,6 @@ type ProvisionWatcher struct {
// UpdateProvisionWatcher and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.x#/UpdateProvisionWatcher
type UpdateProvisionWatcher struct {
common.Versionable `json:",inline"`
Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"`
Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Labels []string `json:"labels"`
Expand Down Expand Up @@ -73,7 +71,6 @@ func FromProvisionWatcherModelToDTO(pw models.ProvisionWatcher) ProvisionWatcher
// FromProvisionWatcherModelToUpdateDTO transforms the ProvisionWatcher Model to the UpdateProvisionWatcher DTO
func FromProvisionWatcherModelToUpdateDTO(pw models.ProvisionWatcher) UpdateProvisionWatcher {
dto := UpdateProvisionWatcher{
Versionable: common.NewVersionable(),
Labels: pw.Labels,
Identifiers: pw.Identifiers,
BlockingIdentifiers: pw.BlockingIdentifiers,
Expand Down
2 changes: 0 additions & 2 deletions v2/dtos/provisionwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package dtos
import (
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/stretchr/testify/assert"
Expand All @@ -17,7 +16,6 @@ import (
func TestFromProvisionWatcherModelToUpdateDTO(t *testing.T) {
model := models.ProvisionWatcher{}
dto := FromProvisionWatcherModelToUpdateDTO(model)
assert.Equal(t, v2.ApiVersion, dto.ApiVersion)
assert.Nil(t, dto.Id)
assert.Nil(t, dto.Name)
assert.Nil(t, dto.Labels)
Expand Down
23 changes: 9 additions & 14 deletions v2/dtos/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,21 @@ import (
"github.com/google/uuid"

"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"
)

// BaseReading and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-data/2.x#/BaseReading
type BaseReading struct {
common.Versionable `json:",inline"`
Id string `json:"id,omitempty"`
Created int64 `json:"created,omitempty"`
Origin int64 `json:"origin" validate:"required"`
DeviceName string `json:"deviceName" validate:"required,edgex-dto-rfc3986-unreserved-chars"`
ResourceName string `json:"resourceName" validate:"required,edgex-dto-rfc3986-unreserved-chars"`
ProfileName string `json:"profileName" validate:"required,edgex-dto-rfc3986-unreserved-chars"`
ValueType string `json:"valueType" validate:"required,edgex-dto-value-type"`
BinaryReading `json:",inline" validate:"-"`
SimpleReading `json:",inline" validate:"-"`
Id string `json:"id,omitempty"`
Created int64 `json:"created,omitempty"`
Origin int64 `json:"origin" validate:"required"`
DeviceName string `json:"deviceName" validate:"required,edgex-dto-rfc3986-unreserved-chars"`
ResourceName string `json:"resourceName" validate:"required,edgex-dto-rfc3986-unreserved-chars"`
ProfileName string `json:"profileName" validate:"required,edgex-dto-rfc3986-unreserved-chars"`
ValueType string `json:"valueType" validate:"required,edgex-dto-value-type"`
BinaryReading `json:",inline" validate:"-"`
SimpleReading `json:",inline" validate:"-"`
}

// SimpleReading and its properties are defined in the APIv2 specification:
Expand All @@ -48,7 +46,6 @@ type BinaryReading struct {

func newBaseReading(profileName string, deviceName string, resourceName string, valueType string) BaseReading {
return BaseReading{
Versionable: common.NewVersionable(),
Id: uuid.NewString(),
Origin: time.Now().UnixNano(),
DeviceName: deviceName,
Expand Down Expand Up @@ -293,7 +290,6 @@ func FromReadingModelToDTO(reading models.Reading) BaseReading {
switch r := reading.(type) {
case models.BinaryReading:
baseReading = BaseReading{
Versionable: common.NewVersionable(),
Id: r.Id,
Created: r.Created,
Origin: r.Origin,
Expand All @@ -305,7 +301,6 @@ func FromReadingModelToDTO(reading models.Reading) BaseReading {
}
case models.SimpleReading:
baseReading = BaseReading{
Versionable: common.Versionable{ApiVersion: v2.ApiVersion},
Id: r.Id,
Created: r.Created,
Origin: r.Origin,
Expand Down
6 changes: 0 additions & 6 deletions v2/dtos/reading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/stretchr/testify/require"

v2 "github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -68,7 +67,6 @@ func TestFromReadingModelToDTO(t *testing.T) {
Value: TestValue,
}
expectedDTO := BaseReading{
Versionable: common.Versionable{ApiVersion: v2.ApiVersion},
Id: TestUUID,
Created: TestTimestamp,
Origin: TestTimestamp,
Expand Down Expand Up @@ -96,7 +94,6 @@ func TestFromReadingModelToDTO(t *testing.T) {
}

func TestNewSimpleReading(t *testing.T) {
expectedApiVersion := v2.ApiVersion
expectedDeviceName := TestDeviceName
expectedProfileName := TestDeviceProfileName
expectedResourceName := TestDeviceResourceName
Expand Down Expand Up @@ -138,7 +135,6 @@ func TestNewSimpleReading(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
actual, err := NewSimpleReading(expectedProfileName, expectedDeviceName, expectedResourceName, tt.expectedValueType, tt.value)
require.NoError(t, err)
assert.Equal(t, expectedApiVersion, actual.ApiVersion)
assert.NotEmpty(t, actual.Id)
assert.Equal(t, expectedProfileName, actual.ProfileName)
assert.Equal(t, expectedDeviceName, actual.DeviceName)
Expand Down Expand Up @@ -192,7 +188,6 @@ func TestNewSimpleReadingError(t *testing.T) {
}

func TestNewBinaryReading(t *testing.T) {
expectedApiVersion := v2.ApiVersion
expectedDeviceName := TestDeviceName
expectedProfileName := TestDeviceProfileName
expectedResourceName := TestDeviceResourceName
Expand All @@ -203,7 +198,6 @@ func TestNewBinaryReading(t *testing.T) {

actual := NewBinaryReading(expectedProfileName, expectedDeviceName, expectedResourceName, expectedBinaryValue, expectedMediaType)

assert.Equal(t, expectedApiVersion, actual.ApiVersion)
assert.NotEmpty(t, actual.Id)
assert.Equal(t, expectedProfileName, actual.ProfileName)
assert.Equal(t, expectedDeviceName, actual.DeviceName)
Expand Down
1 change: 0 additions & 1 deletion v2/dtos/requests/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func NewAddDeviceRequest(dto dtos.Device) AddDeviceRequest {
}

func NewUpdateDeviceRequest(dto dtos.UpdateDevice) UpdateDeviceRequest {
dto.Versionable = common.NewVersionable()
return UpdateDeviceRequest{
BaseRequest: common.NewBaseRequest(),
Device: dto,
Expand Down
2 changes: 0 additions & 2 deletions v2/dtos/requests/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ func mockUpdateDevice() dtos.UpdateDevice {
testDeviceServiceName := TestDeviceServiceName
testProfileName := TestDeviceProfileName
d := dtos.UpdateDevice{}
d.Versionable = common.NewVersionable()
d.Id = &testId
d.Name = &testName
d.Description = &testDescription
Expand Down Expand Up @@ -445,5 +444,4 @@ func TestNewUpdateDeviceRequest(t *testing.T) {
actual := NewUpdateDeviceRequest(dtos.UpdateDevice{})

assert.Equal(t, expectedApiVersion, actual.ApiVersion)
assert.Equal(t, expectedApiVersion, actual.Device.ApiVersion)
}
1 change: 0 additions & 1 deletion v2/dtos/requests/deviceservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func NewAddDeviceServiceRequest(dto dtos.DeviceService) AddDeviceServiceRequest
}

func NewUpdateDeviceServiceRequest(dto dtos.UpdateDeviceService) UpdateDeviceServiceRequest {
dto.Versionable = common.NewVersionable()
return UpdateDeviceServiceRequest{
BaseRequest: common.NewBaseRequest(),
Service: dto,
Expand Down
2 changes: 0 additions & 2 deletions v2/dtos/requests/deviceservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func mockDeviceServiceDTO() dtos.UpdateDeviceService {
testBaseAddress := TestBaseAddress
testAdminState := models.Locked
ds := dtos.UpdateDeviceService{}
ds.Versionable = common.NewVersionable()
ds.Id = &testUUID
ds.Name = &testName
ds.BaseAddress = &testBaseAddress
Expand Down Expand Up @@ -326,5 +325,4 @@ func TestNewUpdateDeviceServiceRequest(t *testing.T) {
actual := NewUpdateDeviceServiceRequest(dtos.UpdateDeviceService{})

assert.Equal(t, expectedApiVersion, actual.ApiVersion)
assert.Equal(t, expectedApiVersion, actual.Service.ApiVersion)
}
Loading

0 comments on commit f574ca0

Please sign in to comment.