如何禁用自动生成的 gRPC 客户端函数中的 `err != nil` 检查?

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

How to disable `err != nil` check in autogenerated gRPC client functions

问题

我有一个接口的protobuf定义。我使用protoc --go_out=... --go-grpc_out=... file.proto自动生成该接口的客户端函数。

自动生成的函数都有以下代码片段:

if err != nil {
    return nil, err
}
return out, nil // out是服务器返回给客户端的结果

我希望所有自动生成的函数都具有此检查,而是直接return out, err。是否有protocgo-grpc_out选项可以更改自动生成行为以实现这一点?

英文:

I have a protobuf definition of an interface. I autogenerate client functions for that interface using protoc --go_out=... --go-grpc_out=... file.proto.

The autogenerated functions all have this code snippet:

if err != nil {
    return nil, err
}
return out, nil // out is what the server returned to this client

I want all autogenerated functions to not have this check, and instead directly return out, err. Is there a protoc or go-grpc_out option that alters autogeneration behaviour in this way?

答案1

得分: 2

在这种情况下,是否有protocgo-grpc_out选项可以改变自动生成行为?

没有。

但是,即使有这样的选项,如果你的目标是查看发送到网络的内容,你的方法也不会起作用。

不过,一个客户端拦截器可能会更接近你的目标,它可以在客户端函数处理之前让你访问gRPC负载。

但是,即使这样,你也无法获得原始的网络数据。如果你收到一个无效的gRPC消息,即使传递给拦截器之前也会被处理。如果这是你需要的,你可能需要一种网络层代理。

英文:

> Is there a protoc or go-grpc_out option that alters autogeneration behaviour in this way?

No.

But if your goal is to see what was sent over the network, your approach won't work anyway, even if there were such an option.

What will get a bit closer, though, is a client interceptor, which can give you access to the gRPC payload before it's handled by the client function.

But even this won't give you the raw network data. If you get an invalid gRPC message, even that will be handled before passed to the interceptor. If that's what you need, you'll want some sort of network layer proxy.

huangapple
  • 本文由 发表于 2022年8月3日 21:45:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/73222779.html
匿名

发表评论

匿名网友

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

确定