Custom protoc plugin parsing not working for custom options

I am trying to write a protoc plugin that requires me to use custom options. I defined my custom option as shown in the example (https://developers.google.com/protocol-buffers/docs/proto#customoptions):

import "google/protobuf/descriptor.proto";

extend google.protobuf.MessageOptions {
    string my_option = 51234;
}

I use it as follows:

message Hello {
    bool greeting = 1;
    string name = 2;
    int32 number = 3;

    option (my_option) = "telephone";
}

However, when I read the parsed request, the options field is empty for the "Hello" message.

I am doing the following to read

data = sys.stdin.read()

request = plugin.CodeGeneratorRequest()
request.ParseFromString(data)

When I print "request," it just gives me this

message_type {
  name: "Hello"
  field {
    name: "greeting"
    number: 1
    label: LABEL_REQUIRED
    type: TYPE_BOOL
    json_name: "greeting"
  }
  field {
    name: "name"
    number: 2
    label: LABEL_REQUIRED
    type: TYPE_STRING
    json_name: "name"
  }
  field {
    name: "number"
    number: 3
    label: LABEL_OPTIONAL
    type: TYPE_INT32
    json_name: "number"
  }
  options {
  }
}

As seen, the options field is empty even though I defined options in my .proto file. Is my syntax incorrect for defining custom options? Or could it be a problem with my version of protoc?


Turns out you need to have the _pb2.py file imported for the .proto file in which the custom option is defined. For example, it you are parsing a file (using ParseFromString ) called example.proto which uses a custom option defined in option.proto , you must import option_pb2.py in the Python file that calls ParseFromString .

链接地址: http://www.djcxy.com/p/64798.html

上一篇: FuelPHP ORM通过数组更新

下一篇: 自定义protoc插件解析不能用于自定义选项