amoraller / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

GetSchema() does not write .proto message correctly for enums marked with [Flags] attribute #329

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Please include an e-mail address if this might need a dialogue!
==============

What steps will reproduce the problem?
1. Create a c# enum decorated with the [Flags] attribute:
    [Flags]
    public enum Breed
    {
        None = 0,
        Siamese = 1
    }
2. Add a property of type Breed to a class.
3. Call GetSchema() for this class.

What is the expected output? What do you see instead?

I see this without the flags attribute:
enum Breed {
   Breed_None = 0;
   Breed_Siamese = 1;
}
message Cat {
   optional Breed Breed = 1 [default = Breed_None];
   optional bcl.DateTime Dob = 2;
}

I see this with the flags attribute:
enum Breed {
}
message Cat {
   optional Breed Breed = 1 [default = None];
   optional bcl.DateTime Dob = 2;
}

What version of the product are you using? On what operating system?
A build based on r591.

Please provide any additional information below.

I'm not sure if there was something special that you intended to do with this. 
I have seen other people implement the .proto message for a flags enum by 
setting the values to bitmasks.

Original issue reported on code.google.com by Franches...@gmail.com on 18 Oct 2012 at 3:47

GoogleCodeExporter commented 9 years ago
I can look at improving the output, but fundamentally my understanding is that 
the protobuf specification does not support [Flags] / enum bitmasks. 
Prototobuf-net passes them through as ints, so I suspect the only sane thing I 
can do here is describe it as int on the .proto schema. I can perhaps list the 
flags etc as comments?

Original comment by marc.gravell on 18 Oct 2012 at 4:25

GoogleCodeExporter commented 9 years ago
We currently have a C++ application that communicates with a C# application 
using an enum of this type, and we use protogen and protoc to create the 
classes to send /receive this enum.

Here is .proto syntax of the enum (with names changed to protect innocent 
variables):

enum EFlags
{
       TF_P                          = 0x00000001;
       TF_D                          = 0x00000002;
       TF_G                          = 0x00000004;
       TF_SP                          = 0x00000008; 
       TF_SKD              = 0x00000010;
       TF_SKG             = 0x00000020;
       TF_VE                                  = 0x00000040; 
       TF_VO               = 0x00000080;        
       TF_RH                           = 0x00000100;              
       TF_AC = 0x00000200;                     
       TF_FO           = 0x00000400;              
       TF_ALL                            = 0x7FFFFFFF; // highest bit not usable due to network serialization
}

Here is the C++ enum as generated by protoc:

enum EFlags {
      TF_P = 1,
      TF_D = 2,
      TF_G = 4,
      TF_SP = 8,
      TF_SKD = 16,
      TF_SKG = 32,
      TF_VE = 64,
      TF_VO = 128,
      TF_RH = 256,
      TF_AC = 512,
      TF_FO = 1024,
      TF_ALL = 2147483647
};

And here is the protogen generated C# enum:

  public enum ETheoFlags {
    TF_P = 1,
    TF_D = 2,
    TF_G = 4,
    TF_SP = 8,
    TF_SKD = 16,
    TF_SKG = 32,
    TF_VE = 64,
    TF_VO = 128,
    TF_RH = 256,
    TF_AC = 512,
    TF_FO = 1024,
    TF_ALL = 2147483647,
  }

Is it possible to have protobuf-net do something similar?

Thanks! :)

Original comment by Franches...@gmail.com on 19 Oct 2012 at 8:31

GoogleCodeExporter commented 9 years ago
Fixed r596

Original comment by marc.gravell on 19 Oct 2012 at 9:23