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

     1  //
     2  // Copyright (C) 2021 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  	dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
    13  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    14  )
    15  
    16  // OperationRequest defines the Request Content for SMA POST Operation.
    17  type OperationRequest struct {
    18  	dtoCommon.BaseRequest `json:",inline"`
    19  	ServiceName           string `json:"serviceName" validate:"required"`
    20  	Action                string `json:"action" validate:"oneof='start' 'stop' 'restart'"`
    21  }
    22  
    23  // Validate satisfies the Validator interface
    24  func (o *OperationRequest) Validate() error {
    25  	err := common.Validate(o)
    26  	return err
    27  }
    28  
    29  // UnmarshalJSON implements the Unmarshaler interface for the OperationRequest type
    30  func (o *OperationRequest) UnmarshalJSON(b []byte) error {
    31  	alias := struct {
    32  		dtoCommon.BaseRequest
    33  		ServiceName string
    34  		Action      string
    35  	}{}
    36  
    37  	if err := json.Unmarshal(b, &alias); err != nil {
    38  		return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err)
    39  	}
    40  	*o = OperationRequest(alias)
    41  
    42  	if err := o.Validate(); err != nil {
    43  		return err
    44  	}
    45  
    46  	return nil
    47  }