Skip to content

Commit

Permalink
Runtime support for Protobuf Editions in C#.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 611246775
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Feb 28, 2024
1 parent 119e71c commit 450022d
Show file tree
Hide file tree
Showing 16 changed files with 809 additions and 67 deletions.
2 changes: 2 additions & 0 deletions csharp/generate_protos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ $PROTOC -Isrc -I. \
src/google/protobuf/unittest_well_known_types.proto \
src/google/protobuf/test_messages_proto3.proto \
src/google/protobuf/test_messages_proto2.proto \
src/google/protobuf/unittest_features.proto \
src/google/protobuf/unittest_legacy_features.proto \
src/google/protobuf/unittest_proto3_optional.proto \
src/google/protobuf/unittest_retention.proto

Expand Down
62 changes: 62 additions & 0 deletions csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#endregion

using Google.Protobuf.TestProtos;
using LegacyFeaturesUnittest;
using NUnit.Framework;
using ProtobufUnittest;
using System;
using System.Collections.Generic;
using System.Linq;
using UnitTest.Issues.TestProtos;
using static Google.Protobuf.Reflection.FeatureSet.Types;
using proto2 = Google.Protobuf.TestProtos.Proto2;

namespace Google.Protobuf.Reflection
{
Expand Down Expand Up @@ -454,6 +457,65 @@ public void OptionRetention()
UnittestRetentionExtensions.SourceRetentionOption));
}

[Test]
public void GetOptionsStripsFeatures()
{
var messageDescriptor = TestEditionsMessage.Descriptor;
var fieldDescriptor = messageDescriptor.FindFieldByName("required_field");
// Note: ideally we'd test GetOptions() for other descriptor types as well, but that requires
// non-fields with features applied.
Assert.Null(fieldDescriptor.GetOptions().Features);
}

[Test]
public void LegacyRequiredTransform()
{
var messageDescriptor = TestEditionsMessage.Descriptor;
var fieldDescriptor = messageDescriptor.FindFieldByName("required_field");
Assert.True(fieldDescriptor.IsRequired);
}

[Test]
public void LegacyGroupTransform()
{
var messageDescriptor = TestEditionsMessage.Descriptor;
var fieldDescriptor = messageDescriptor.FindFieldByName("delimited_field");
Assert.AreEqual(FieldType.Group, fieldDescriptor.FieldType);
}

[Test]
public void LegacyInferRequired()
{
var messageDescriptor = proto2::TestRequired.Descriptor;
var fieldDescriptor = messageDescriptor.FindFieldByName("a");
Assert.AreEqual(FieldPresence.LegacyRequired, fieldDescriptor.Features.FieldPresence);
}

[Test]
public void LegacyInferGroup()
{
var messageDescriptor = proto2::TestAllTypes.Descriptor;
var fieldDescriptor = messageDescriptor.FindFieldByName("optionalgroup");
Assert.AreEqual(MessageEncoding.Delimited, fieldDescriptor.Features.MessageEncoding);
}

[Test]
public void LegacyInferProto2Packed()
{
var messageDescriptor = proto2::TestPackedTypes.Descriptor;
var fieldDescriptor = messageDescriptor.FindFieldByName("packed_int32");
Assert.AreEqual(RepeatedFieldEncoding.Packed, fieldDescriptor.Features.RepeatedFieldEncoding);
}

[Test]
public void LegacyInferProto3Expanded()
{
var messageDescriptor = TestUnpackedTypes.Descriptor;
var fieldDescriptor = messageDescriptor.FindFieldByName("unpacked_int32");
Assert.NotNull(fieldDescriptor);
Assert.AreEqual(RepeatedFieldEncoding.Expanded, fieldDescriptor.Features.RepeatedFieldEncoding);
}

private static void TestDescriptorToProto(Func<IMessage> toProtoFunction, IMessage expectedProto)
{
var clone1 = toProtoFunction();
Expand Down
Loading

0 comments on commit 450022d

Please sign in to comment.