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
pull/594/head
Jon Skeet 10 years ago
parent 1660c5631e
commit 739d13d5d1
  1. 12
      csharp/generate_protos.sh
  2. 22
      csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs
  3. 10
      csharp/src/ProtocolBuffers/ProtocolBuffers.csproj
  4. 2
      src/google/protobuf/any.proto
  5. 1
      src/google/protobuf/api.proto
  6. 2
      src/google/protobuf/compiler/csharp/csharp_message.cc
  7. 2
      src/google/protobuf/duration.proto
  8. 1
      src/google/protobuf/empty.proto
  9. 2
      src/google/protobuf/field_mask.proto
  10. 1
      src/google/protobuf/source_context.proto
  11. 2
      src/google/protobuf/struct.proto
  12. 2
      src/google/protobuf/timestamp.proto
  13. 1
      src/google/protobuf/type.proto
  14. 2
      src/google/protobuf/wrappers.proto

@ -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 \

@ -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<InvalidCastException>(() => 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<InvalidOperationException>(() => fields[TestMap.MapStringStringFieldNumber].SetValue(message, new Dictionary<string, string>()));
}
@ -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<InvalidOperationException>(() => 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<InvalidCastException>(() => 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);

@ -96,6 +96,16 @@
<Compile Include="Reflection\ServiceDescriptor.cs" />
<Compile Include="Reflection\SingleFieldAccessor.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" />
</ItemGroup>
<ItemGroup>

@ -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";

@ -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";

@ -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"

@ -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

@ -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";

@ -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";

@ -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";

@ -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";

@ -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";

@ -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";

@ -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";

Loading…
Cancel
Save