【问题标题】:How to get the method options from protobuf descriptor?如何从 protobuf 描述符中获取方法选项?
【发布时间】:2016-12-17 14:11:54
【问题描述】:

我尝试使用 protobuf 编译器解析 .proto 文件。但这是一个令人困惑的问题,我无法获得方法选项。

似乎我的选项被视为“未知字段”,而不是选项。

有什么办法可以解决这个问题吗?谢谢。

(我不想在这里粘贴很多代码,但我认为完整描述问题很重要。抱歉。)

(环境:g++ 4.7,Ubuntu 16.04,Protobuf 3.0.0)

#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/compiler/importer.h>

using namespace std;
using namespace google::protobuf;
using namespace google::protobuf::compiler;

#define print(x) std::cout << x << std::endl
#define input(x) std::cin >> x

int main() {
    DiskSourceTree sourceTree;
    sourceTree.MapPath("", "./");
    sourceTree.MapPath("", "./protobuf/include/");
    Importer importer(&sourceTree, NULL);

    auto fd = importer.Import("example.proto");
    assert(fd);

    int service_count = fd->service_count();

    for (int i = 0; i < service_count; i++) {
        auto service_d = fd->service(i);
        int method_count = service_d->method_count();
        for (int j = 0; j < method_count; j++) {
            auto method_d = service_d->method(j);
            print(method_d->options().unknown_fields().field_count());$ 
            print(">> " << method_d->options().uninterpreted_option_size());
        }
    }
    return 0;
}

// lrpc.proto
syntax = "proto3";
package lrpc;
import "google/protobuf/descriptor.proto";


extend google.protobuf.MethodOptions {
    int32 CmdID      = 50000;
    string OptString = 50001;
    string Usage     = 50002;
}

// example.proto
syntax = "proto3";

package foobar;

import "google/protobuf/wrappers.proto";
import "google/protobuf/empty.proto";

import "lrpc.proto";

message SearchRequest {
    // ... 
}

message SearchResponse {
    // ...
}

service SearchService {
    rpc Search( SearchRequest ) returns( SearchResponse ) {
        option( lrpc.CmdID ) = 1;
    }
}

【问题讨论】:

    标签: protocol-buffers protoc


    【解决方案1】:

    选项不是未知字段,因为它们是extensions!扩展应该在 proto3 中被删除,但是当您使用 Importer 动态解析 .proto 文件时,无论您声明的语法版本如何,都会启用扩展。

    如果您在内部 for() 循环中添加一行,例如:

    print(method_d->options().DebugString());
    

    你会得到如下输出:

    [lrpc.CmdID]: 1
    

    您可以使用 protobuf 反射枚举扩展值——它们会在您调用 Reflection::ListFields() 时出现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多