Skip to content

Commit

Permalink
Adding string cast operators and null APIs to Variant (Azure#38508)
Browse files Browse the repository at this point in the history
* Adding string cast operators and null APIs

* pr fb
  • Loading branch information
annelo-msft authored and yaotongms committed Oct 12, 2023
1 parent 6f7c68c commit a94d8a8
Show file tree
Hide file tree
Showing 26 changed files with 99 additions and 21 deletions.
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.
/// </summary>
/// <param name="value"></param>
public static implicit operator Variant(string value) => new(value);

/// <summary>
/// TBD.
/// </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;
Assert.True(variant.IsNull);

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

0 comments on commit a94d8a8

Please sign in to comment.