Extending protobuf.FieldOptions in imported .proto file

半城伤御伤魂 提交于 2019-12-07 09:57:12

问题


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)



来源:https://stackoverflow.com/questions/17126230/extending-protobuf-fieldoptions-in-imported-proto-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!