pizheng / protobuf-net

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

Optional enumeration fields create uncompilable code #157

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When compiling the attached .proto file with protogen.exe generating C# code, 
the resulting .cs file has a syntax error.

message Test {
  enum Foo {
    FOO_A = 1;
    FOO_B = 2;
  }

  optional Foo bar = 1;
}

This issue occurs with protobuf-net 282 and the VS10 .msi packages.

Original issue reported on code.google.com by m...@bakedbeans.com on 1 Mar 2011 at 5:56

Attachments:

GoogleCodeExporter commented 9 years ago
I think you need to provide a default value for optional enums:

  optional Foo bar = 1 [default = FOO_A];

Original comment by b...@bachcg.com on 8 Mar 2011 at 9:53

GoogleCodeExporter commented 9 years ago
Actually, according to: 

http://code.google.com/apis/protocolbuffers/docs/proto.html#optional

says "A well-formed message may or may not contain an optional element." and 
"For enums, the default value is the first value listed in the enum's type 
definition."  So I'd say this is a bug and can verify the same issue occurs for 
me.

Original comment by b...@bachcg.com on 8 Mar 2011 at 10:02

GoogleCodeExporter commented 9 years ago
OK; looks like I need to revisit a number of glitches here...

Original comment by marc.gravell on 8 Mar 2011 at 10:25

GoogleCodeExporter commented 9 years ago
Thanks Marc. The workaround for now is just to add a default value as in 
comment 1.

Original comment by b...@bachcg.com on 8 Mar 2011 at 10:30

GoogleCodeExporter commented 9 years ago
The simple file above works fine. What still doesn't work is when you have your 
enums defined in a different .proto file than the class that uses them. The 
XSLT only works if you use -d parameter and end up with one .cs file instead of 
one .cs for each .proto.

I.e. consider the below example. 
Base.proto:
package baz;
enum Foo {
    FOO_A = 1;
    FOO_B = 2;
}

MyMsg.proto:
package baz;
import "Base.proto";

message Test {
    optional Foo bar = 1;
}

Resulting code is missing the required member

    private baz.Foo _bar = baz.Foo.;
    [global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"bar", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
    [global::System.ComponentModel.DefaultValue(baz.Foo.)]
    [global::System.Xml.Serialization.XmlElement(@"bar", Order = 1)]

    public baz.Foo bar
    {
      get { return _bar; }
      set { _bar = value; }
    }

Original comment by yanov...@gmail.com on 9 May 2013 at 9:20

GoogleCodeExporter commented 9 years ago
also it's a duplicate of bug #92, bug #99, bug #129

Original comment by yanov...@gmail.com on 29 May 2013 at 7:18

GoogleCodeExporter commented 9 years ago
Here's a patch which addresses this. If you're not putting everything in one 
file, Marc's code will find nothing, so instead it will put in 
default(Your.EnumType) as the default value.

Original comment by yanov...@gmail.com on 29 May 2013 at 8:16

Attachments: