3

I use protobuf-net and ProtoGen.exe to parse following .proto file (given by another project)

enum RGBFlags { FLAG_RED = 1; FLAG_GREEN = 2; FLAG_BLUE = 4; }

message SomeMessage {
  // Values from RGBFlags only allowed
  optional int32 flags = 2;
}   

My fellow programmers in C++ don't care about type safety and treat flags field as a plain integer. I wanted to be more strict and try to avoid such code:

SomeMessage foo = new SomeMessage();
foo.flags = (int)RGBFlags.FLAG_BLUE | (int)RGBFlags.FLAG_GREEN;

I thought that I could use protbuf custom options to amend proto code and modify XSLT transform of ProtoGet to generate necessary `[Flags]' annotations.

extend google.protobuf.EnumOptions {
  optional bool generate_bit_field = 60000;
}

enum RGBFlags {
  option (generate_bit_field) = true;
  FLAG_RED = 1; FLAG_GREEN = 2; FLAG_BLUE = 4;
}

message SomeMessage {
  // Values from RGBFlags only allowed
  optional int32 flags = 2;
}   

Problem is that all custom options appear as uninterpreted_option in the temporary file in ProtoGen.

Any idea what I could do to get [Flags] annotations in my code?

Charles
  • 50,943
  • 13
  • 104
  • 142
Michal Sznajder
  • 9,338
  • 4
  • 44
  • 62

1 Answers1

1

Re flags; the raw protobuf spec doesn't really have support for composite enum values, so in some ways I understand why they are doing it that way. And sadly there is no such this as partial enum, so you can't add the [Flags] in a separate code file.

Re custom options; it is an excellent question, and support for custom options has been raised before. It is definitely something I'd like to add, but relative to other features it simply isn't a massively demanded item, so (due to limited resource) it has not (yet) been investigated fully.

Therefore, I don't have a great answer for you; that feature isn't really there much right now. You could hard-code that one scenario in your xslt (for your specific types). Or wait until I get around to it (I don't have a specific timescale). Or take a peek yourself.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Is there an update to this? @MarcGravell Is it possible to have custom options while using protogen to generate C# files? – nj_bubbles Feb 15 '21 at 09:31
  • @nj_bubbles we don't currently persist custom option data in the generate types. In theory we could. Do you have a suggestion on how you would expect to query this data, it it was persisted somehow? – Marc Gravell Feb 15 '21 at 13:58
  • I am not sure about the persistence. But for instance if I generate a C# equivalent from a proto file that has an 'extend' like in the question above, I get a 'Google' namespace not found error. If I were to use Google Protobuf package instead then this would work but I was wondering how to achieve this with protobuf-net. – nj_bubbles Feb 16 '21 at 08:15
  • @nj_bubbles that sounds like a completely different problem - it sounds like you're simply missing an "import", specifically `import "google/protobuf/descriptor.proto";` - `extend` works just fine, and the data is fully available during the protogen processing – Marc Gravell Feb 16 '21 at 08:41
  • Ah my bad. Thanks for the tip. I added this import in the proto file but the generated C# equivalent still has the namespace not found error. Should I also add some specific package in the C# side additionally? I use protogen to generate the C# files. – nj_bubbles Feb 16 '21 at 09:04
  • @nj_bubbles again, you haven't told me what you expect to happen; protogen currently directly processes a few, but no custom option data is *directly* written out - that's where the persistence bit would come in... – Marc Gravell Feb 16 '21 at 09:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228785/discussion-between-nj-bubbles-and-marc-gravell). – nj_bubbles Feb 16 '21 at 09:18
  • Okay I will try to summarize what I am trying to do. I have a proto (proto2) file. I want to generate its C# equivalent using protogen so as to use in my C# application later. The proto file has an extend and I want this available in the C# file. Right now, I have a partial class `Extensions` (in the generated C#) with these custom options/fields but I cannot access them due to the namespace error and I am trying to resolve this issue. – nj_bubbles Feb 16 '21 at 09:42
  • @nj_bubbles custom options (as per the original question here) are *metadata* - they aren't per instance data; can I assume, then, that you're not really talking about "custom options", but are *just* talking about extension fields? if so, can you please be very specific about *what* namespace error you are seeing? what exact line (in the generated code) is causing an error? – Marc Gravell Feb 16 '21 at 13:37
  • I have posted it as a separate question since it looks like a different problem altogether. @MarcGravell Please check out [this](https://stackoverflow.com/questions/66226784/is-it-possible-to-generate-c-sharp-files-using-protobuf-net-for-proto2-files-wit) – nj_bubbles Feb 16 '21 at 14:59