【问题标题】:Nest+GRPC proto file response structureNest+GRPC proto 文件响应结构
【发布时间】:2021-05-05 05:04:54
【问题描述】:

我正在使用 Nest 和 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 消息?

【问题讨论】:

    标签: api nestjs grpc proto grpc-node


    【解决方案1】:

    我认为您可以使用 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 naming convention 在您的 protobuf 中保持一致的标准命名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 2016-12-08
      • 2021-01-27
      • 2020-04-09
      • 2020-08-24
      • 1970-01-01
      • 2020-04-27
      相关资源
      最近更新 更多