Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import and usage of different package files in protobuf?

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.

like image 523
Muthurathinam Avatar asked Jul 15 '16 07:07

Muthurathinam


People also ask

Can proto3 import proto2?

It's possible to import proto3 message types and use them in your proto2 messages, and vice versa. However, proto2 enums cannot be used in proto3 syntax.

What is protobuf package?

proto file starts with a package declaration, which helps to prevent naming conflicts between different projects.

What are .proto files?

A . proto file is similar to a JSON file in that it represents structured data, but you can run a compiler on the . proto file to generate code that can read and write the data in the programming language of your choice.

What is oneof in protobuf?

Protocol Buffer (Protobuf) provides two simpler options for dealing with values that might be of more than one type. The Any type can represent any known Protobuf message type. And you can use the oneof keyword to specify that only one of a range of fields can be set in any message.


1 Answers

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;   ... } 
like image 136
Ajay Mishra Avatar answered Oct 05 '22 16:10

Ajay Mishra