2016-12-15 3 views
1

私はprotobufコンパイラで.protoファイルを解析しようとしています。しかし、私は方法のオプションを得ることができないという混乱する問題があります。protobuf記述子からメソッドオプションを取得するには?

私のオプションは「未知のフィールド」として扱われますが、オプションは扱われないようです。

問題を解決する方法はありますか?ありがとう。

(私はここで多くのコードを貼り付けることを憎むが、私はそれが完全に問題を記述するために不可欠だと考えているため申し訳ありません。。)

(ENV: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; 
    } 
} 

答えて

1

extensionsであるため、オプションは不明なフィールドではありません!拡張機能はproto3では削除されていたが、Importerを使用してファイルを動的に解析すると、宣言した構文バージョンに関係なく拡張機能が有効になります。

あなたのような()ループのためにあなたの内に行を追加する場合:あなたがいるProtobufリフレクションを使用して拡張値を列挙することができます

[lrpc.CmdID]: 1 

- 彼らは:

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

あなたが好きな出力が得られますReflection::ListFields()に電話をしたときに表示されます。

関連する問題