protoc plugin in Python and unknown extension options

287 views
Skip to first unread message

Robert Dyer

unread,
Apr 24, 2017, 11:19:23 PM4/24/17
to Protocol Buffers
I'm writing a protoc plugin for 2.5.0.  My plugin is in Python.

Given the following proto:

import "google/protobuf/descriptor.proto";

message
Test {
    extend google.protobuf.MessageOptions {
        optional
int custom_option = 50000;
   
}

    message
Nested {
        option (custom_option) = 5;
    }


If I want, I can compile this to Python.  Then in my plugin I can import that Test_pb2.py file and lookup the option:


var.options.Extensions[Test.custom_option]


This works fine.  However, the problem I can't figure out is that I need to design the plugin so it works with *any* input file with *any* declared extension option.  So I can't write my code as 'Test.custom_option' because I don't know beforehand what options will be declared.


I can get the extension dynamically from the Descriptor.extension list.  But this is a FieldDescriptorProto and to look up the extension in options.Extensions I need an 'extension handle'.  I'm not sure what that is or how to get it.

Josh Haberman

unread,
Apr 28, 2017, 8:44:01 PM4/28/17
to Protocol Buffers
Have you tried just using the FieldDescriptor? I suspect that will work, as it's what some of our own code does. For example see:

Robert Dyer

unread,
Apr 29, 2017, 11:54:37 PM4/29/17
to Protocol Buffers
That would work to find the actual extension.  That isn't the problem I am having (I can find the extension).  The problem is that as an extension option, the message containing the option doesn't show it.  For example if I do a text_format on the pb from inside the plugin I see this:
 

name: "Test"
nested_type
{

  name: "Nested"

  options {

  }

}
extension {

  name: "custom_option"

  extendee: ".google.protobuf.MessageOptions"

  number: 50000

  label: LABEL_OPTIONAL

  type: TYPE_INT32

}




So while it clearly shows an option was set, it isn't listing the option value of 5.  What I need is to get that value.

Note that I can figure out how to do this if I compile the .proto into Python and then hard code the specific types and look up the extension option that way.  The problem is we wont know the types beforehand and must do this inside the protoc plugin as we process the .proto itself.

Ryan Herbert

unread,
Mar 13, 2019, 10:45:09 PM3/13/19
to Protocol Buffers
Did you ever figure out a solution for this? I'm running into exactly the same issue. I can even pull DOCUMENTATION out of the proto file using source_code_info - but there's nowhere I can look to find the value that the option is set to.

Robert Dyer

unread,
Mar 13, 2019, 10:48:54 PM3/13/19
to Protocol Buffers
Unfortunately no.

I went with the option I mentioned - compiling the proto and hard coding the specific types.  Not the best, but it worked.
Reply all
Reply to author
Forward
0 new messages