Protobuf-net [default] not recognized in .proto file?

I am using .proto files to point my protobuf posts. I am using [default=]

but I cannot see where the values ​​are set. They are not found in autogenerated .cs files. I want to set some default values ​​when creating a post. I cannot use the default constructor because it is inside the auto-generated .cs files.

Any ideas how to solve this problem?

My .proto file:

package Messages;

message Ack
{
    required bool is_error = 1 [default=false];
    required string message = 2 [default="ok"];
    required string request_id = 3;
}

      

generates:

namespace Messages
{
  [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Ack")]
  public partial class Ack : global::ProtoBuf.IExtensible
  {
    public Ack() {}

    private bool _is_error;
    [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"is_error", DataFormat = global::ProtoBuf.DataFormat.Default)]
    public bool is_error
    {
      get { return _is_error; }
      set { _is_error = value; }
    }
    private string _message;
    [global::ProtoBuf.ProtoMember(2, IsRequired = true, Name=@"message", DataFormat = global::ProtoBuf.DataFormat.Default)]
    public string message
    {
      get { return _message; }
      set { _message = value; }
    }
    private string _request_id;
    [global::ProtoBuf.ProtoMember(3, IsRequired = true, Name=@"request_id", DataFormat = global::ProtoBuf.DataFormat.Default)]
    public string request_id
    {
      get { return _request_id; }
      set { _request_id = value; }
    }
    private global::ProtoBuf.IExtension extensionObject;
    global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
      { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
  }

      

I am using Visual Studio 2013 Update 3 english and latest protobuf-net.

+3


source to share


1 answer


If I take as an example:

 message Foo {
    optional int32 value = 1 [default = 123];
 }

      

and run this through protogen, then I get:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

// Generated from: my.proto
namespace my
{
  [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Foo")]
  public partial class Foo : global::ProtoBuf.IExtensible
  {
    public Foo() {}

    private int _value = (int)123;
    [global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"value", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
    [global::System.ComponentModel.DefaultValue((int)123)]
    public int value
    {
      get { return _value; }
      set { _value = value; }
    }
    private global::ProtoBuf.IExtension extensionObject;
    global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
      { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
  }

}

      



This explicitly contains a default value both in the field initializer and via DefaultValueAttribute

.

If I add a parameter -p:detectMissing

, I get:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

// Option: missing-value detection (*Specified/ShouldSerialize*/Reset*) enabled

// Generated from: my.proto
namespace my
{
  [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Foo")]
  public partial class Foo : global::ProtoBuf.IExtensible
  {
    public Foo() {}

    private int? _value;
    [global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"value", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
    public int value
    {
      get { return _value?? (int)123; }
      set { _value = value; }
    }
    [global::System.Xml.Serialization.XmlIgnore]
    [global::System.ComponentModel.Browsable(false)]
    public bool valueSpecified
    {
      get { return this._value != null; }
      set { if (value == (this._value== null)) this._value = value ? this.value : (int?)null; }
    }
    private bool ShouldSerializevalue() { return valueSpecified; }
    private void Resetvalue() { valueSpecified = false; }

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

}

      

This is now the default for "getter".

+1


source







All Articles