Commit 1851676b authored by csharptest's avatar csharptest Committed by rogerk

Allow field names that begin with underscore and number '_0'

parent 50a89c1c
...@@ -80,3 +80,9 @@ message B { ...@@ -80,3 +80,9 @@ message B {
message AB { message AB {
optional int32 a_b = 1; optional int32 a_b = 1;
} }
// Similar issue with numberic names
message NumberField {
optional int32 _01 = 1;
}
...@@ -75,6 +75,9 @@ namespace Google.ProtocolBuffers.ProtoGen ...@@ -75,6 +75,9 @@ namespace Google.ProtocolBuffers.ProtoGen
public void Generate(TextGenerator writer) public void Generate(TextGenerator writer)
{ {
if (Descriptor.File.CSharpOptions.ClsCompliance && GetFieldConstantName(Descriptor).StartsWith("_"))
writer.WriteLine("[global::System.CLSCompliant(false)]");
writer.WriteLine("public const int {0} = {1};", GetFieldConstantName(Descriptor), Descriptor.FieldNumber); writer.WriteLine("public const int {0} = {1};", GetFieldConstantName(Descriptor), Descriptor.FieldNumber);
if (UseLiteRuntime) if (UseLiteRuntime)
......
...@@ -246,6 +246,9 @@ namespace Google.ProtocolBuffers.ProtoGen ...@@ -246,6 +246,9 @@ namespace Google.ProtocolBuffers.ProtoGen
foreach (FieldDescriptor fieldDescriptor in Descriptor.Fields) foreach (FieldDescriptor fieldDescriptor in Descriptor.Fields)
{ {
if (Descriptor.File.CSharpOptions.ClsCompliance && GetFieldConstantName(fieldDescriptor).StartsWith("_"))
writer.WriteLine("[global::System.CLSCompliant(false)]");
// Rats: we lose the debug comment here :( // Rats: we lose the debug comment here :(
writer.WriteLine("public const int {0} = {1};", GetFieldConstantName(fieldDescriptor), writer.WriteLine("public const int {0} = {1};", GetFieldConstantName(fieldDescriptor),
fieldDescriptor.FieldNumber); fieldDescriptor.FieldNumber);
......
...@@ -352,7 +352,12 @@ namespace Google.ProtocolBuffers.Descriptors ...@@ -352,7 +352,12 @@ namespace Google.ProtocolBuffers.Descriptors
public bool IsCLSCompliant public bool IsCLSCompliant
{ {
get { return mappedType != MappedType.UInt32 && mappedType != MappedType.UInt64; } get
{
return mappedType != MappedType.UInt32 &&
mappedType != MappedType.UInt64 &&
!NameHelpers.UnderscoresToPascalCase(Name).StartsWith("_");
}
} }
public int FieldNumber public int FieldNumber
......
...@@ -67,12 +67,22 @@ namespace Google.ProtocolBuffers ...@@ -67,12 +67,22 @@ namespace Google.ProtocolBuffers
private static string UnderscoresToPascalOrCamelCase(string input, bool pascal) private static string UnderscoresToPascalOrCamelCase(string input, bool pascal)
{ {
string name = Transform(input, pascal ? UnderlineToPascal : UnderlineToCamel, x => x.Value.TrimStart('_').ToUpper()); string name = Transform(input, pascal ? UnderlineToPascal : UnderlineToCamel, x => x.Value.TrimStart('_').ToUpper());
if (!pascal && name.Length > 0 && Char.IsUpper(name[0]))
if (name.Length == 0)
throw new ArgumentException(String.Format("The field name '{0}' is invalid.", input));
// Pascal case always begins with lower-case letter
if (!pascal && Char.IsUpper(name[0]))
{ {
char[] chars = name.ToCharArray(); char[] chars = name.ToCharArray();
chars[0] = char.ToLower(chars[0]); chars[0] = char.ToLower(chars[0]);
return new string(chars); return new string(chars);
} }
// Fields can not start with a number
if (Char.IsNumber(name[0]))
name = '_' + name;
return name; return name;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment