参见https://github.com/grpc/grpc-node/issues/1202。
通常在CRUD操作中,未提供的值表示不会更改该字段,而空数组[]表示清除该字段中的所有项。
但是,如果您试图实现CRUD操作并通过grpc将它们作为服务提供,那么上述场景很难实现。
service CRUD {
rpc updateTable(updateRequest) returns updateResponse {}
}
message updateRequest {
repeated string a = 1;
string b = 2;
}
message updateResponse {
boolean success = 1;
}如果使用默认选项加载包,则客户端无法通过以下方式删除项目
client.CRUD.updateTable({a: []})因为当参数{a: []}到达服务器端时,它变成了{}。
如果您使用选项{arrays: true}加载程序包,则字段a将被无意中清除,而客户端仅尝试更新其他字段:
client.CRUD.updateTable({b: 'updated value'}) 因为当参数{b: 'updated value'}到达服务器端时,它变成了{a: [], b: 'updated value'}。
关于如何使用grpc-node和proto3处理这两个场景,有没有人可以分享一些更好的想法
发布于 2019-11-28 02:28:01
protobuf编码没有区分这两种情况。因为protobuf是语言不可知的,所以它不理解Javascript的“未定义”和"[]“的概念上的细微差别。
您需要在proto消息中传递额外的信息,以便区分这两种情况。
我强烈建议阅读这里的设计文档:https://developers.google.com/protocol-buffers
https://stackoverflow.com/questions/59064092
复制相似问题