api - 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 消息呢?
解决方案
我认为您可以使用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 中保持一致的标准命名。
推荐阅读
- android - 从 firebase 数据库中检索特定数据
- java - 国际化两大类;资源包
- python - Python LightGBM 中的 leaf_values 是什么?
- java - 如何将数组的元素向左旋转
- google-bigquery - 在 gcs 上使用 parquet 文件创建新的大查询外部表时。显示错误
- jquery - jquery圆形进度条,末尾有小圆圈
- php - 如何在搜索查询中获取包含日期范围和搜索字段(如“%xyz%”)的记录?
- php - Yii2:使用 ActiveRecord 在数据库中重复行
- android - 根据活动的按钮可见性
- powershell - CSV 输入,powershell 从目标列中提取 $null 值行