gRPC是否可以响应任意类型的数据?

huangapple go评论89阅读模式
英文:

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 over Any, as that more appropriately declares and restricts what the expected / permitted data is

huangapple
  • 本文由 发表于 2023年7月5日 16:16:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76618366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定