我最近使用gRPC与proto3,我已经注意到,required并optional在新的语法已被删除.
有人会解释为什么在proto3中删除了必需/可选项?这样的约束似乎对于使定义具有鲁棒性是必要的.
语法proto2:
message SearchRequest {
required string query = 1;
optional int32 page_number = 2;
optional int32 result_per_page = 3;
}
Run Code Online (Sandbox Code Playgroud)
语法proto3:
syntax = "proto3";
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
}
Run Code Online (Sandbox Code Playgroud) 协议缓冲区的proto2版本允许指定消息元素的默认值:
optional double scaling_factor = 3 [default = 1.0];
Run Code Online (Sandbox Code Playgroud)
为什么proto3中不再可能这样?我认为这是一个简洁的功能,可以在线上保存额外的字节,而无需编写任何包装代码.
我正在使用谷歌grpc与json代理.由于某种原因,我需要omitempty从*.pb.go文件中生成的结构中删除标记.
如果我有这样的原型消息
message Status {
int32 code = 1;
string message = 2;
}
Run Code Online (Sandbox Code Playgroud)
生成的结构看起来像这样
type Status struct {
Code int32 `protobuf:"varint,1,opt,name=code" json:"code,omitempty"`
Message string `protobuf:"bytes,2,opt,name=message" json:"message,omitempty"`
}
Run Code Online (Sandbox Code Playgroud)
但我需要omitempty从生成的结构中删除标记.我怎样才能做到这一点?
协议缓冲区v3声称,该库是json友好的(https://developers.google.com/protocol-buffers/docs/proto3#json),但我找不到如何实现获取该映射.我应该在protoc中添加一些插件或一些选项,还是调用一些特殊的东西而不是SerializeTo/ParseFrom?
是否有人使用该功能?
假设我们使用gRCP/Protobuf来连接许多应用程序.这些应用程序是以自己的速度在自己的团队中开发和发布的.随着时间的推移,将在定义的界面上使用不同版本的相同应用程序的不同版本(例如,在用户PC上安装的桌面应用程序).
虽然Protobuf旨在允许向后兼容,但有没有办法知道哪个版本的接口在不同的点运行?
最简单的实现是使接口版本等于app版本.但是,由于使用了许多语言,在所有语言中实现应用程序版本并非易事.
那么版本界面如何让服务器知道客户端版本?我认为服务器应该能够登录
来自AppName v.version的DATETIME连接[using interface v.version]
如果我有这样的服务:
service MyService {
rpc GetThings(GetThingsRequest) returns (GetThingsResponse);
}
Run Code Online (Sandbox Code Playgroud)
我如何标记GetThings为已弃用?
我知道如何将字段或消息标记为已弃用但我找不到有关rpcs的任何信息.
这是针对proto3的.
我目前正在使用 Google Protocol Buffers重新审视一个项目。
在项目中,我想利用协议缓冲区的描述符和反射功能。
官方文档说明.proto文件的注释可以读取:
DebugStringWithOptions(),在消息或描述符上调用。GetSourceLocation(),在描述符上调用。我无法检索评论,因此我认为我做错了什么,或者该功能尚未在 Protocol Buffers 中完全实现。
下面是一些代码片段:
google::protobuf::DebugStringOptions options;
options.include_comments = true;
std::cout << "google::protobuf::Descriptor::DebugStringWithOptions(): "
<< message.descriptor()->DebugStringWithOptions(options) << std::endl
<< std::endl;
const google::protobuf::FieldDescriptor* field_descriptor{
message.descriptor()->field(1)};
// TODO(wolters): Why doesn't this work?
google::protobuf::SourceLocation* source_location{
new google::protobuf::SourceLocation};
field_descriptor->GetSourceLocation(source_location);
// if (field_descriptor->GetSourceLocation(source_location)) {
std::cout << "start_line: " << source_location->leading_comments
<< std::endl;
std::cout << "end_line: " << source_location->leading_comments << std::endl;
std::cout << "start_column: " << source_location->start_column …Run Code Online (Sandbox Code Playgroud) 我想将 AutoMapper 与 proto3 一起使用,但我遇到的最大问题是从可能允许的源属性映射null到从不允许的原型。手动进行此类填充时,必须执行以下操作:
var proto = new Proto();
if (source.Field != null)
{
proto.Field = source.Field;
}
Run Code Online (Sandbox Code Playgroud)
我仍然觉得这很荒谬,但 proto3 显然就是这样。
无论如何,这意味着映射必须有条件,以确保null值不会传播到原型:
config
.CreateMap<Source, Proto>()
.ForMember(
x => x.Field,
options => options.Condition(source => source.Field != null));
Run Code Online (Sandbox Code Playgroud)
我能感觉到它变老的速度非常快,因为我的原型中有很多属性。
我想知道是否有办法让我在更高的抽象级别上处理这个问题?
我正在尝试编写一个通用的Java类,该类可用于反序列化/解析任何protobuf消息。
以下是代码在理想环境中的外观:
public abstract class ProtoDeserializer<T extends Message> {
public T deserialize(final byte[] bytes) throws Exception {
Parser<T> parser = T.getParserForType(); // Syntax Error: this method is not static!
T message = parser.parseFrom(bytes);
validate(message);
return message;
}
public abstract void validate(final T message) throws Exception;
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法获得通用protobuf消息的正确解析器。实现此类泛型类的正确方法是什么?
proto3 ×10
protocol-buffers ×10
grpc ×4
c++ ×2
go ×2
automapper ×1
c# ×1
c++11 ×1
descriptor ×1
generics ×1
java ×1
json ×1
versioning ×1