From 739d13d5d1f93e8bf8770081d1500372758fffa1 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Tue, 14 Jul 2015 14:26:31 +0100 Subject: [PATCH] Generate the well-known types in C# This involves: - Specifying a namespace in each proto (including ones we'd previously missed) - Updating the generation script - Changing codegen to implement IReflectedMessage.Fields explicitly (a good thing anyway) - Changing reflection tests to take account of the explicit interface implementation Non-generated code in this commit; generated code to follow --- csharp/generate_protos.sh | 12 ++++++++++ .../GeneratedMessageTest.cs | 22 ++++++++++--------- .../ProtocolBuffers/ProtocolBuffers.csproj | 10 +++++++++ src/google/protobuf/any.proto | 2 +- src/google/protobuf/api.proto | 1 + .../compiler/csharp/csharp_message.cc | 2 +- src/google/protobuf/duration.proto | 2 +- src/google/protobuf/empty.proto | 1 + src/google/protobuf/field_mask.proto | 2 +- src/google/protobuf/source_context.proto | 1 + src/google/protobuf/struct.proto | 2 +- src/google/protobuf/timestamp.proto | 2 +- src/google/protobuf/type.proto | 1 + src/google/protobuf/wrappers.proto | 2 +- 14 files changed, 45 insertions(+), 17 deletions(-) diff --git a/csharp/generate_protos.sh b/csharp/generate_protos.sh index 8ec103cd48..b7f837e181 100755 --- a/csharp/generate_protos.sh +++ b/csharp/generate_protos.sh @@ -42,6 +42,18 @@ $PROTOC -Isrc --csharp_out=csharp/src/ProtocolBuffers/Reflection \ src/google/protobuf/descriptor_proto_file.proto rm src/google/protobuf/descriptor_proto_file.proto +$PROTOC -Isrc --csharp_out=csharp/src/ProtocolBuffers/WellKnownTypes \ + src/google/protobuf/any.proto \ + src/google/protobuf/api.proto \ + src/google/protobuf/duration.proto \ + src/google/protobuf/empty.proto \ + src/google/protobuf/field_mask.proto \ + src/google/protobuf/source_context.proto \ + src/google/protobuf/struct.proto \ + src/google/protobuf/timestamp.proto \ + src/google/protobuf/type.proto \ + src/google/protobuf/wrappers.proto + $PROTOC -Isrc --csharp_out=csharp/src/ProtocolBuffers.Test/TestProtos \ src/google/protobuf/map_unittest_proto3.proto \ src/google/protobuf/unittest_proto3.proto \ diff --git a/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs b/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs index 28c2195f9f..acb20b1572 100644 --- a/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs +++ b/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs @@ -604,7 +604,7 @@ namespace Google.Protobuf public void Reflection_GetValue() { var message = SampleMessages.CreateFullTestAllTypes(); - var fields = message.Fields; + var fields = ((IReflectedMessage) message).Fields; Assert.AreEqual(message.SingleBool, fields[TestAllTypes.SingleBoolFieldNumber].GetValue(message)); Assert.AreEqual(message.SingleBytes, fields[TestAllTypes.SingleBytesFieldNumber].GetValue(message)); Assert.AreEqual(message.SingleDouble, fields[TestAllTypes.SingleDoubleFieldNumber].GetValue(message)); @@ -639,7 +639,8 @@ namespace Google.Protobuf // Just a single map field, for the same reason var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } }; - var dictionary = (IDictionary)mapMessage.Fields[TestMap.MapStringStringFieldNumber].GetValue(mapMessage); + fields = ((IReflectedMessage) mapMessage).Fields; + var dictionary = (IDictionary) fields[TestMap.MapStringStringFieldNumber].GetValue(mapMessage); Assert.AreEqual(mapMessage.MapStringString, dictionary); Assert.AreEqual("value1", dictionary["key1"]); } @@ -647,7 +648,7 @@ namespace Google.Protobuf [Test] public void Reflection_Clear() { - var message = SampleMessages.CreateFullTestAllTypes(); + IReflectedMessage message = SampleMessages.CreateFullTestAllTypes(); var fields = message.Fields; fields[TestAllTypes.SingleBoolFieldNumber].Clear(message); fields[TestAllTypes.SingleInt32FieldNumber].Clear(message); @@ -672,7 +673,8 @@ namespace Google.Protobuf // Separately, maps. var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } }; - mapMessage.Fields[TestMap.MapStringStringFieldNumber].Clear(mapMessage); + fields = ((IReflectedMessage) mapMessage).Fields; + fields[TestMap.MapStringStringFieldNumber].Clear(mapMessage); Assert.AreEqual(0, mapMessage.MapStringString.Count); } @@ -680,7 +682,7 @@ namespace Google.Protobuf public void Reflection_SetValue_SingleFields() { // Just a sample (primitives, messages, enums, strings, byte strings) - var message = SampleMessages.CreateFullTestAllTypes(); + IReflectedMessage message = SampleMessages.CreateFullTestAllTypes(); var fields = message.Fields; fields[TestAllTypes.SingleBoolFieldNumber].SetValue(message, false); fields[TestAllTypes.SingleInt32FieldNumber].SetValue(message, 500); @@ -707,7 +709,7 @@ namespace Google.Protobuf [Test] public void Reflection_SetValue_SingleFields_WrongType() { - var message = SampleMessages.CreateFullTestAllTypes(); + IReflectedMessage message = SampleMessages.CreateFullTestAllTypes(); var fields = message.Fields; Assert.Throws(() => fields[TestAllTypes.SingleBoolFieldNumber].SetValue(message, "This isn't a bool")); } @@ -715,7 +717,7 @@ namespace Google.Protobuf [Test] public void Reflection_SetValue_MapFields() { - var message = new TestMap(); + IReflectedMessage message = new TestMap(); var fields = message.Fields; Assert.Throws(() => fields[TestMap.MapStringStringFieldNumber].SetValue(message, new Dictionary())); } @@ -723,7 +725,7 @@ namespace Google.Protobuf [Test] public void Reflection_SetValue_RepeatedFields() { - var message = SampleMessages.CreateFullTestAllTypes(); + IReflectedMessage message = SampleMessages.CreateFullTestAllTypes(); var fields = message.Fields; Assert.Throws(() => fields[TestAllTypes.RepeatedDoubleFieldNumber].SetValue(message, new double[10])); } @@ -731,7 +733,7 @@ namespace Google.Protobuf [Test] public void Reflection_GetValue_IncorrectType() { - var message = SampleMessages.CreateFullTestAllTypes(); + IReflectedMessage message = SampleMessages.CreateFullTestAllTypes(); Assert.Throws(() => message.Fields[TestAllTypes.SingleBoolFieldNumber].GetValue(new TestMap())); } @@ -739,7 +741,7 @@ namespace Google.Protobuf public void Reflection_Oneof() { var message = new TestAllTypes(); - var fields = message.Fields; + var fields = ((IReflectedMessage) message).Fields; Assert.AreEqual(1, fields.Oneofs.Count); var oneof = fields.Oneofs[0]; Assert.AreEqual("oneof_field", oneof.Descriptor.Name); diff --git a/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj b/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj index eef54391f0..f529c19a2e 100644 --- a/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj +++ b/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj @@ -96,6 +96,16 @@ + + + + + + + + + + diff --git a/src/google/protobuf/any.proto b/src/google/protobuf/any.proto index 458dfff234..d3ad3acc60 100644 --- a/src/google/protobuf/any.proto +++ b/src/google/protobuf/any.proto @@ -35,7 +35,7 @@ option java_generate_equals_and_hash = true; option java_multiple_files = true; option java_outer_classname = "AnyProto"; option java_package = "com.google.protobuf"; -option csharp_namespace = "Google.Protobuf"; +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; option objc_class_prefix = "GPB"; diff --git a/src/google/protobuf/api.proto b/src/google/protobuf/api.proto index f368c24d0a..d6c2a8b35c 100644 --- a/src/google/protobuf/api.proto +++ b/src/google/protobuf/api.proto @@ -37,6 +37,7 @@ import "google/protobuf/type.proto"; option java_multiple_files = true; option java_outer_classname = "ApiProto"; option java_package = "com.google.protobuf"; +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; option objc_class_prefix = "GPB"; diff --git a/src/google/protobuf/compiler/csharp/csharp_message.cc b/src/google/protobuf/compiler/csharp/csharp_message.cc index 7cbba911d0..4acc899bd2 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message.cc @@ -215,7 +215,7 @@ void MessageGenerator::Generate(io::Printer* printer) { " get { return $descriptor_accessor$; }\n" "}\n" "\n" - "public pbr::FieldAccessorTable Fields {\n" + "pbr::FieldAccessorTable pb::IReflectedMessage.Fields {\n" " get { return $umbrella_class_name$.internal__$identifier$__FieldAccessorTable; }\n" "}\n" "\n" diff --git a/src/google/protobuf/duration.proto b/src/google/protobuf/duration.proto index dc6b575f0b..0762c3c281 100644 --- a/src/google/protobuf/duration.proto +++ b/src/google/protobuf/duration.proto @@ -35,7 +35,7 @@ option java_generate_equals_and_hash = true; option java_multiple_files = true; option java_outer_classname = "DurationProto"; option java_package = "com.google.protobuf"; -option csharp_namespace = "Google.Protobuf"; +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; option objc_class_prefix = "GPB"; // A Duration represents a signed, fixed-length span of time represented diff --git a/src/google/protobuf/empty.proto b/src/google/protobuf/empty.proto index 94df039714..363ec175fe 100644 --- a/src/google/protobuf/empty.proto +++ b/src/google/protobuf/empty.proto @@ -34,6 +34,7 @@ package google.protobuf; option java_multiple_files = true; option java_outer_classname = "EmptyProto"; option java_package = "com.google.protobuf"; +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; option objc_class_prefix = "GPB"; diff --git a/src/google/protobuf/field_mask.proto b/src/google/protobuf/field_mask.proto index ad7a1dfaab..c19f4410e7 100644 --- a/src/google/protobuf/field_mask.proto +++ b/src/google/protobuf/field_mask.proto @@ -34,7 +34,7 @@ package google.protobuf; option java_multiple_files = true; option java_outer_classname = "FieldMaskProto"; option java_package = "com.google.protobuf"; -option csharp_namespace = "Google.Protobuf"; +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; option objc_class_prefix = "GPB"; diff --git a/src/google/protobuf/source_context.proto b/src/google/protobuf/source_context.proto index 2c8a17a868..98d4920a47 100644 --- a/src/google/protobuf/source_context.proto +++ b/src/google/protobuf/source_context.proto @@ -34,6 +34,7 @@ package google.protobuf; option java_multiple_files = true; option java_outer_classname = "SourceContextProto"; option java_package = "com.google.protobuf"; +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; option objc_class_prefix = "GPB"; diff --git a/src/google/protobuf/struct.proto b/src/google/protobuf/struct.proto index a0ec961d7b..4ff10cd0d9 100644 --- a/src/google/protobuf/struct.proto +++ b/src/google/protobuf/struct.proto @@ -35,7 +35,7 @@ option java_generate_equals_and_hash = true; option java_multiple_files = true; option java_outer_classname = "StructProto"; option java_package = "com.google.protobuf"; -option csharp_namespace = "Google.Protobuf"; +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; option objc_class_prefix = "GPB"; diff --git a/src/google/protobuf/timestamp.proto b/src/google/protobuf/timestamp.proto index d49bd766bd..11f258da84 100644 --- a/src/google/protobuf/timestamp.proto +++ b/src/google/protobuf/timestamp.proto @@ -35,7 +35,7 @@ option java_generate_equals_and_hash = true; option java_multiple_files = true; option java_outer_classname = "TimestampProto"; option java_package = "com.google.protobuf"; -option csharp_namespace = "Google.Protobuf"; +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; option objc_class_prefix = "GPB"; diff --git a/src/google/protobuf/type.proto b/src/google/protobuf/type.proto index ace5d995a4..ce22d33dc0 100644 --- a/src/google/protobuf/type.proto +++ b/src/google/protobuf/type.proto @@ -37,6 +37,7 @@ import "google/protobuf/source_context.proto"; option java_multiple_files = true; option java_outer_classname = "TypeProto"; option java_package = "com.google.protobuf"; +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; option objc_class_prefix = "GPB"; diff --git a/src/google/protobuf/wrappers.proto b/src/google/protobuf/wrappers.proto index 14725524e1..6d3181bfc8 100644 --- a/src/google/protobuf/wrappers.proto +++ b/src/google/protobuf/wrappers.proto @@ -40,7 +40,7 @@ package google.protobuf; option java_multiple_files = true; option java_outer_classname = "WrappersProto"; option java_package = "com.google.protobuf"; -option csharp_namespace = "Google.Protobuf"; +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; option objc_class_prefix = "GPB";