英文:
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
。是否有protoc
或go-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
在这种情况下,是否有protoc
或go-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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论