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

Adding string cast operators and null APIs to Variant #38508

Merged
merged 2 commits into from
Sep 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public readonly partial struct Variant
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public static readonly Azure.Variant Null;
public Variant(System.ArraySegment<byte> segment) { throw null; }
public Variant(System.ArraySegment<char> segment) { throw null; }
public Variant(bool value) { throw null; }
Expand Down Expand Up @@ -50,6 +51,7 @@ public readonly partial struct Variant
public Variant(ushort value) { throw null; }
public Variant(uint value) { throw null; }
public Variant(ulong value) { throw null; }
public bool IsNull { get { throw null; } }
public System.Type? Type { get { throw null; } }
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public T As<T>() { throw null; }
public static Azure.Variant Create<T>(T value) { throw null; }
Expand Down Expand Up @@ -82,6 +84,7 @@ public readonly partial struct Variant
public static explicit operator ulong? (in Azure.Variant value) { throw null; }
public static explicit operator sbyte (in Azure.Variant value) { throw null; }
public static explicit operator float (in Azure.Variant value) { throw null; }
public static explicit operator string (in Azure.Variant value) { throw null; }
public static explicit operator ushort (in Azure.Variant value) { throw null; }
public static explicit operator uint (in Azure.Variant value) { throw null; }
public static explicit operator ulong (in Azure.Variant value) { throw null; }
Expand Down Expand Up @@ -114,6 +117,7 @@ public readonly partial struct Variant
public static implicit operator Azure.Variant (ulong? value) { throw null; }
public static implicit operator Azure.Variant (sbyte value) { throw null; }
public static implicit operator Azure.Variant (float value) { throw null; }
public static implicit operator Azure.Variant (string value) { throw null; }
public static implicit operator Azure.Variant (ushort value) { throw null; }
public static implicit operator Azure.Variant (uint value) { throw null; }
public static implicit operator Azure.Variant (ulong value) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public readonly partial struct Variant
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public static readonly Azure.Variant Null;
public Variant(System.ArraySegment<byte> segment) { throw null; }
public Variant(System.ArraySegment<char> segment) { throw null; }
public Variant(bool value) { throw null; }
Expand Down Expand Up @@ -50,6 +51,7 @@ public readonly partial struct Variant
public Variant(ushort value) { throw null; }
public Variant(uint value) { throw null; }
public Variant(ulong value) { throw null; }
public bool IsNull { get { throw null; } }
public System.Type? Type { get { throw null; } }
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public T As<T>() { throw null; }
public static Azure.Variant Create<T>(T value) { throw null; }
Expand Down Expand Up @@ -82,6 +84,7 @@ public readonly partial struct Variant
public static explicit operator ulong? (in Azure.Variant value) { throw null; }
public static explicit operator sbyte (in Azure.Variant value) { throw null; }
public static explicit operator float (in Azure.Variant value) { throw null; }
public static explicit operator string (in Azure.Variant value) { throw null; }
public static explicit operator ushort (in Azure.Variant value) { throw null; }
public static explicit operator uint (in Azure.Variant value) { throw null; }
public static explicit operator ulong (in Azure.Variant value) { throw null; }
Expand Down Expand Up @@ -114,6 +117,7 @@ public readonly partial struct Variant
public static implicit operator Azure.Variant (ulong? value) { throw null; }
public static implicit operator Azure.Variant (sbyte value) { throw null; }
public static implicit operator Azure.Variant (float value) { throw null; }
public static implicit operator Azure.Variant (string value) { throw null; }
public static implicit operator Azure.Variant (ushort value) { throw null; }
public static implicit operator Azure.Variant (uint value) { throw null; }
public static implicit operator Azure.Variant (ulong value) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public readonly partial struct Variant
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public static readonly Azure.Variant Null;
public Variant(System.ArraySegment<byte> segment) { throw null; }
public Variant(System.ArraySegment<char> segment) { throw null; }
public Variant(bool value) { throw null; }
Expand Down Expand Up @@ -50,6 +51,7 @@ public readonly partial struct Variant
public Variant(ushort value) { throw null; }
public Variant(uint value) { throw null; }
public Variant(ulong value) { throw null; }
public bool IsNull { get { throw null; } }
public System.Type? Type { get { throw null; } }
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public T As<T>() { throw null; }
public static Azure.Variant Create<T>(T value) { throw null; }
Expand Down Expand Up @@ -82,6 +84,7 @@ public readonly partial struct Variant
public static explicit operator ulong? (in Azure.Variant value) { throw null; }
public static explicit operator sbyte (in Azure.Variant value) { throw null; }
public static explicit operator float (in Azure.Variant value) { throw null; }
public static explicit operator string (in Azure.Variant value) { throw null; }
public static explicit operator ushort (in Azure.Variant value) { throw null; }
public static explicit operator uint (in Azure.Variant value) { throw null; }
public static explicit operator ulong (in Azure.Variant value) { throw null; }
Expand Down Expand Up @@ -114,6 +117,7 @@ public readonly partial struct Variant
public static implicit operator Azure.Variant (ulong? value) { throw null; }
public static implicit operator Azure.Variant (sbyte value) { throw null; }
public static implicit operator Azure.Variant (float value) { throw null; }
public static implicit operator Azure.Variant (string value) { throw null; }
public static implicit operator Azure.Variant (ushort value) { throw null; }
public static implicit operator Azure.Variant (uint value) { throw null; }
public static implicit operator Azure.Variant (ulong value) { throw null; }
Expand Down
19 changes: 19 additions & 0 deletions sdk/core/Azure.Core.Experimental/src/Variant/Variant.Null.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Azure
{
public readonly partial struct Variant
{
/// <summary>
/// Null Variant.
/// </summary>
public static readonly Variant Null = new((object?)null);

/// <summary>
/// Indicates whether the Variant is null or has a value.
/// </summary>
/// <returns><code>true</code> if the Variant is <code>null</code>; <code>false</code> otherwise.</returns>
public bool IsNull => Type == null;
}
}
14 changes: 14 additions & 0 deletions sdk/core/Azure.Core.Experimental/src/Variant/Variant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,20 @@ public Variant(ArraySegment<char> segment)
public static explicit operator decimal?(in Variant value) => value.As<decimal?>();
#endregion

