Import and usage of different package files in protobuf?

Muthurathinam picture Muthurathinam · Jul 15, 2016 · Viewed 37.4k times · Source

I have imported an other proto which having different package name than mine. For usage of messages from other package, have accessed that message with package name.

For Example :

other.proto

package muthu.other;

message Other{
   required float val = 1;
}

myproto.proto

package muthu.test;

import "other.proto";

message MyProto{
  required string str = 1;
  optional muthu.other.Other.val = 2;
}

Is there a way to use val of muthu.other package directly like optional val = 2; instead of using muthu.other.Other.val ?

Couldn't find any help document regarding this. Help me out.

Answer

Ajay Mishra picture Ajay Mishra · Mar 3, 2017

If the package name is same then you can omit the package name from the field declaration but otherwise there is no other way. if you can include muthu.test in the same package by specifying "package muthu.other" then it is allowed.

From Google documentation of protobuf:

You can add an optional package specifier to a .proto file to prevent name clashes between protocol message types.

package foo.bar;
message Open { ... }

You can then use the package specifier when defining fields of your message type:

message Foo {
  ...
  required foo.bar.Open open = 1;
  ...
}