dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/requests/devicecommand.go (about)

     1  //
     2  // Copyright (C) 2022 IOTech Ltd
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  
     6  package requests
     7  
     8  import (
     9  	"encoding/json"
    10  
    11  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    12  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos"
    13  	dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
    14  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    15  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models"
    16  )
    17  
    18  // AddDeviceCommandRequest defines the Request Content for POST DeviceCommand DTO.
    19  type AddDeviceCommandRequest struct {
    20  	dtoCommon.BaseRequest `json:",inline"`
    21  	ProfileName           string             `json:"profileName" validate:"required,edgex-dto-none-empty-string"`
    22  	DeviceCommand         dtos.DeviceCommand `json:"deviceCommand"`
    23  }
    24  
    25  // Validate satisfies the Validator interface
    26  func (request AddDeviceCommandRequest) Validate() error {
    27  	err := common.Validate(request)
    28  	return err
    29  }
    30  
    31  // UnmarshalJSON implements the Unmarshaler interface for the AddDeviceCommandRequest type
    32  func (dc *AddDeviceCommandRequest) UnmarshalJSON(b []byte) error {
    33  	alias := struct {
    34  		dtoCommon.BaseRequest
    35  		ProfileName   string
    36  		DeviceCommand dtos.DeviceCommand
    37  	}{}
    38  
    39  	if err := json.Unmarshal(b, &alias); err != nil {
    40  		return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err)
    41  	}
    42  	*dc = AddDeviceCommandRequest(alias)
    43  
    44  	if err := dc.Validate(); err != nil {
    45  		return err
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  // UpdateDeviceCommandRequest defines the Request Content for PATCH DeviceCommand DTO.
    52  type UpdateDeviceCommandRequest struct {
    53  	dtoCommon.BaseRequest `json:",inline"`
    54  	ProfileName           string                   `json:"profileName" validate:"required,edgex-dto-none-empty-string"`
    55  	DeviceCommand         dtos.UpdateDeviceCommand `json:"deviceCommand"`
    56  }
    57  
    58  // Validate satisfies the Validator interface
    59  func (request UpdateDeviceCommandRequest) Validate() error {
    60  	err := common.Validate(request)
    61  	return err
    62  }
    63  
    64  // UnmarshalJSON implements the Unmarshaler interface for the UpdateDeviceCommandRequest type
    65  func (dc *UpdateDeviceCommandRequest) UnmarshalJSON(b []byte) error {
    66  	alias := struct {
    67  		dtoCommon.BaseRequest
    68  		ProfileName   string
    69  		DeviceCommand dtos.UpdateDeviceCommand
    70  	}{}
    71  
    72  	if err := json.Unmarshal(b, &alias); err != nil {
    73  		return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err)
    74  	}
    75  	*dc = UpdateDeviceCommandRequest(alias)
    76  
    77  	if err := dc.Validate(); err != nil {
    78  		return err
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  // ReplaceDeviceCommandModelFieldsWithDTO replace existing DeviceCommand's fields with DTO patch
    85  func ReplaceDeviceCommandModelFieldsWithDTO(dc *models.DeviceCommand, patch dtos.UpdateDeviceCommand) {
    86  	if patch.IsHidden != nil {
    87  		dc.IsHidden = *patch.IsHidden
    88  	}
    89  }