#region String
/// <summary>
/// TBD.
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="value"></param>
public static implicit operator Variant(string value) => new(value);

/// <summary>
/// TBD.
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="value"></param>
public static explicit operator string(in Variant value) => value.As<string>();
#endregion

#region T
/// <summary>
/// TBD.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringArrays
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.
using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringBoolean
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.
using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringByte
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringChar
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringDateTime
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringDateTimeOffset
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringDecimal
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringDouble
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Runtime.CompilerServices;
using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringEnum
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringFloat
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringInt
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringLong
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringNull
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringSByte
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringShort
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringUInt
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringUShort
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class StoringULong
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

using NUnit.Framework;

namespace Azure
namespace Azure.Core.Experimental.Tests
{
public class Creation
public class VariantCreation
{
[Test]
public void CreateIsAllocationFree()
Expand Down
32 changes: 32 additions & 0 deletions sdk/core/Azure.Core.Experimental/tests/Variant/VariantUsage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using NUnit.Framework;

namespace Azure.Core.Experimental.Tests
{
public class VariantUsage
{
[Test]
public void CanUseVariantAsString()
{
Variant variant = "hi";

Assert.AreEqual("hi", (string)variant);

Assert.IsTrue("hi" == (string)variant);
Assert.IsTrue((string)variant == "hi");

Assert.AreEqual("hi", $"{(string)variant}");
}

[Test]
public void CanTestForNull()
{
Variant variant = Variant.Null;
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
Assert.True(variant.IsNull);

Assert.True(new Variant((object)null).IsNull);
}
}
}