dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/subscription.go (about) 1 // 2 // Copyright (C) 2020-2021 IOTech Ltd 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 6 package dtos 7 8 import ( 9 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models" 10 ) 11 12 type Subscription struct { 13 DBTimestamp `json:",inline"` 14 Id string `json:"id,omitempty" validate:"omitempty,uuid"` 15 Name string `json:"name" validate:"required,edgex-dto-none-empty-string"` 16 Channels []Address `json:"channels" validate:"required,gt=0,dive"` 17 Receiver string `json:"receiver" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"` 18 Categories []string `json:"categories,omitempty" validate:"required_without=Labels,omitempty,gt=0,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"` 19 Labels []string `json:"labels,omitempty" validate:"required_without=Categories,omitempty,gt=0,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"` 20 Description string `json:"description,omitempty"` 21 ResendLimit int `json:"resendLimit,omitempty"` 22 ResendInterval string `json:"resendInterval,omitempty" validate:"omitempty,edgex-dto-duration"` 23 AdminState string `json:"adminState" validate:"oneof='LOCKED' 'UNLOCKED'"` 24 } 25 26 type UpdateSubscription struct { 27 Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"` 28 Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string"` 29 Channels []Address `json:"channels" validate:"omitempty,gt=0,dive"` 30 Receiver *string `json:"receiver" validate:"omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"` 31 Categories []string `json:"categories" validate:"omitempty,dive,gt=0,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"` 32 Labels []string `json:"labels" validate:"omitempty,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"` 33 Description *string `json:"description"` 34 ResendLimit *int `json:"resendLimit"` 35 ResendInterval *string `json:"resendInterval" validate:"omitempty,edgex-dto-duration"` 36 AdminState *string `json:"adminState" validate:"omitempty,oneof='LOCKED' 'UNLOCKED'"` 37 } 38 39 // ToSubscriptionModel transforms the Subscription DTO to the Subscription Model 40 func ToSubscriptionModel(s Subscription) models.Subscription { 41 var m models.Subscription 42 m.Categories = s.Categories 43 m.Labels = s.Labels 44 m.Channels = ToAddressModels(s.Channels) 45 m.DBTimestamp = models.DBTimestamp(s.DBTimestamp) 46 m.Description = s.Description 47 m.Id = s.Id 48 m.Receiver = s.Receiver 49 m.Name = s.Name 50 m.ResendLimit = s.ResendLimit 51 m.ResendInterval = s.ResendInterval 52 m.AdminState = models.AdminState(s.AdminState) 53 return m 54 } 55 56 // ToSubscriptionModels transforms the Subscription DTO array to the Subscription model array 57 func ToSubscriptionModels(subs []Subscription) []models.Subscription { 58 models := make([]models.Subscription, len(subs)) 59 for i, s := range subs { 60 models[i] = ToSubscriptionModel(s) 61 } 62 return models 63 } 64 65 // FromSubscriptionModelToDTO transforms the Subscription Model to the Subscription DTO 66 func FromSubscriptionModelToDTO(s models.Subscription) Subscription { 67 return Subscription{ 68 DBTimestamp: DBTimestamp(s.DBTimestamp), 69 Categories: s.Categories, 70 Labels: s.Labels, 71 Channels: FromAddressModelsToDTOs(s.Channels), 72 Description: s.Description, 73 Id: s.Id, 74 Receiver: s.Receiver, 75 Name: s.Name, 76 ResendLimit: s.ResendLimit, 77 ResendInterval: s.ResendInterval, 78 AdminState: string(s.AdminState), 79 } 80 } 81 82 // FromSubscriptionModelsToDTOs transforms the Subscription model array to the Subscription DTO array 83 func FromSubscriptionModelsToDTOs(subscruptions []models.Subscription) []Subscription { 84 dtos := make([]Subscription, len(subscruptions)) 85 for i, s := range subscruptions { 86 dtos[i] = FromSubscriptionModelToDTO(s) 87 } 88 return dtos 89 }