PeppeDx
PeppeDx

Reputation: 167

Handling Custom options within a protoc plugin

I'm trying to develop a plugin in order to autogenerate code that is specific to my application. While a maybe simpler strategy is to make my code use the files generated by the c++ plugin I'm trying to write the plugin from scratch.

So as explained in the documentation I've added to my package

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 ];
}

now the question is how do I access to "testext options" ?

I've been able to dump 50000 42 (the extension number and the value assigned) using

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 <<"\t\t"<<file->message_type(i)->field(j)->options().DebugString()<<std::endl;
      .................

assuming (i and j are correct)

but apart of this I haven't been able after exploring the documentation how to test if a field has the extension testext enabled (even by using 50000) and the value assigned.

The Language guide suggests but the GetExtension method uses a type that is only generated inside the *.pb.h so I can't use it in my generator.

string value = MyMessage::descriptor()->options().GetExtension(my_option);

Any clues?

Upvotes: 4

Views: 2627

Answers (1)

jpa
jpa

Reputation: 12176

You have to run the C++ generator for the file that defines your custom options. After that you can use the normal GetExtension() syntax that you have already figured out.

Upvotes: 1

Related Questions