Protocol Buffers 記述子を含む文字列を逆コンパイルして .proto ファイルに戻すことはできますか?
次のような長い文字列があるとします
\n\file.proto\u001a\u000ccommon.proto\"\u00a3\u0001\n\nMsg1Request\u0012\u0017\n\u0006common\u0018\u0001 ...
等
.proto を復元する必要があります。元どおりにする必要はありませんが、コンパイル可能です。
Protocol Buffers 記述子を含む文字列を逆コンパイルして .proto ファイルに戻すことはできますか?
次のような長い文字列があるとします
\n\file.proto\u001a\u000ccommon.proto\"\u00a3\u0001\n\nMsg1Request\u0012\u0017\n\u0006common\u0018\u0001 ...
等
.proto を復元する必要があります。元どおりにする必要はありませんが、コンパイル可能です。
C++ では、FileDescriptor
インターフェイスにはDebugString()
、記述子の内容を.proto
構文でフォーマットするメソッドがあります。つまり、まさに必要なものです。それを使用するには、最初にインターフェイスを使用して rawFileDescriptorProto
をに変換するコードを記述する必要があります。FileDescriptor
DescriptorPool
このような何かがそれを行う必要があります:
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
#include <iostream>
int main() {
google::protobuf::FileDescriptorProto fileProto;
fileProto.ParseFromFileDescriptor(0);
google::protobuf::DescriptorPool pool;
const google::protobuf::FileDescriptor* desc =
pool.BuildFile(fileProto);
std::cout << desc->DebugString() << std::endl;
return 0;
}
このプログラムに FileDescriptorProto の未加工のバイトを供給する必要があります。これは、Java を使用して ISO-8859-1 文字セットを使用して文字列をバイトにエンコードすることで取得できます。
また、ファイルが他のファイルをインポートする場合、上記は機能しないことに注意してください-それらのインポートをDescriptorPool
最初にロードする必要があります。