42

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.

Muthurathinam
  • 962
  • 2
  • 11
  • 19
  • What do you expect this to do? `val` is a field of `Other`. What does it mean to "use" this field in another type? – Kenton Varda Jul 17 '16 at 04:43
  • @KentonVarda i meant, is there a way to import package directly instead of proto file? so i can use val instead of preceding it with package name. – Muthurathinam Jul 18 '16 at 06:12
  • Again, what are you trying to use `val` for? The code you gave doesn't make any sense. `val` is a field -- what does it mean to reference that field inside another type definition? – Kenton Varda Jul 18 '16 at 17:38
  • sorry for that poor piece of code @KentonVarda. so here is my intention, i need to use some common piece of proto definition, which is defined in some common package name(stable and one i can't change), which is used by many of out internal projects. i have got my own project that needs to be defined in a separate package name(for understanding), which uses that common package. – Muthurathinam Jul 19 '16 at 02:20
  • 3
    It's normal to use _types_ defined in other files. E.g. you could say: `optional muthu.other.Other foo = 2;` to declare a field of _type_ `Other`. There is no way to avoid the package prefix, although because both files are under `muthu`, you could simply write `other.Other` in this case. – Kenton Varda Jul 21 '16 at 01:16
  • @KentonVarda You have cleared my path... Thanks for your time and help... – Muthurathinam Jul 21 '16 at 02:55

1 Answers1

28

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;
  ...
}
Dave Hogan
  • 3,201
  • 6
  • 29
  • 54
Ajay Mishra
  • 383
  • 3
  • 6
  • So this means that I can do ```import foo.bar; message Foo { ... required Open open = 1; ... }``` ? – kekko12 Apr 27 '18 at 14:13