dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/requests/deviceprofile.go (about) 1 // 2 // Copyright (C) 2020-2021 IOTech Ltd 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 6 package requests 7 8 import ( 9 "encoding/json" 10 "errors" 11 "strings" 12 13 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common" 14 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos" 15 dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common" 16 edgexErrors "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors" 17 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models" 18 ) 19 20 // DeviceProfileRequest defines the Request Content for POST DeviceProfile DTO. 21 type DeviceProfileRequest struct { 22 dtoCommon.BaseRequest `json:",inline"` 23 Profile dtos.DeviceProfile `json:"profile"` 24 } 25 26 // Validate satisfies the Validator interface 27 func (dp DeviceProfileRequest) Validate() error { 28 err := common.Validate(dp) 29 if err != nil { 30 // The DeviceProfileBasicInfo is the internal struct in Golang programming, not in the Profile model, 31 // so it should be hidden from the error messages. 32 err = errors.New(strings.ReplaceAll(err.Error(), ".DeviceProfileBasicInfo", "")) 33 return edgexErrors.NewCommonEdgeX(edgexErrors.KindContractInvalid, "", err) 34 } 35 return dp.Profile.Validate() 36 } 37 38 // UnmarshalJSON implements the Unmarshaler interface for the DeviceProfileRequest type 39 func (dp *DeviceProfileRequest) UnmarshalJSON(b []byte) error { 40 var alias struct { 41 dtoCommon.BaseRequest 42 Profile dtos.DeviceProfile 43 } 44 if err := json.Unmarshal(b, &alias); err != nil { 45 return edgexErrors.NewCommonEdgeX(edgexErrors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err) 46 } 47 48 *dp = DeviceProfileRequest(alias) 49 50 // validate DeviceProfileRequest DTO 51 if err := dp.Validate(); err != nil { 52 return err 53 } 54 55 // Normalize resource's value type 56 for i, resource := range dp.Profile.DeviceResources { 57 valueType, err := common.NormalizeValueType(resource.Properties.ValueType) 58 if err != nil { 59 return edgexErrors.NewCommonEdgeXWrapper(err) 60 } 61 dp.Profile.DeviceResources[i].Properties.ValueType = valueType 62 } 63 return nil 64 } 65 66 // DeviceProfileReqToDeviceProfileModel transforms the DeviceProfileRequest DTO to the DeviceProfile model 67 func DeviceProfileReqToDeviceProfileModel(addReq DeviceProfileRequest) (DeviceProfiles models.DeviceProfile) { 68 return dtos.ToDeviceProfileModel(addReq.Profile) 69 } 70 71 // DeviceProfileReqToDeviceProfileModels transforms the DeviceProfileRequest DTO array to the DeviceProfile model array 72 func DeviceProfileReqToDeviceProfileModels(addRequests []DeviceProfileRequest) (DeviceProfiles []models.DeviceProfile) { 73 for _, req := range addRequests { 74 dp := DeviceProfileReqToDeviceProfileModel(req) 75 DeviceProfiles = append(DeviceProfiles, dp) 76 } 77 return DeviceProfiles 78 } 79 80 func NewDeviceProfileRequest(dto dtos.DeviceProfile) DeviceProfileRequest { 81 return DeviceProfileRequest{ 82 BaseRequest: dtoCommon.NewBaseRequest(), 83 Profile: dto, 84 } 85 }