4

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?

PeppeDx
  • 133
  • 1
  • 10

1 Answers1

1

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.

jpa
  • 10,351
  • 1
  • 28
  • 45
  • Thank you but if possible I would like to not introduce any dependency between cpp generator and mine... – PeppeDx Nov 27 '13 at 20:05
  • You are always depending on the presence of protobuf libraries, so having a dependency on the generated protobuf files should add nothing. And the user is bound to need protoc anyway if he is to use your plugin. – jpa Nov 28 '13 at 07:44
  • To expand on what jpa said, since you're building your code generator on the C++ protobuf libraries (e.g. `FileDescriptor` and such), you already depend on the standard C++ code generator indirectly. But only your plugin depends on it -- the code it generates doesn't have to. – Kenton Varda Nov 28 '13 at 10:32
  • I know that in every case my plugin has to be dependant from the protobuf libraries, I was trying to be independent from the outcome of the cpp_generator in order to simplify the build phase of the project... but I got it ! I will use the GetExtension. Thank you very much! – PeppeDx Nov 28 '13 at 12:55