Import and usage of different package files in protobuf?

我与影子孤独终老i 提交于 2021-02-04 21:59:05

问题


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.


回答1:


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;
  ...
}


来源:https://stackoverflow.com/questions/38390260/import-and-usage-of-different-package-files-in-protobuf

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