相关疑难解决方法(0)

使用协议缓冲区描述符对象从 .proto 文件中读取注释

我目前正在使用 Google Protocol Buffers重新审视一个项目。

在项目中,我想利用协议缓冲区的描述符反射功能。

官方文档说明.proto文件的注释可以读取:

  1. 使用函数DebugStringWithOptions(),在消息或描述符上调用。
  2. 使用函数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)

c++ descriptor protocol-buffers c++11 proto3

7
推荐指数
1
解决办法
3871
查看次数

标签 统计

c++ ×1

c++11 ×1

descriptor ×1

proto3 ×1

protocol-buffers ×1