首页 > 解决方案 > Nest+GRPC proto 文件响应结构

问题描述

我正在使用嵌套和 GRPC 微服务来构建我的后端。

所以首先我创建了rest api,现在我将它转换为GRPC调用。

所以我的api响应如下:

{
"data": {
    "2-2-2-2": [             //id
        [
            24.942795,       //lat
            60.17088         //lng
        ]
    ]
}

}

protofile:

    syntax = "proto3";
package LocationTracking;

service LocationTrackingService {
  rpc getLocations(GetLocationInputs) returns (GetLocationResponse);
 
}

     message GetLocationInputs {
    string assetIds = 1;
    optional string from = 4;
    optional string to = 5;
    
  }

  message GetLocationResponse {
   
  }

那么我应该如何为这种响应构建 GetLocationResponse 消息呢?

标签: apinestjsgrpcprotogrpc-node

解决方案


我认为您可以使用Maps类型来定义您的GetLocationResponse和消息Location来定义您的两个值

例子

syntax = "proto3";

package location.tracking

message Location {
    double lat;
    double lng;
}

service LocationTrackingService {
  rpc getLocations(GetLocationRequest) returns (GetLocationResponse);
}

message GetLocationRequest {
  string assetIds = 1;
  optional string from = 4;
  optional string to = 5;  
}

message GetLocationResponse {
  map<string, Location> data = 1;   
}

我还建议您使用Google 命名约定,以在您的 protobuf 中保持一致的标准命名。


推荐阅读