英文:
Can we response an Any type on gRPC?
问题
我正在构建一个服务器来支持第三方应用程序。该服务器需要只有一个路径,但根据提供的操作,会返回不同类型的输出。问题在于有时候响应是一个数组,但在其他情况下,它们是对象(我需要短消息和流的混合)。有人知道如何满足这个逻辑吗?请帮忙。谢谢。
我目前在我的 .proto 文件中有这个逻辑,但显然它不起作用。
rpc CustomHandler(CustomActionRequest) returns (Any) {
option (google.api.http) = {
post: "/custom"
body: "*"
};
}
英文:
I'm building a server to support a 3rd party app. The server is required to have only one path but will respond different types of output based on their provided actions. The issue occurs when sometimes the response is an array but in other cases, they are objects (I need a mix of message and stream in short). Does anyone know how to satisfy this logic? Please help. Thank you
I currently have this logic in my .proto but obviously, it doesn't work.
rpc CustomHandler(CustomActionRequest) returns (Any) {
option (google.api.http) = {
post: "/custom"
body: "*"
};
}
答案1
得分: 1
关于消息与数组的问题:与其发送一个数组,考虑发送一个包含数组的消息。
一般来说,考虑使用oneof
而不是Any
,因为它更准确地声明和限制了预期/允许的数据类型。
英文:
- re the message vs array problem: rather than sending an array, consider sending a message that has an array
- in general, consider
oneof
overAny
, as that more appropriately declares and restricts what the expected / permitted data is
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论