我正在尝试开发一个插件,以便自动生成特定于我的应用程序的代码。虽然一个可能更简单的策略是让我的代码使用c++插件生成的文件,但我正在尝试从头开始编写插件。
因此,正如我在文件中所解释的,我已经添加到我的包中
import "google/protobuf/descriptor.proto";
extend google.protobuf.FieldOptions {
optional int32 testext = 50000;
}
...
message replyT {
enum ackT {
ok = 0;
failed = 1;
}
required ackT ack = 1 [ (testext) = 42 ];
}
现在的问题是如何访问"testext选项"?
我已经能够使用转储50000 42(分机号和分配的值)
class TestGenerator: public CodeGenerator {
int i;
public:
TestGenerator(const string& name) {};
virtual ~TestGenerator() {};
virtual bool Generate(const FileDescriptor* file,
const string& parameter,
GeneratorContext* context,
string* error) const
{
........................
std::cerr <<"tt"<<file->message_type(i)->field(j)->options().DebugString()<<std::endl;
.................
假设(i和j是正确的)
但除此之外,在研究了文档后,我还无法测试字段是否启用了扩展testext(甚至使用50000)和分配的值。
语言指南建议但是GetExtension方法使用了一个只在*.pb.h内部生成的类型,所以我不能在生成器中使用它。
string value = MyMessage::descriptor()->options().GetExtension(my_option);
有线索吗?
您必须为定义自定义选项的文件运行C++生成器。之后,您可以使用您已经理解的普通GetExtension()
语法。