Skip to content

Commit

Permalink
refactor: Refactor DTO's Created and Modified field to DBTimestamp
Browse files Browse the repository at this point in the history
For reusing the Created and Modified fields, extract these fields to a single struct.
- Rename the Timestamps to DBTimestamp
- Refactor DTO's Created and Modified field to DBTimestamp struct

Close #560

Signed-off-by: weichou <weichou1229@gmail.com>
  • Loading branch information
weichou1229 committed Mar 25, 2021
1 parent 8edfeb6 commit 101c1da
Show file tree
Hide file tree
Showing 19 changed files with 49 additions and 42 deletions.
11 changes: 11 additions & 0 deletions v2/dtos/dbtimestamp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Copyright (C) 2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

package dtos

type DBTimestamp struct {
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
}
3 changes: 1 addition & 2 deletions v2/dtos/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ 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 {
DBTimestamp `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'"`
Expand Down
4 changes: 4 additions & 0 deletions v2/dtos/deviceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// 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 {
DBTimestamp `json:",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"`
Expand All @@ -38,6 +39,7 @@ 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 {
DBTimestamp
Id string `yaml:"id"`
Name string `yaml:"name"`
Manufacturer string `yaml:"manufacturer"`
Expand Down Expand Up @@ -70,6 +72,7 @@ func (dp *DeviceProfile) UnmarshalYAML(unmarshal func(interface{}) error) error
// ToDeviceProfileModel transforms the DeviceProfile DTO to the DeviceProfile model
func ToDeviceProfileModel(deviceProfileDTO DeviceProfile) models.DeviceProfile {
return models.DeviceProfile{
DBTimestamp: models.DBTimestamp(deviceProfileDTO.DBTimestamp),
Id: deviceProfileDTO.Id,
Name: deviceProfileDTO.Name,
Description: deviceProfileDTO.Description,
Expand All @@ -84,6 +87,7 @@ func ToDeviceProfileModel(deviceProfileDTO DeviceProfile) models.DeviceProfile {
// FromDeviceProfileModelToDTO transforms the DeviceProfile Model to the DeviceProfile DTO
func FromDeviceProfileModelToDTO(deviceProfile models.DeviceProfile) DeviceProfile {
return DeviceProfile{
DBTimestamp: DBTimestamp(deviceProfile.DBTimestamp),
Id: deviceProfile.Id,
Name: deviceProfile.Name,
Description: deviceProfile.Description,
Expand Down
3 changes: 1 addition & 2 deletions v2/dtos/deviceservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ 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 {
DBTimestamp `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"`
Expand Down
15 changes: 7 additions & 8 deletions v2/dtos/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ 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 {
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"`
DBTimestamp `json:",inline"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
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
4 changes: 2 additions & 2 deletions v2/dtos/intervalaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ 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 {
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
DBTimestamp `json:",inline"`
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"`
Expand Down Expand Up @@ -53,6 +52,7 @@ 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.DBTimestamp = DBTimestamp(model.DBTimestamp)
dto.Id = model.Id
dto.Name = model.Name
dto.IntervalName = model.IntervalName
Expand Down
9 changes: 3 additions & 6 deletions v2/dtos/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ 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 {
DBTimestamp `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"`
Expand All @@ -43,8 +42,7 @@ func NewNotification(labels []string, category, content, sender, severity string
func ToNotificationModel(n Notification) models.Notification {
var m models.Notification
m.Id = n.Id
m.Created = n.Created
m.Modified = n.Modified
m.DBTimestamp = models.DBTimestamp(n.DBTimestamp)
m.Category = n.Category
m.Labels = n.Labels
m.Content = n.Content
Expand All @@ -68,9 +66,8 @@ func ToNotificationModels(notifications []Notification) []models.Notification {
// FromNotificationModelToDTO transforms the Notification Model to the Notification DTO
func FromNotificationModelToDTO(n models.Notification) Notification {
return Notification{
DBTimestamp: DBTimestamp(n.DBTimestamp),
Id: n.Id,
Created: n.Created,
Modified: n.Modified,
Category: string(n.Category),
Labels: n.Labels,
Content: n.Content,
Expand Down
3 changes: 2 additions & 1 deletion v2/dtos/provisionwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 {
DBTimestamp `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 @@ -40,7 +41,7 @@ type UpdateProvisionWatcher struct {
// ToProvisionWatcherModel transforms the ProvisionWatcher DTO to the ProvisionWatcher model
func ToProvisionWatcherModel(dto ProvisionWatcher) models.ProvisionWatcher {
return models.ProvisionWatcher{
Timestamps: models.Timestamps{},
DBTimestamp: models.DBTimestamp(dto.DBTimestamp),
Id: dto.Id,
Name: dto.Name,
Labels: dto.Labels,
Expand Down
9 changes: 3 additions & 6 deletions v2/dtos/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import (
// Subscription and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/support-notifications/2.x#/Subscription
type Subscription struct {
DBTimestamp `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"`
Channels []Address `json:"channels" validate:"required,gt=0,dive"`
Receiver string `json:"receiver" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Categories []string `json:"categories,omitempty" validate:"required_without=Labels,omitempty,gt=0,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Labels []string `json:"labels,omitempty" validate:"required_without=Categories,omitempty,gt=0,dive,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"`
ResendLimit int64 `json:"resendLimit,omitempty"`
ResendInterval string `json:"resendInterval,omitempty" validate:"omitempty,edgex-dto-frequency"`
Expand All @@ -45,8 +44,7 @@ func ToSubscriptionModel(s Subscription) models.Subscription {
m.Categories = s.Categories
m.Labels = s.Labels
m.Channels = ToAddressModels(s.Channels)
m.Created = s.Created
m.Modified = s.Modified
m.DBTimestamp = models.DBTimestamp(s.DBTimestamp)
m.Description = s.Description
m.Id = s.Id
m.Receiver = s.Receiver
Expand All @@ -68,11 +66,10 @@ func ToSubscriptionModels(subs []Subscription) []models.Subscription {
// FromSubscriptionModelToDTO transforms the Subscription Model to the Subscription DTO
func FromSubscriptionModelToDTO(s models.Subscription) Subscription {
return Subscription{
DBTimestamp: DBTimestamp(s.DBTimestamp),
Categories: s.Categories,
Labels: s.Labels,
Channels: FromAddressModelsToDTOs(s.Channels),
Created: s.Created,
Modified: s.Modified,
Description: s.Description,
Id: s.Id,
Receiver: s.Receiver,
Expand Down
2 changes: 1 addition & 1 deletion v2/models/timestamps.go → v2/models/dbtimestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package models

type Timestamps struct {
type DBTimestamp struct {
Created int64 // Created is a timestamp indicating when the entity was created.
Modified int64 // Modified is a timestamp indicating when the entity was last modified.
}
2 changes: 1 addition & 1 deletion v2/models/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package models
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.x#/Device
// Model fields are same as the DTOs documented by this swagger. Exceptions, if any, are noted below.
type Device struct {
Timestamps
DBTimestamp
Id string
Name string
Description string
Expand Down
2 changes: 1 addition & 1 deletion v2/models/deviceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package models
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.x#/DeviceProfile
// Model fields are same as the DTOs documented by this swagger. Exceptions, if any, are noted below.
type DeviceProfile struct {
Timestamps
DBTimestamp
Description string
Id string
Name string
Expand Down
2 changes: 1 addition & 1 deletion v2/models/deviceservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package models
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.x#/DeviceService
// Model fields are same as the DTOs documented by this swagger. Exceptions, if any, are noted below.
type DeviceService struct {
Timestamps
DBTimestamp
Id string
Name string
Description string
Expand Down
2 changes: 1 addition & 1 deletion v2/models/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package models
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/support-scheduler/2.x#/Interval
// Model fields are same as the DTOs documented by this swagger. Exceptions, if any, are noted below.
type Interval struct {
Timestamps
DBTimestamp
Id string
Name string
Start string
Expand Down
6 changes: 3 additions & 3 deletions v2/models/intervalaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/support-scheduler/2.x#/IntervalAction
// Model fields are same as the DTOs documented by this swagger. Exceptions, if any, are noted below.
type IntervalAction struct {
Timestamps
DBTimestamp
Id string
Name string
IntervalName string
Expand All @@ -24,7 +24,7 @@ type IntervalAction struct {

func (intervalAction *IntervalAction) UnmarshalJSON(b []byte) error {
var alias struct {
Timestamps
DBTimestamp
Id string
Name string
IntervalName string
Expand All @@ -39,7 +39,7 @@ func (intervalAction *IntervalAction) UnmarshalJSON(b []byte) error {
}

*intervalAction = IntervalAction{
Timestamps: alias.Timestamps,
DBTimestamp: alias.DBTimestamp,
Id: alias.Id,
Name: alias.Name,
IntervalName: alias.IntervalName,
Expand Down
4 changes: 2 additions & 2 deletions v2/models/intervalaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func actionWithRESTAddressData() IntervalAction {
return IntervalAction{
Timestamps: Timestamps{},
DBTimestamp: DBTimestamp{},
Id: ExampleUUID,
Name: TestIntervalActionName,
IntervalName: TestIntervalName,
Expand All @@ -33,7 +33,7 @@ func actionWithRESTAddressData() IntervalAction {
}
func actionWithMQTTPubAddressData() IntervalAction {
return IntervalAction{
Timestamps: Timestamps{},
DBTimestamp: DBTimestamp{},
Id: ExampleUUID,
Name: TestIntervalActionName,
IntervalName: TestIntervalName,
Expand Down
2 changes: 1 addition & 1 deletion v2/models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package models
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/support-notifications/2.x#/Notification
// Model fields are same as the DTOs documented by this swagger. Exceptions, if any, are noted below.
type Notification struct {
Timestamps
DBTimestamp
Category string
Content string
ContentType string
Expand Down
2 changes: 1 addition & 1 deletion v2/models/provisionwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package models
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.x#/ProvisionWatcher
// Model fields are same as the DTOs documented by this swagger. Exceptions, if any, are noted below.
type ProvisionWatcher struct {
Timestamps
DBTimestamp
Id string
Name string
Labels []string
Expand Down
6 changes: 3 additions & 3 deletions v2/models/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/support-notifications/2.x#/Subscription
// Model fields are same as the DTOs documented by this swagger. Exceptions, if any, are noted below.
type Subscription struct {
Timestamps
DBTimestamp
Categories []string
Labels []string
Channels []Address
Expand All @@ -29,7 +29,7 @@ type Subscription struct {

func (subscription *Subscription) UnmarshalJSON(b []byte) error {
var alias struct {
Timestamps
DBTimestamp
Categories []string
Labels []string
Channels []interface{}
Expand All @@ -53,7 +53,7 @@ func (subscription *Subscription) UnmarshalJSON(b []byte) error {
}

*subscription = Subscription{
Timestamps: alias.Timestamps,
DBTimestamp: alias.DBTimestamp,
Categories: alias.Categories,
Labels: alias.Labels,
Description: alias.Description,
Expand Down

0 comments on commit 101c1da

Please sign in to comment.