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

Allow As<T> to return null for reference types #38611

Merged
merged 2 commits into from
Sep 11, 2023
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
19 changes: 15 additions & 4 deletions sdk/core/Azure.Core.Experimental/src/Variant/Variant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@ public readonly Type? Type
}

[DoesNotReturn]
private static void ThrowInvalidCast() => throw new InvalidCastException();
private static void ThrowInvalidCast(Type? source, Type target)
{
if (source is null)
{
throw new InvalidCastException($"Unable to cast null Variant to type '{target}'.");
}
else
{
throw new InvalidCastException($"Unable to cast Variant of type '{source}' to type '{target}'.");
}
}

[DoesNotReturn]
private static void ThrowArgumentNull(string paramName) => throw new ArgumentNullException(paramName);
Expand Down Expand Up @@ -1296,11 +1306,12 @@ private readonly bool TryGetObjectSlow<T>(out T value)
{
// Single return has a significant performance benefit.

bool result = false;
bool result;

if (_object is null)
{
value = default!;
result = true;
}
else if (typeof(T) == typeof(char[]))
{
Expand Down Expand Up @@ -1380,9 +1391,9 @@ private readonly bool TryGetObjectSlow<T>(out T value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly T As<T>()
{
if (!TryGetValue<T>(out T value))
if (!TryGetValue(out T value))
{
ThrowInvalidCast();
ThrowInvalidCast(Type, typeof(T));
}

return value;
Expand Down
36 changes: 36 additions & 0 deletions sdk/core/Azure.Core.Experimental/tests/Variant/VariantUsage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,42 @@ public void VariantAssignmentHasReferenceSemantics()
Assert.AreEqual("3", b.As<List<string>>()[0]);
}

[Test]
public void ReferenceTypesCanBeNull()
{
string s = null;
Variant stringVariant = new(s);

Assert.AreEqual(Variant.Null, stringVariant);
Assert.IsNull(stringVariant.As<string>());

List<int> list = null;
Variant listVariant = new(list);

Assert.AreEqual(Variant.Null, listVariant);
Assert.IsNull(listVariant.As<string>());
}

[Test]
public void NonNullableValueTypesCannotBeNull()
{
int? i = null;
Variant intVariant = new(i);

Assert.AreEqual(Variant.Null, intVariant);
Assert.Throws<InvalidCastException>(() => intVariant.As<int>());
}

[Test]
public void NullableValueTypesCanBeNull()
{
int? i = null;
Variant intVariant = new(i);

Assert.AreEqual(Variant.Null, intVariant);
Assert.IsNull(intVariant.As<int?>());
}

#region Helpers
public static IEnumerable<Variant[]> VariantValues()
{
Expand Down