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

     1  //
     2  // Copyright (C) 2020-2021 IOTech Ltd
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  
     6  package common
     7  
     8  import (
     9  	"github.com/google/uuid"
    10  
    11  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    12  )
    13  
    14  // BaseRequest defines the base content for request DTOs (data transfer objects).
    15  type BaseRequest struct {
    16  	Versionable `json:",inline"`
    17  	RequestId   string `json:"requestId" validate:"len=0|uuid"`
    18  }
    19  
    20  func NewBaseRequest() BaseRequest {
    21  	return BaseRequest{
    22  		Versionable: NewVersionable(),
    23  		RequestId:   uuid.NewString(),
    24  	}
    25  }
    26  
    27  // BaseResponse defines the base content for response DTOs (data transfer objects).
    28  type BaseResponse struct {
    29  	Versionable `json:",inline"`
    30  	RequestId   string `json:"requestId,omitempty"`
    31  	Message     string `json:"message,omitempty"`
    32  	StatusCode  int    `json:"statusCode"`
    33  }
    34  
    35  // Versionable shows the API version in DTOs
    36  type Versionable struct {
    37  	ApiVersion string `json:"apiVersion" yaml:"apiVersion" validate:"required"`
    38  }
    39  
    40  // BaseWithIdResponse defines the base content for response DTOs (data transfer objects).
    41  type BaseWithIdResponse struct {
    42  	BaseResponse `json:",inline"`
    43  	Id           string `json:"id"`
    44  }
    45  
    46  func NewBaseResponse(requestId string, message string, statusCode int) BaseResponse {
    47  	return BaseResponse{
    48  		Versionable: NewVersionable(),
    49  		RequestId:   requestId,
    50  		Message:     message,
    51  		StatusCode:  statusCode,
    52  	}
    53  }
    54  
    55  func NewVersionable() Versionable {
    56  	return Versionable{ApiVersion: common.ApiVersion}
    57  }
    58  
    59  func NewBaseWithIdResponse(requestId string, message string, statusCode int, id string) BaseWithIdResponse {
    60  	return BaseWithIdResponse{
    61  		BaseResponse: NewBaseResponse(requestId, message, statusCode),
    62  		Id:           id,
    63  	}
    64  }
    65  
    66  // BaseWithTotalCountResponse defines the base content for response DTOs (data transfer objects).
    67  type BaseWithTotalCountResponse struct {
    68  	BaseResponse `json:",inline"`
    69  	TotalCount   uint32 `json:"totalCount"`
    70  }
    71  
    72  func NewBaseWithTotalCountResponse(requestId string, message string, statusCode int, totalCount uint32) BaseWithTotalCountResponse {
    73  	return BaseWithTotalCountResponse{
    74  		BaseResponse: NewBaseResponse(requestId, message, statusCode),
    75  		TotalCount:   totalCount,
    76  	}
    77  }
    78  
    79  // BaseWithServiceNameResponse defines the base content for response DTOs (data transfer objects).
    80  type BaseWithServiceNameResponse struct {
    81  	BaseResponse `json:",inline"`
    82  	ServiceName  string `json:"serviceName"`
    83  }
    84  
    85  // BaseWithConfigResponse defines the base content for response DTOs (data transfer objects).
    86  type BaseWithConfigResponse struct {
    87  	BaseResponse `json:",inline"`
    88  	ServiceName  string      `json:"serviceName"`
    89  	Config       interface{} `json:"config"`
    90  }