Re: [protobuf] Is possible to use enum from external class in .proto files?

5,205 views
Skip to first unread message

Henner Zeller

unread,
Dec 7, 2012, 12:19:29 AM12/7/12
to Milan Stankovic, Protocol Buffers
On 6 December 2012 15:32, Milan Stankovic <desprez...@gmail.com> wrote:
Is possible to use enum from external class ? I have couple .proto definition and all need to use same enum Type with same tokens. I want to avoid definition of same enum in every class and to avoid sync between, I have that enum in regular external c++ class which I use in code.

It is not possible to have an external C++ enum somewhere, as this would require the proto parser to parse C++.

However, what I usually do in that case is to define my enumeration in a separate *.proto file
myenums.proto ---
enum Foo {
   a = 1;
   b = 2;
   c = 3;
};

enum Bar {
    x = 1;
------

from this, you can generate a myenums.pb.h - it essentially contains the enumerations as you would write them in C++ anyway, and some useful conversion functions (e.g. that convert it to a printable version).
(also, this would generate enumeration for other languages as well, if you need it). From there, you can just use these enums as any other enums.

Also, in your other proto-buffer files, you then can import this file and  use the enumeration

otherprotofile.proto ----
import "myenmus.proto";

message MyMessage {
  Foo foo_enum = 1;
};
----------

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/protobuf/-/H0dBbyOrwrgJ.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.

ronil.m...@practo.com

unread,
Apr 7, 2017, 8:46:12 AM4/7/17
to Protocol Buffers, desprez...@gmail.com, h.ze...@acm.org
Hi, on the same lines need help on an idea.

Is it possible to define my enums in one .proto file and use them across other .proto files.
In context of grpc protobuf3.

Any help is appreciated.

Adam Cozzette

unread,
Apr 14, 2017, 5:27:06 PM4/14/17
to ronil.m...@practo.com, Protocol Buffers, desprez...@gmail.com, h.ze...@acm.org
Yes, that should work. Unless I'm misunderstanding something, you should just be able to do what Henner suggested above to accomplish this.

To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+unsubscribe@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.

Ronil Merchant

unread,
Apr 17, 2017, 3:21:44 AM4/17/17
to Adam Cozzette, Protocol Buffers, desprez...@gmail.com, h.ze...@acm.org
Thanks for the help, the suggested approach by henner worked. Was making an error in the implementation
Reply all
Reply to author
Forward
0 new messages