Extending protobuf.FieldOptions in imported .proto

2019-05-18 05:19发布

问题:

I'm trying to define my custom field option in google protocol buffers. If I create such a file, everything works ok:

import "google/protobuf/descriptor.proto";

package tutorial;

extend google.protobuf.FieldOptions {
  optional int32 myopt = 70000;
}

message Persona {
  required string name = 1 [(myopt)=5];
}

However, if I try to move "myopt" definition to another file, compilation fails:

myext.proto:

package myext;

import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
  optional int32 myopt = 70000;
}

addressbook.proto:

import "google/protobuf/descriptor.proto";
import "myext.proto";

package tutorial;


message Persona {
  required string name = 1 [(myopt)=5];
}

compilation:

$ protoc --cpp_out=. -I/usr/include -I. addressbook.proto
addressbook.proto:8:29: Option "(myopt)" unknown.

Is there any way to define custom field options in other file than the one that use it? It is important to move common part to a common file if I want to use my option in several .proto files.

回答1:

Because you have a package myext

you should be doing

import "myext/myext.proto";

with myext.proto located in a sub-directory of myext.

In protocol buffer package indicates the directory where the file should reside (like in java)