Commit bbefae3c authored by csharptest's avatar csharptest Committed by rogerk

Merged branch for issue 13

parents 25981d40 bde57ffc
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
<WorkingDirectories Include="$(BuildTempDirectory)" /> <WorkingDirectories Include="$(BuildTempDirectory)" />
<WorkingDirectories Include="$(BuildOutputDirectory)" /> <WorkingDirectories Include="$(BuildOutputDirectory)" />
<Protos Include="$(ProtosDirectory)\extest\unittest_issues.proto" />
<Protos Include="$(ProtosDirectory)\extest\unittest_extras_full.proto" /> <Protos Include="$(ProtosDirectory)\extest\unittest_extras_full.proto" />
<Protos Include="$(ProtosDirectory)\extest\unittest_extras_lite.proto" /> <Protos Include="$(ProtosDirectory)\extest\unittest_extras_lite.proto" />
<Protos Include="$(ProtosDirectory)\extest\unittest_extras_xmltest.proto" /> <Protos Include="$(ProtosDirectory)\extest\unittest_extras_xmltest.proto" />
...@@ -71,6 +72,9 @@ ...@@ -71,6 +72,9 @@
<TargetDirectory>$(SourceDirectory)\AddressBook</TargetDirectory> <TargetDirectory>$(SourceDirectory)\AddressBook</TargetDirectory>
</GeneratedSource> </GeneratedSource>
<!-- Unit test --> <!-- Unit test -->
<GeneratedSource Include="$(BuildTempDirectory)\UnitTestExtrasIssuesProtoFile.cs">
<TargetDirectory>$(SourceDirectory)\ProtocolBuffers.Test\TestProtos</TargetDirectory>
</GeneratedSource>
<GeneratedSource Include="$(BuildTempDirectory)\UnitTestXmlSerializerTestProtoFile.cs"> <GeneratedSource Include="$(BuildTempDirectory)\UnitTestXmlSerializerTestProtoFile.cs">
<TargetDirectory>$(SourceDirectory)\ProtocolBuffers.Test\TestProtos</TargetDirectory> <TargetDirectory>$(SourceDirectory)\ProtocolBuffers.Test\TestProtos</TargetDirectory>
</GeneratedSource> </GeneratedSource>
......
// These proto descriptors have at one time been reported as an issue or defect.
// They are kept here to replicate the issue, and continue to verify the fix.
import "google/protobuf/csharp_options.proto";
// Issue: Non-"Google.Protobuffers" namespace will ensure that protobuffer library types are qualified
option (google.protobuf.csharp_file_options).namespace = "UnitTest.Issues.TestProtos";
option (google.protobuf.csharp_file_options).umbrella_classname = "UnitTestExtrasIssuesProtoFile";
package unittest_issues;
option optimize_for = SPEED;
// The following is a representative set of features
/*
enum EnumOptions {
ONE = 0;
TWO = 1;
THREE = 2;
}
message TestBasicChild
{
repeated EnumOptions options = 3;
optional bytes binary = 4;
}
message TestBasicNoFields {
}
message TestBasicRescursive {
optional TestBasicRescursive child = 1;
}
message TestBasicMessage {
optional int64 number = 6;
repeated int32 numbers = 2;
optional string text = 3;
repeated string textlines = 700;
optional bool valid = 5;
optional TestBasicChild child = 1;
repeated group Children = 401
{
repeated EnumOptions options = 3;
optional bytes binary = 4;
}
extensions 100 to 199;
}
message TestBasicExtension {
required int32 number = 1;
}
extend TestBasicMessage {
optional EnumOptions extension_enum = 101;
optional string extension_text = 102;
repeated int32 extension_number = 103 [packed = true];
optional TestBasicExtension extension_message = 199;
}
// Issue for non-qualified type reference in new services generation
option (google.protobuf.csharp_file_options).service_generator_type = IRPCDISPATCH;
service TestGenericService {
rpc Foo(TestBasicNoFields) returns (TestBasicMessage);
rpc Bar(TestBasicNoFields) returns (TestBasicMessage);
}
*/
// Issue 13: http://code.google.com/p/protobuf-csharp-port/issues/detail?id=13
message A {
optional int32 _A = 1;
}
message B {
optional int32 B_ = 1;
}
message AB {
optional int32 a_b = 1;
}
// Similar issue with numeric names
message NumberField {
optional int32 _01 = 1;
}
...@@ -75,6 +75,11 @@ namespace Google.ProtocolBuffers.ProtoGen ...@@ -75,6 +75,11 @@ 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,11 @@ namespace Google.ProtocolBuffers.ProtoGen ...@@ -246,6 +246,11 @@ 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);
......
...@@ -48,6 +48,11 @@ namespace Google.ProtocolBuffers ...@@ -48,6 +48,11 @@ namespace Google.ProtocolBuffers
Assert.AreEqual("FooBar", NameHelpers.UnderscoresToPascalCase("foo_bar")); Assert.AreEqual("FooBar", NameHelpers.UnderscoresToPascalCase("foo_bar"));
Assert.AreEqual("Foo0Bar", NameHelpers.UnderscoresToPascalCase("Foo0bar")); Assert.AreEqual("Foo0Bar", NameHelpers.UnderscoresToPascalCase("Foo0bar"));
Assert.AreEqual("FooBar", NameHelpers.UnderscoresToPascalCase("Foo_+_Bar")); Assert.AreEqual("FooBar", NameHelpers.UnderscoresToPascalCase("Foo_+_Bar"));
Assert.AreEqual("Bar", NameHelpers.UnderscoresToPascalCase("__+bar"));
Assert.AreEqual("Bar", NameHelpers.UnderscoresToPascalCase("bar_"));
Assert.AreEqual("_0Bar", NameHelpers.UnderscoresToPascalCase("_0bar"));
Assert.AreEqual("_1Bar", NameHelpers.UnderscoresToPascalCase("_1_bar"));
} }
[Test] [Test]
...@@ -57,6 +62,11 @@ namespace Google.ProtocolBuffers ...@@ -57,6 +62,11 @@ namespace Google.ProtocolBuffers
Assert.AreEqual("fooBar", NameHelpers.UnderscoresToCamelCase("foo_bar")); Assert.AreEqual("fooBar", NameHelpers.UnderscoresToCamelCase("foo_bar"));
Assert.AreEqual("foo0Bar", NameHelpers.UnderscoresToCamelCase("Foo0bar")); Assert.AreEqual("foo0Bar", NameHelpers.UnderscoresToCamelCase("Foo0bar"));
Assert.AreEqual("fooBar", NameHelpers.UnderscoresToCamelCase("Foo_+_Bar")); Assert.AreEqual("fooBar", NameHelpers.UnderscoresToCamelCase("Foo_+_Bar"));
Assert.AreEqual("bar", NameHelpers.UnderscoresToCamelCase("__+bar"));
Assert.AreEqual("bar", NameHelpers.UnderscoresToCamelCase("bar_"));
Assert.AreEqual("_0Bar", NameHelpers.UnderscoresToCamelCase("_0bar"));
Assert.AreEqual("_1Bar", NameHelpers.UnderscoresToCamelCase("_1_bar"));
} }
[Test] [Test]
......
...@@ -104,6 +104,7 @@ ...@@ -104,6 +104,7 @@
<Compile Include="TestProtos\UnitTestCustomOptionsProtoFile.cs" /> <Compile Include="TestProtos\UnitTestCustomOptionsProtoFile.cs" />
<Compile Include="TestProtos\UnitTestEmbedOptimizeForProtoFile.cs" /> <Compile Include="TestProtos\UnitTestEmbedOptimizeForProtoFile.cs" />
<Compile Include="TestProtos\UnitTestEmptyProtoFile.cs" /> <Compile Include="TestProtos\UnitTestEmptyProtoFile.cs" />
<Compile Include="TestProtos\UnitTestExtrasIssuesProtoFile.cs" />
<Compile Include="TestProtos\UnitTestGenericServices.cs" /> <Compile Include="TestProtos\UnitTestGenericServices.cs" />
<Compile Include="TestProtos\UnitTestGoogleSizeProtoFile.cs" /> <Compile Include="TestProtos\UnitTestGoogleSizeProtoFile.cs" />
<Compile Include="TestProtos\UnitTestGoogleSpeedProtoFile.cs" /> <Compile Include="TestProtos\UnitTestGoogleSpeedProtoFile.cs" />
......
...@@ -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
......
...@@ -177,7 +177,7 @@ namespace Google.ProtocolBuffers ...@@ -177,7 +177,7 @@ namespace Google.ProtocolBuffers
/// <summary> /// <summary>
/// Reads an array of primitive values into the list, if the wire-type of fieldTag is length-prefixed and the /// Reads an array of primitive values into the list, if the wire-type of fieldTag is length-prefixed and the
/// type is numberic, it will read a packed array. /// type is numeric, it will read a packed array.
/// </summary> /// </summary>
[CLSCompliant(false)] [CLSCompliant(false)]
void ReadPrimitiveArray(FieldType fieldType, uint fieldTag, string fieldName, ICollection<object> list); void ReadPrimitiveArray(FieldType fieldType, uint fieldTag, string fieldName, ICollection<object> list);
......
...@@ -34,8 +34,8 @@ ...@@ -34,8 +34,8 @@
#endregion #endregion
using System.Globalization; using System;
using System.Text; using System.Text.RegularExpressions;
namespace Google.ProtocolBuffers namespace Google.ProtocolBuffers
{ {
...@@ -44,69 +44,74 @@ namespace Google.ProtocolBuffers ...@@ -44,69 +44,74 @@ namespace Google.ProtocolBuffers
/// </summary> /// </summary>
public class NameHelpers public class NameHelpers
{ {
/// <summary>
/// All characters that are not alpha-numeric
/// </summary>
private static readonly Regex NonAlphaNumericCharacters = new Regex(@"[^a-zA-Z0-9]+");
/// <summary>
/// Matches lower-case character that follow either an underscore, or a number
/// </summary>
private static readonly Regex UnderscoreOrNumberWithLowerCase = new Regex(@"[0-9_][a-z]");
/// <summary>
/// Removes non alpha numeric characters while capitalizing letters that follow
/// a number or underscore. The first letter is always upper case.
/// </summary>
public static string UnderscoresToPascalCase(string input) public static string UnderscoresToPascalCase(string input)
{ {
return UnderscoresToPascalOrCamelCase(input, true); string name = UnderscoresToUpperCase(input);
}
public static string UnderscoresToCamelCase(string input) // Pascal case always begins with upper-case letter
if (Char.IsLower(name[0]))
{ {
return UnderscoresToPascalOrCamelCase(input, false); char[] chars = name.ToCharArray();
chars[0] = char.ToUpper(chars[0]);
return new string(chars);
}
return name;
} }
/// <summary> /// <summary>
/// Converts a string to Pascal or Camel case. The first letter is capitalized or /// Removes non alpha numeric characters while capitalizing letters that follow
/// lower-cased depending on <paramref name="pascal"/> is true. /// a number or underscore. The first letter is always lower case.
/// After the first letter, any punctuation is removed but triggers capitalization
/// of the next letter. Digits are preserved but trigger capitalization of the next
/// letter.
/// All capitalisation is done in the invariant culture.
/// </summary> /// </summary>
private static string UnderscoresToPascalOrCamelCase(string input, bool pascal) public static string UnderscoresToCamelCase(string input)
{
StringBuilder result = new StringBuilder();
bool capitaliseNext = pascal;
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
if ('a' <= c && c <= 'z')
{
if (capitaliseNext)
{ {
result.Append(char.ToUpper(c, CultureInfo.InvariantCulture)); string name = UnderscoresToUpperCase(input);
}
else // Camel case always begins with lower-case letter
if (Char.IsUpper(name[0]))
{ {
result.Append(c); char[] chars = name.ToCharArray();
chars[0] = char.ToLower(chars[0]);
return new string(chars);
} }
capitaliseNext = false; return name;
} }
else if ('A' <= c && c <= 'Z')
{ /// <summary>
if (i == 0 && !pascal) /// Capitalizes any characters following an '_' or a number '0' - '9' and removes
{ /// all non alpha-numeric characters. If the resulting string begins with a number
// Force first letter to lower-case unless explicitly told to /// an '_' will be prefixed.
// capitalize it. /// </summary>
result.Append(char.ToLower(c, CultureInfo.InvariantCulture)); private static string UnderscoresToUpperCase(string input)
}
else
{ {
// Capital letters after the first are left as-is. string name = UnderscoreOrNumberWithLowerCase.Replace(input, x => x.Value.ToUpper());
result.Append(c); name = NonAlphaNumericCharacters.Replace(name, String.Empty);
}
capitaliseNext = false; if (name.Length == 0)
}
else if ('0' <= c && c <= '9')
{ {
result.Append(c); throw new ArgumentException(String.Format("The field name '{0}' is invalid.", input));
capitaliseNext = true;
} }
else
// Fields can not start with a number
if (Char.IsNumber(name[0]))
{ {
capitaliseNext = true; name = '_' + name;
}
} }
return result.ToString();
return name;
} }
internal static string StripProto(string text) internal static string StripProto(string text)
......
...@@ -23,6 +23,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "unittest", "unittest", "{C8 ...@@ -23,6 +23,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "unittest", "unittest", "{C8
..\protos\extest\unittest_generic_services.proto = ..\protos\extest\unittest_generic_services.proto ..\protos\extest\unittest_generic_services.proto = ..\protos\extest\unittest_generic_services.proto
..\protos\google\protobuf\unittest_import.proto = ..\protos\google\protobuf\unittest_import.proto ..\protos\google\protobuf\unittest_import.proto = ..\protos\google\protobuf\unittest_import.proto
..\protos\google\protobuf\unittest_import_lite.proto = ..\protos\google\protobuf\unittest_import_lite.proto ..\protos\google\protobuf\unittest_import_lite.proto = ..\protos\google\protobuf\unittest_import_lite.proto
..\protos\extest\unittest_issues.proto = ..\protos\extest\unittest_issues.proto
..\protos\google\protobuf\unittest_lite.proto = ..\protos\google\protobuf\unittest_lite.proto ..\protos\google\protobuf\unittest_lite.proto = ..\protos\google\protobuf\unittest_lite.proto
..\protos\google\protobuf\unittest_lite_imports_nonlite.proto = ..\protos\google\protobuf\unittest_lite_imports_nonlite.proto ..\protos\google\protobuf\unittest_lite_imports_nonlite.proto = ..\protos\google\protobuf\unittest_lite_imports_nonlite.proto
..\protos\google\protobuf\unittest_mset.proto = ..\protos\google\protobuf\unittest_mset.proto ..\protos\google\protobuf\unittest_mset.proto = ..\protos\google\protobuf\unittest_mset.proto
...@@ -67,7 +68,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{66ED1950 ...@@ -67,7 +68,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{66ED1950
..\build\RunBenchmarks.bat = ..\build\RunBenchmarks.bat ..\build\RunBenchmarks.bat = ..\build\RunBenchmarks.bat
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtocolBuffers.Serialization", "ProtocolBuffers.Serialization\ProtocolBuffers.Serialization.csproj", "{231391AF-449C-4a39-986C-AD7F270F4750}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtocolBuffers.Serialization", "ProtocolBuffers.Serialization\ProtocolBuffers.Serialization.csproj", "{231391AF-449C-4A39-986C-AD7F270F4750}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
...@@ -147,14 +148,10 @@ Global ...@@ -147,14 +148,10 @@ Global
{EEFFED24-3750-4567-9A23-1DB676A15610}.Release_Silverlight2|Any CPU.ActiveCfg = Release|Any CPU {EEFFED24-3750-4567-9A23-1DB676A15610}.Release_Silverlight2|Any CPU.ActiveCfg = Release|Any CPU
{EEFFED24-3750-4567-9A23-1DB676A15610}.Release|Any CPU.ActiveCfg = Release|Any CPU {EEFFED24-3750-4567-9A23-1DB676A15610}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEFFED24-3750-4567-9A23-1DB676A15610}.Release|Any CPU.Build.0 = Release|Any CPU {EEFFED24-3750-4567-9A23-1DB676A15610}.Release|Any CPU.Build.0 = Release|Any CPU
{231391AF-449C-4a39-986C-AD7F270F4750}.Debug_Silverlight2|Any CPU.ActiveCfg = Debug_Silverlight2|Any CPU {231391AF-449C-4A39-986C-AD7F270F4750}.Debug_Silverlight2|Any CPU.ActiveCfg = Debug_Silverlight2|Any CPU
{231391AF-449C-4a39-986C-AD7F270F4750}.Debug_Silverlight2|Any CPU.Build.0 = Debug_Silverlight2|Any CPU {231391AF-449C-4A39-986C-AD7F270F4750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{231391AF-449C-4a39-986C-AD7F270F4750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {231391AF-449C-4A39-986C-AD7F270F4750}.Release_Silverlight2|Any CPU.ActiveCfg = Release_Silverlight2|Any CPU
{231391AF-449C-4a39-986C-AD7F270F4750}.Debug|Any CPU.Build.0 = Debug|Any CPU {231391AF-449C-4A39-986C-AD7F270F4750}.Release|Any CPU.ActiveCfg = Release|Any CPU
{231391AF-449C-4a39-986C-AD7F270F4750}.Release_Silverlight2|Any CPU.ActiveCfg = Release_Silverlight2|Any CPU
{231391AF-449C-4a39-986C-AD7F270F4750}.Release_Silverlight2|Any CPU.Build.0 = Release_Silverlight2|Any CPU
{231391AF-449C-4a39-986C-AD7F270F4750}.Release|Any CPU.ActiveCfg = Release|Any CPU
{231391AF-449C-4a39-986C-AD7F270F4750}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
......
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