peper0
peper0

Reputation: 3223

Extending protobuf.FieldOptions in imported .proto file

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.

Upvotes: 6

Views: 11751

Answers (2)

Jedidiah Bartlett
Jedidiah Bartlett

Reputation: 257

Because you've made a new package with your new proto file, you'll need to reference the package's namespace.

As you noted in your comment, just use "(myext.myopt)" instead of "(myopt)", so it looks like this:

myext.proto:

Unmodified from what you showed

package myext;

import "google/protobuf/descriptor.proto";

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

addressbook.proto:

Replace "(myopt)" with "(myext.myopt)"

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

package tutorial;


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

Upvotes: 2

Bruce Martin
Bruce Martin

Reputation: 10543

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)

Upvotes: 1

Related Questions