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

refactor: Remove ApiVersion from normal DTOs except EventDTO #538

Merged
merged 1 commit into from
Mar 15, 2021
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
32 changes: 15 additions & 17 deletions v2/dtos/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ import (
// Device and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.x#/Device
type Device struct {
common.Versionable `json:",inline"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Name string `json:"name" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Description string `json:"description,omitempty"`
AdminState string `json:"adminState" validate:"oneof='LOCKED' 'UNLOCKED'"`
OperatingState string `json:"operatingState" validate:"oneof='UP' 'DOWN' 'UNKNOWN'"`
LastConnected int64 `json:"lastConnected,omitempty"`
LastReported int64 `json:"lastReported,omitempty"`
Labels []string `json:"labels,omitempty"`
Location interface{} `json:"location,omitempty"`
ServiceName string `json:"serviceName" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
ProfileName string `json:"profileName" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
AutoEvents []AutoEvent `json:"autoEvents,omitempty" validate:"dive"`
Protocols map[string]ProtocolProperties `json:"protocols" validate:"required,gt=0"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Name string `json:"name" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Description string `json:"description,omitempty"`
AdminState string `json:"adminState" validate:"oneof='LOCKED' 'UNLOCKED'"`
OperatingState string `json:"operatingState" validate:"oneof='UP' 'DOWN' 'UNKNOWN'"`
LastConnected int64 `json:"lastConnected,omitempty"`
LastReported int64 `json:"lastReported,omitempty"`
Labels []string `json:"labels,omitempty"`
Location interface{} `json:"location,omitempty"`
ServiceName string `json:"serviceName" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
ProfileName string `json:"profileName" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
AutoEvents []AutoEvent `json:"autoEvents,omitempty" validate:"dive"`
Protocols map[string]ProtocolProperties `json:"protocols" validate:"required,gt=0"`
}

// UpdateDevice and its properties are defined in the APIv2 specification:
Expand Down Expand Up @@ -73,7 +72,6 @@ func ToDeviceModel(dto Device) models.Device {
// FromDeviceModelToDTO transforms the Device Model to the Device DTO
func FromDeviceModelToDTO(d models.Device) Device {
var dto Device
dto.Versionable = common.NewVersionable()
dto.Id = d.Id
dto.Name = d.Name
dto.Description = d.Description
Expand Down
36 changes: 16 additions & 20 deletions v2/dtos/deviceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@ import (

"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
"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"
)

// DeviceProfile and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.x#/DeviceProfile
type DeviceProfile struct {
common.Versionable `json:",inline" yaml:",inline"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Name string `json:"name" yaml:"name" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Manufacturer string `json:"manufacturer,omitempty" yaml:"manufacturer,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Model string `json:"model,omitempty" yaml:"model,omitempty"`
Labels []string `json:"labels,omitempty" yaml:"labels,flow,omitempty"`
DeviceResources []DeviceResource `json:"deviceResources" yaml:"deviceResources" validate:"required,gt=0,dive"`
DeviceCommands []DeviceCommand `json:"deviceCommands,omitempty" yaml:"deviceCommands,omitempty" validate:"dive"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Name string `json:"name" yaml:"name" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Manufacturer string `json:"manufacturer,omitempty" yaml:"manufacturer,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Model string `json:"model,omitempty" yaml:"model,omitempty"`
Labels []string `json:"labels,omitempty" yaml:"labels,flow,omitempty"`
DeviceResources []DeviceResource `json:"deviceResources" yaml:"deviceResources" validate:"required,gt=0,dive"`
DeviceCommands []DeviceCommand `json:"deviceCommands,omitempty" yaml:"deviceCommands,omitempty" validate:"dive"`
}

// Validate satisfies the Validator interface
Expand All @@ -40,15 +38,14 @@ func (dp *DeviceProfile) Validate() error {
// UnmarshalYAML implements the Unmarshaler interface for the DeviceProfile type
func (dp *DeviceProfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
var alias struct {
common.Versionable `yaml:",inline"`
Id string `yaml:"id"`
Name string `yaml:"name"`
Manufacturer string `yaml:"manufacturer"`
Description string `yaml:"description"`
Model string `yaml:"model"`
Labels []string `yaml:"labels"`
DeviceResources []DeviceResource `yaml:"deviceResources"`
DeviceCommands []DeviceCommand `yaml:"deviceCommands"`
Id string `yaml:"id"`
Name string `yaml:"name"`
Manufacturer string `yaml:"manufacturer"`
Description string `yaml:"description"`
Model string `yaml:"model"`
Labels []string `yaml:"labels"`
DeviceResources []DeviceResource `yaml:"deviceResources"`
DeviceCommands []DeviceCommand `yaml:"deviceCommands"`
}
if err := unmarshal(&alias); err != nil {
return errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to unmarshal request body as YAML.", err)
Expand Down Expand Up @@ -87,7 +84,6 @@ func ToDeviceProfileModel(deviceProfileDTO DeviceProfile) models.DeviceProfile {
// FromDeviceProfileModelToDTO transforms the DeviceProfile Model to the DeviceProfile DTO
func FromDeviceProfileModelToDTO(deviceProfile models.DeviceProfile) DeviceProfile {
return DeviceProfile{
Versionable: common.NewVersionable(),
Id: deviceProfile.Id,
Name: deviceProfile.Name,
Description: deviceProfile.Description,
Expand Down
2 changes: 0 additions & 2 deletions v2/dtos/deviceprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"testing"

"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 @@ -49,7 +48,6 @@ var testDeviceProfile = models.DeviceProfile{

func profileData() DeviceProfile {
return DeviceProfile{
Versionable: common.NewVersionable(),
Name: TestDeviceProfileName,
Manufacturer: TestManufacturer,
Description: TestDescription,
Expand Down
22 changes: 10 additions & 12 deletions v2/dtos/deviceservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ import (
// DeviceService and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.x#/DeviceService
type DeviceService struct {
common.Versionable `json:",inline"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Name string `json:"name" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Description string `json:"description,omitempty"`
LastConnected int64 `json:"lastConnected,omitempty"`
LastReported int64 `json:"lastReported,omitempty"`
Labels []string `json:"labels,omitempty"`
BaseAddress string `json:"baseAddress" validate:"required,uri"`
AdminState string `json:"adminState" validate:"oneof='LOCKED' 'UNLOCKED'"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Name string `json:"name" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Description string `json:"description,omitempty"`
LastConnected int64 `json:"lastConnected,omitempty"`
LastReported int64 `json:"lastReported,omitempty"`
Labels []string `json:"labels,omitempty"`
BaseAddress string `json:"baseAddress" validate:"required,uri"`
AdminState string `json:"adminState" validate:"oneof='LOCKED' 'UNLOCKED'"`
}

// UpdateDeviceService and its properties are defined in the APIv2 specification:
Expand Down Expand Up @@ -54,7 +53,6 @@ func ToDeviceServiceModel(dto DeviceService) models.DeviceService {
// FromDeviceServiceModelToDTO transforms the DeviceService Model to the DeviceService DTO
func FromDeviceServiceModelToDTO(ds models.DeviceService) DeviceService {
var dto DeviceService
dto.Versionable = common.NewVersionable()
dto.Id = ds.Id
dto.Name = ds.Name
dto.Description = ds.Description
Expand Down
18 changes: 8 additions & 10 deletions v2/dtos/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import (
// Interval and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/support-scheduler/2.x#/Interval
type Interval struct {
common.Versionable `json:",inline"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Name string `json:"name" validate:"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" validate:"required,edgex-dto-frequency"`
RunOnce bool `json:"runOnce,omitempty"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Name string `json:"name" validate:"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" validate:"required,edgex-dto-frequency"`
RunOnce bool `json:"runOnce,omitempty"`
}

// UpdateInterval and its properties are defined in the APIv2 specification:
Expand Down Expand Up @@ -51,7 +50,6 @@ func ToIntervalModel(dto Interval) models.Interval {
// FromIntervalModelToDTO transforms the Interval Model to the Interval DTO
func FromIntervalModelToDTO(model models.Interval) Interval {
var dto Interval
dto.Versionable = common.NewVersionable()
dto.Id = model.Id
dto.Name = model.Name
dto.Start = model.Start
Expand Down
15 changes: 6 additions & 9 deletions v2/dtos/intervalaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ import (
// IntervalAction and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/support-scheduler/2.x#/IntervalAction
type IntervalAction struct {
common.Versionable `json:",inline"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Name string `json:"name" validate:"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" validate:"required"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Name string `json:"name" validate:"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" validate:"required"`
}

func NewIntervalAction(name string, intervalName string, address Address) IntervalAction {
return IntervalAction{
Versionable: common.NewVersionable(),
Id: uuid.NewString(),
Name: name,
IntervalName: intervalName,
Expand Down Expand Up @@ -57,7 +55,6 @@ func ToIntervalActionModel(dto IntervalAction) models.IntervalAction {
// FromIntervalActionModelToDTO transforms the IntervalAction Model to the IntervalAction DTO
func FromIntervalActionModelToDTO(model models.IntervalAction) IntervalAction {
var dto IntervalAction
dto.Versionable = common.NewVersionable()
dto.Id = model.Id
dto.Name = model.Name
dto.IntervalName = model.IntervalName
Expand Down
38 changes: 17 additions & 21 deletions v2/dtos/notification.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 All @@ -15,30 +14,28 @@ import (
// Notification and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/support-notifications/2.x#/Notification
type Notification struct {
common.Versionable `json:",inline"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Category string `json:"category,omitempty" validate:"required_without=Labels,omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Labels []string `json:"labels,omitempty" validate:"required_without=Category,omitempty,gt=0,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Content string `json:"content" validate:"required,edgex-dto-none-empty-string"`
ContentType string `json:"contentType,omitempty"`
Description string `json:"description,omitempty"`
Sender string `json:"sender" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Severity string `json:"severity" validate:"required,oneof='MINOR' 'NORMAL' 'CRITICAL'"`
Status string `json:"status,omitempty" validate:"omitempty,oneof='NEW' 'PROCESSED' 'ESCALATED'"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Category string `json:"category,omitempty" validate:"required_without=Labels,omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Labels []string `json:"labels,omitempty" validate:"required_without=Category,omitempty,gt=0,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Content string `json:"content" validate:"required,edgex-dto-none-empty-string"`
ContentType string `json:"contentType,omitempty"`
Description string `json:"description,omitempty"`
Sender string `json:"sender" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Severity string `json:"severity" validate:"required,oneof='MINOR' 'NORMAL' 'CRITICAL'"`
Status string `json:"status,omitempty" validate:"omitempty,oneof='NEW' 'PROCESSED' 'ESCALATED'"`
}

// NewNotification creates and returns a Notification DTO
func NewNotification(labels []string, category, content, sender, severity string) Notification {
return Notification{
Versionable: common.NewVersionable(),
Id: uuid.NewString(),
Labels: labels,
Category: category,
Content: content,
Sender: sender,
Severity: severity,
Id: uuid.NewString(),
Labels: labels,
Category: category,
Content: content,
Sender: sender,
Severity: severity,
}
}

Expand Down Expand Up @@ -71,7 +68,6 @@ func ToNotificationModels(notifications []Notification) []models.Notification {
// FromNotificationModelToDTO transforms the Notification Model to the Notification DTO
func FromNotificationModelToDTO(n models.Notification) Notification {
return Notification{
Versionable: common.NewVersionable(),
Id: n.Id,
Created: n.Created,
Modified: n.Modified,
Expand Down
3 changes: 0 additions & 3 deletions v2/dtos/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ 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"
)

func TestNewNotification(t *testing.T) {
expectedApiVersion := v2.ApiVersion
expectedLabels := []string{"label1", "label2"}
expectedCategory := "category"
expectedContent := "content"
Expand All @@ -19,7 +17,6 @@ func TestNewNotification(t *testing.T) {

actual := NewNotification(expectedLabels, expectedCategory, expectedContent, expectedSender, expectedSeverity)

assert.Equal(t, expectedApiVersion, actual.ApiVersion)
assert.NotEmpty(t, actual.Id)
assert.Equal(t, expectedLabels, actual.Labels)
assert.Equal(t, expectedCategory, actual.Category)
Expand Down
2 changes: 0 additions & 2 deletions v2/dtos/provisionwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
// ProvisionWatcher and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.x#/ProvisionWatcher
type ProvisionWatcher struct {
common.Versionable `json:",inline"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Name string `json:"name" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Labels []string `json:"labels,omitempty"`
Expand Down Expand Up @@ -59,7 +58,6 @@ func ToProvisionWatcherModel(dto ProvisionWatcher) models.ProvisionWatcher {
// FromProvisionWatcherModelToDTO transforms the ProvisionWatcher Model to the ProvisionWatcher DTO
func FromProvisionWatcherModelToDTO(pw models.ProvisionWatcher) ProvisionWatcher {
return ProvisionWatcher{
Versionable: common.NewVersionable(),
Id: pw.Id,
Name: pw.Name,
Labels: pw.Labels,
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 @@ -131,7 +131,6 @@ func ReplaceDeviceModelFieldsWithDTO(device *models.Device, patch dtos.UpdateDev
}

func NewAddDeviceRequest(dto dtos.Device) AddDeviceRequest {
dto.Versionable = common.NewVersionable()
return AddDeviceRequest{
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 @@ -41,7 +41,6 @@ var testAddDevice = AddDeviceRequest{
Versionable: common.NewVersionable(),
},
Device: dtos.Device{
Versionable: common.NewVersionable(),
Name: TestDeviceName,
ServiceName: TestDeviceServiceName,
ProfileName: TestDeviceProfileName,
Expand Down Expand Up @@ -438,7 +437,6 @@ func TestNewAddDeviceRequest(t *testing.T) {
actual := NewAddDeviceRequest(dtos.Device{})

assert.Equal(t, expectedApiVersion, actual.ApiVersion)
assert.Equal(t, expectedApiVersion, actual.Device.ApiVersion)
}

func TestNewUpdateDeviceRequest(t *testing.T) {
Expand Down
Loading