Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper method on Any to allow an any to be unpacked more easily #9695

Merged
merged 1 commit into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion

using Google.Protobuf.Reflection;
using Google.Protobuf.TestProtos;
using NUnit.Framework;
using System.Linq;
using UnitTest.Issues.TestProtos;

namespace Google.Protobuf.WellKnownTypes
{
Expand Down Expand Up @@ -148,10 +151,34 @@ public void IsWrongType()
Assert.False(any.Is(TestOneof.Descriptor));
}

[Test]
public void IsRightType()
{
var any = Any.Pack(SampleMessages.CreateFullTestAllTypes());
Assert.True(any.Is(TestAllTypes.Descriptor));
}

[Test]
public void Unpack_TypeRegistry()
{
var messages = new IMessage[]
{
SampleMessages.CreateFullTestAllTypes(),
new TestWellKnownTypes { BoolField = true },
new MoreString { Data = { "x" } },
new MoreBytes { Data = ByteString.CopyFromUtf8("xyz") },
new ReservedNames { Descriptor_ = 10 }
};
var anyMessages = messages.Select(Any.Pack);

// The type registry handles the first four of the packed messages, but not the final one.
var registry = TypeRegistry.FromFiles(
UnittestWellKnownTypesReflection.Descriptor,
UnittestProto3Reflection.Descriptor);
var unpacked = anyMessages.Select(any => any.Unpack(registry)).ToList();
var expected = (IMessage[]) messages.Clone();
expected[4] = null;
Assert.AreEqual(expected, unpacked);
}
}
}
20 changes: 20 additions & 0 deletions csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ public bool Is(MessageDescriptor descriptor)
return true;
}

/// <summary>
/// Attempts to unpack the content of this Any message into one of the message types
/// in the given type registry, based on the type URL.
/// </summary>
/// <param name="registry">The type registry to consult for messages.</param>
/// <returns>The unpacked message, or <c>null</c> if no matching message was found.</returns>
public IMessage Unpack(TypeRegistry registry)
{
string typeName = GetTypeName(TypeUrl);
MessageDescriptor descriptor = registry.Find(typeName);
if (descriptor == null)
{
return null;
}

var message = descriptor.Parser.CreateTemplate();
message.MergeFrom(Value);
return message;
}

/// <summary>
/// Packs the specified message into an Any message using a type URL prefix of "type.googleapis.com".
/// </summary>
Expand Down