Commit 739d13d5 authored by Jon Skeet's avatar Jon Skeet

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
parent 1660c563
...@@ -42,6 +42,18 @@ $PROTOC -Isrc --csharp_out=csharp/src/ProtocolBuffers/Reflection \ ...@@ -42,6 +42,18 @@ $PROTOC -Isrc --csharp_out=csharp/src/ProtocolBuffers/Reflection \
src/google/protobuf/descriptor_proto_file.proto src/google/protobuf/descriptor_proto_file.proto
rm 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 \ $PROTOC -Isrc --csharp_out=csharp/src/ProtocolBuffers.Test/TestProtos \
src/google/protobuf/map_unittest_proto3.proto \ src/google/protobuf/map_unittest_proto3.proto \
src/google/protobuf/unittest_proto3.proto \ src/google/protobuf/unittest_proto3.proto \
......
...@@ -604,7 +604,7 @@ namespace Google.Protobuf ...@@ -604,7 +604,7 @@ namespace Google.Protobuf
public void Reflection_GetValue() public void Reflection_GetValue()
{ {
var message = SampleMessages.CreateFullTestAllTypes(); 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.SingleBool, fields[TestAllTypes.SingleBoolFieldNumber].GetValue(message));
Assert.AreEqual(message.SingleBytes, fields[TestAllTypes.SingleBytesFieldNumber].GetValue(message)); Assert.AreEqual(message.SingleBytes, fields[TestAllTypes.SingleBytesFieldNumber].GetValue(message));
Assert.AreEqual(message.SingleDouble, fields[TestAllTypes.SingleDoubleFieldNumber].GetValue(message)); Assert.AreEqual(message.SingleDouble, fields[TestAllTypes.SingleDoubleFieldNumber].GetValue(message));
...@@ -639,7 +639,8 @@ namespace Google.Protobuf ...@@ -639,7 +639,8 @@ namespace Google.Protobuf
// Just a single map field, for the same reason // Just a single map field, for the same reason
var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } }; 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(mapMessage.MapStringString, dictionary);
Assert.AreEqual("value1", dictionary["key1"]); Assert.AreEqual("value1", dictionary["key1"]);
} }
...@@ -647,7 +648,7 @@ namespace Google.Protobuf ...@@ -647,7 +648,7 @@ namespace Google.Protobuf
[Test] [Test]
public void Reflection_Clear() public void Reflection_Clear()
{ {
var message = SampleMessages.CreateFullTestAllTypes(); IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Fields; var fields = message.Fields;
fields[TestAllTypes.SingleBoolFieldNumber].Clear(message); fields[TestAllTypes.SingleBoolFieldNumber].Clear(message);
fields[TestAllTypes.SingleInt32FieldNumber].Clear(message); fields[TestAllTypes.SingleInt32FieldNumber].Clear(message);
...@@ -672,7 +673,8 @@ namespace Google.Protobuf ...@@ -672,7 +673,8 @@ namespace Google.Protobuf
// Separately, maps. // Separately, maps.
var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } }; 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); Assert.AreEqual(0, mapMessage.MapStringString.Count);
} }
...@@ -680,7 +682,7 @@ namespace Google.Protobuf ...@@ -680,7 +682,7 @@ namespace Google.Protobuf
public void Reflection_SetValue_SingleFields() public void Reflection_SetValue_SingleFields()
{ {
// Just a sample (primitives, messages, enums, strings, byte strings) // Just a sample (primitives, messages, enums, strings, byte strings)
var message = SampleMessages.CreateFullTestAllTypes(); IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Fields; var fields = message.Fields;
fields[TestAllTypes.SingleBoolFieldNumber].SetValue(message, false); fields[TestAllTypes.SingleBoolFieldNumber].SetValue(message, false);
fields[TestAllTypes.SingleInt32FieldNumber].SetValue(message, 500); fields[TestAllTypes.SingleInt32FieldNumber].SetValue(message, 500);
...@@ -707,7 +709,7 @@ namespace Google.Protobuf ...@@ -707,7 +709,7 @@ namespace Google.Protobuf
[Test] [Test]
public void Reflection_SetValue_SingleFields_WrongType() public void Reflection_SetValue_SingleFields_WrongType()
{ {
var message = SampleMessages.CreateFullTestAllTypes(); IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Fields; var fields = message.Fields;
Assert.Throws<InvalidCastException>(() => fields[TestAllTypes.SingleBoolFieldNumber].SetValue(message, "This isn't a bool")); Assert.Throws<InvalidCastException>(() => fields[TestAllTypes.SingleBoolFieldNumber].SetValue(message, "This isn't a bool"));
} }
...@@ -715,7 +717,7 @@ namespace Google.Protobuf ...@@ -715,7 +717,7 @@ namespace Google.Protobuf
[Test] [Test]
public void Reflection_SetValue_MapFields() public void Reflection_SetValue_MapFields()
{ {
var message = new TestMap(); IReflectedMessage message = new TestMap();
var fields = message.Fields; var fields = message.Fields;
Assert.Throws<InvalidOperationException>(() => fields[TestMap.MapStringStringFieldNumber].SetValue(message, new Dictionary<string, string>())); Assert.Throws<InvalidOperationException>(() => fields[TestMap.MapStringStringFieldNumber].SetValue(message, new Dictionary<string, string>()));
} }
...@@ -723,7 +725,7 @@ namespace Google.Protobuf ...@@ -723,7 +725,7 @@ namespace Google.Protobuf
[Test] [Test]
public void Reflection_SetValue_RepeatedFields() public void Reflection_SetValue_RepeatedFields()
{ {
var message = SampleMessages.CreateFullTestAllTypes(); IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Fields; var fields = message.Fields;
Assert.Throws<InvalidOperationException>(() => fields[TestAllTypes.RepeatedDoubleFieldNumber].SetValue(message, new double[10])); Assert.Throws<InvalidOperationException>(() => fields[TestAllTypes.RepeatedDoubleFieldNumber].SetValue(message, new double[10]));
} }
...@@ -731,7 +733,7 @@ namespace Google.Protobuf ...@@ -731,7 +733,7 @@ namespace Google.Protobuf
[Test] [Test]
public void Reflection_GetValue_IncorrectType() public void Reflection_GetValue_IncorrectType()
{ {
var message = SampleMessages.CreateFullTestAllTypes(); IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
Assert.Throws<InvalidCastException>(() => message.Fields[TestAllTypes.SingleBoolFieldNumber].GetValue(new TestMap())); Assert.Throws<InvalidCastException>(() => message.Fields[TestAllTypes.SingleBoolFieldNumber].GetValue(new TestMap()));
} }
...@@ -739,7 +741,7 @@ namespace Google.Protobuf ...@@ -739,7 +741,7 @@ namespace Google.Protobuf
public void Reflection_Oneof() public void Reflection_Oneof()
{ {
var message = new TestAllTypes(); var message = new TestAllTypes();
var fields = message.Fields; var fields = ((IReflectedMessage) message).Fields;
Assert.AreEqual(1, fields.Oneofs.Count); Assert.AreEqual(1, fields.Oneofs.Count);
var oneof = fields.Oneofs[0]; var oneof = fields.Oneofs[0];
Assert.AreEqual("oneof_field", oneof.Descriptor.Name); Assert.AreEqual("oneof_field", oneof.Descriptor.Name);
......
...@@ -96,6 +96,16 @@ ...@@ -96,6 +96,16 @@
<Compile Include="Reflection\ServiceDescriptor.cs" /> <Compile Include="Reflection\ServiceDescriptor.cs" />
<Compile Include="Reflection\SingleFieldAccessor.cs" /> <Compile Include="Reflection\SingleFieldAccessor.cs" />
<Compile Include="ThrowHelper.cs" /> <Compile Include="ThrowHelper.cs" />
<Compile Include="WellKnownTypes\Any.cs" />
<Compile Include="WellKnownTypes\Api.cs" />
<Compile Include="WellKnownTypes\Duration.cs" />
<Compile Include="WellKnownTypes\Empty.cs" />
<Compile Include="WellKnownTypes\FieldMask.cs" />
<Compile Include="WellKnownTypes\SourceContext.cs" />
<Compile Include="WellKnownTypes\Struct.cs" />
<Compile Include="WellKnownTypes\Timestamp.cs" />
<Compile Include="WellKnownTypes\Type.cs" />
<Compile Include="WellKnownTypes\Wrappers.cs" />
<Compile Include="WireFormat.cs" /> <Compile Include="WireFormat.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
......
...@@ -35,7 +35,7 @@ option java_generate_equals_and_hash = true; ...@@ -35,7 +35,7 @@ option java_generate_equals_and_hash = true;
option java_multiple_files = true; option java_multiple_files = true;
option java_outer_classname = "AnyProto"; option java_outer_classname = "AnyProto";
option java_package = "com.google.protobuf"; option java_package = "com.google.protobuf";
option csharp_namespace = "Google.Protobuf"; option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option objc_class_prefix = "GPB"; option objc_class_prefix = "GPB";
......
...@@ -37,6 +37,7 @@ import "google/protobuf/type.proto"; ...@@ -37,6 +37,7 @@ import "google/protobuf/type.proto";
option java_multiple_files = true; option java_multiple_files = true;
option java_outer_classname = "ApiProto"; option java_outer_classname = "ApiProto";
option java_package = "com.google.protobuf"; option java_package = "com.google.protobuf";
option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option objc_class_prefix = "GPB"; option objc_class_prefix = "GPB";
......
...@@ -215,7 +215,7 @@ void MessageGenerator::Generate(io::Printer* printer) { ...@@ -215,7 +215,7 @@ void MessageGenerator::Generate(io::Printer* printer) {
" get { return $descriptor_accessor$; }\n" " get { return $descriptor_accessor$; }\n"
"}\n" "}\n"
"\n" "\n"
"public pbr::FieldAccessorTable Fields {\n" "pbr::FieldAccessorTable pb::IReflectedMessage.Fields {\n"
" get { return $umbrella_class_name$.internal__$identifier$__FieldAccessorTable; }\n" " get { return $umbrella_class_name$.internal__$identifier$__FieldAccessorTable; }\n"
"}\n" "}\n"
"\n" "\n"
......
...@@ -35,7 +35,7 @@ option java_generate_equals_and_hash = true; ...@@ -35,7 +35,7 @@ option java_generate_equals_and_hash = true;
option java_multiple_files = true; option java_multiple_files = true;
option java_outer_classname = "DurationProto"; option java_outer_classname = "DurationProto";
option java_package = "com.google.protobuf"; option java_package = "com.google.protobuf";
option csharp_namespace = "Google.Protobuf"; option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option objc_class_prefix = "GPB"; option objc_class_prefix = "GPB";
// A Duration represents a signed, fixed-length span of time represented // A Duration represents a signed, fixed-length span of time represented
......
...@@ -34,6 +34,7 @@ package google.protobuf; ...@@ -34,6 +34,7 @@ package google.protobuf;
option java_multiple_files = true; option java_multiple_files = true;
option java_outer_classname = "EmptyProto"; option java_outer_classname = "EmptyProto";
option java_package = "com.google.protobuf"; option java_package = "com.google.protobuf";
option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option objc_class_prefix = "GPB"; option objc_class_prefix = "GPB";
......
...@@ -34,7 +34,7 @@ package google.protobuf; ...@@ -34,7 +34,7 @@ package google.protobuf;
option java_multiple_files = true; option java_multiple_files = true;
option java_outer_classname = "FieldMaskProto"; option java_outer_classname = "FieldMaskProto";
option java_package = "com.google.protobuf"; option java_package = "com.google.protobuf";
option csharp_namespace = "Google.Protobuf"; option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option objc_class_prefix = "GPB"; option objc_class_prefix = "GPB";
......
...@@ -34,6 +34,7 @@ package google.protobuf; ...@@ -34,6 +34,7 @@ package google.protobuf;
option java_multiple_files = true; option java_multiple_files = true;
option java_outer_classname = "SourceContextProto"; option java_outer_classname = "SourceContextProto";
option java_package = "com.google.protobuf"; option java_package = "com.google.protobuf";
option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option objc_class_prefix = "GPB"; option objc_class_prefix = "GPB";
......
...@@ -35,7 +35,7 @@ option java_generate_equals_and_hash = true; ...@@ -35,7 +35,7 @@ option java_generate_equals_and_hash = true;
option java_multiple_files = true; option java_multiple_files = true;
option java_outer_classname = "StructProto"; option java_outer_classname = "StructProto";
option java_package = "com.google.protobuf"; option java_package = "com.google.protobuf";
option csharp_namespace = "Google.Protobuf"; option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option objc_class_prefix = "GPB"; option objc_class_prefix = "GPB";
......
...@@ -35,7 +35,7 @@ option java_generate_equals_and_hash = true; ...@@ -35,7 +35,7 @@ option java_generate_equals_and_hash = true;
option java_multiple_files = true; option java_multiple_files = true;
option java_outer_classname = "TimestampProto"; option java_outer_classname = "TimestampProto";
option java_package = "com.google.protobuf"; option java_package = "com.google.protobuf";
option csharp_namespace = "Google.Protobuf"; option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option objc_class_prefix = "GPB"; option objc_class_prefix = "GPB";
......
...@@ -37,6 +37,7 @@ import "google/protobuf/source_context.proto"; ...@@ -37,6 +37,7 @@ import "google/protobuf/source_context.proto";
option java_multiple_files = true; option java_multiple_files = true;
option java_outer_classname = "TypeProto"; option java_outer_classname = "TypeProto";
option java_package = "com.google.protobuf"; option java_package = "com.google.protobuf";
option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option objc_class_prefix = "GPB"; option objc_class_prefix = "GPB";
......
...@@ -40,7 +40,7 @@ package google.protobuf; ...@@ -40,7 +40,7 @@ package google.protobuf;
option java_multiple_files = true; option java_multiple_files = true;
option java_outer_classname = "WrappersProto"; option java_outer_classname = "WrappersProto";
option java_package = "com.google.protobuf"; option java_package = "com.google.protobuf";
option csharp_namespace = "Google.Protobuf"; option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option objc_class_prefix = "GPB"; option objc_class_prefix = "GPB";
......
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