silentorbit / protobuf

C# code generator for reading and writing the protocol buffers format
https://silentorbit.com/protobuf/
MIT License
307 stars 105 forks source link

Default values for string field generate invalid C# code #21

Closed ido-ran closed 9 years ago

ido-ran commented 9 years ago

I've created a message with default value like so:

message ProtocolVersion {
  required string current = 1 [default = "2.0.0"];
}

and it generate the following code which did not compile:

        /// <summary>Takes the remaining content of the stream and deserialze it into the instance.</summary>
        public static Test.Protocol.ProtocolVersion Deserialize(Stream stream, Test.Protocol.ProtocolVersion instance)
        {
            instance.Current = 2.0.0;   <------- This should have been a string
            instance.ServerSupported = 2.x;
            while (true)
            {
            ....

The default value was not wrapped with quotes, it was emitted bare.

Another problem is that the message class does not have the default value in it:

    public partial class ProtocolVersion
    {
        /// <summary> The current version of this protocol</summary>
        public string Current { get; set; }
    }

In other languages when creating new instance of ProtocolVersion message and getting the value of Current it will return the default value.

I use it as a way of adding "constant" to the protocol buffer.