anon
anon

Reputation:

protobuf-net and optional values

I'm attempting to generate protocol buffers C# code from a .proto file and, unfortunately, protobuf-net is generating code that doesn't compile.

The specific part of my .proto that is causing a problem is:

// Counter value response
message RpbCounterGetResp {
    optional sint64 value = 1;
}

When autogenerated by protobuf-net using the command line tools 'C:\Program Files (x86)\protobuf-net\protobuf-net-VS9\protogen.exe' -p:detectMissing -ns:CorrugatedIron.Messages -i:riak_kv.proto -o:riak_kv.cs

I get the following chunk o' uncompilable code:

[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"RpbCounterUpdateResp")]
public partial class RpbCounterUpdateResp : global::ProtoBuf.IExtensible, global::System.ComponentModel.INotifyPropertyChanging
{
  public RpbCounterUpdateResp() {}  

  private long? _value;
  [global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"value", DataFormat = global::ProtoBuf.DataFormat.ZigZag)]
  public long value
  {
    get { return _value?? default(long); }
    set { OnPropertyChanging(@"value"); _value = value; }
  }
  [global::System.Xml.Serialization.XmlIgnore]
  [global::System.ComponentModel.Browsable(false)]
  public bool valueSpecified
  {
    get { return _value != null; }
    /* this is the big that won't compile as there's no conversion 
       between bool and System.Nullable<long>
     */ 
    set { if (value == (_value== null)) _value = value ? value : (long?)null; }
  }
  private bool ShouldSerializevalue() { return valueSpecified; }
  private void Resetvalue() { valueSpecified = false; }

  public event global::System.ComponentModel.PropertyChangingEventHandler PropertyChanging;
  protected virtual void OnPropertyChanging(string propertyName)
  { if(PropertyChanging != null) PropertyChanging(this, new global::System.ComponentModel.PropertyChangingEventArgs(propertyName)); }

  private global::ProtoBuf.IExtension extensionObject;
  global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
    { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}

Are there additional protogen options that can be supplied to correct this issue?

Upvotes: 1

Views: 1215

Answers (1)

anon
anon

Reputation:

Turns out this is caused by a C# reserved keyword conflict between the .proto property named value, which produces code that won't compile. Changing the name of the autogenerated property to returnValue fixes things... until the next time code is autogenerated.

Upvotes: 1

Related Questions