Skip to content

Commit f850488

Browse files
authored
Adding string cast operators and null APIs to Variant (#38508)
* Adding string cast operators and null APIs * pr fb
1 parent cceb6ab commit f850488

26 files changed

+99
-21
lines changed

sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.net461.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public readonly partial struct Variant
1919
{
2020
private readonly object _dummy;
2121
private readonly int _dummyPrimitive;
22+
public static readonly Azure.Variant Null;
2223
public Variant(System.ArraySegment<byte> segment) { throw null; }
2324
public Variant(System.ArraySegment<char> segment) { throw null; }
2425
public Variant(bool value) { throw null; }
@@ -50,6 +51,7 @@ public readonly partial struct Variant
5051
public Variant(ushort value) { throw null; }
5152
public Variant(uint value) { throw null; }
5253
public Variant(ulong value) { throw null; }
54+
public bool IsNull { get { throw null; } }
5355
public System.Type? Type { get { throw null; } }
5456
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public T As<T>() { throw null; }
5557
public static Azure.Variant Create<T>(T value) { throw null; }
@@ -82,6 +84,7 @@ public readonly partial struct Variant
8284
public static explicit operator ulong? (in Azure.Variant value) { throw null; }
8385
public static explicit operator sbyte (in Azure.Variant value) { throw null; }
8486
public static explicit operator float (in Azure.Variant value) { throw null; }
87+
public static explicit operator string (in Azure.Variant value) { throw null; }
8588
public static explicit operator ushort (in Azure.Variant value) { throw null; }
8689
public static explicit operator uint (in Azure.Variant value) { throw null; }
8790
public static explicit operator ulong (in Azure.Variant value) { throw null; }
@@ -114,6 +117,7 @@ public readonly partial struct Variant
114117
public static implicit operator Azure.Variant (ulong? value) { throw null; }
115118
public static implicit operator Azure.Variant (sbyte value) { throw null; }
116119
public static implicit operator Azure.Variant (float value) { throw null; }
120+
public static implicit operator Azure.Variant (string value) { throw null; }
117121
public static implicit operator Azure.Variant (ushort value) { throw null; }
118122
public static implicit operator Azure.Variant (uint value) { throw null; }
119123
public static implicit operator Azure.Variant (ulong value) { throw null; }

sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.net6.0.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public readonly partial struct Variant
1919
{
2020
private readonly object _dummy;
2121
private readonly int _dummyPrimitive;
22+
public static readonly Azure.Variant Null;
2223
public Variant(System.ArraySegment<byte> segment) { throw null; }
2324
public Variant(System.ArraySegment<char> segment) { throw null; }
2425
public Variant(bool value) { throw null; }
@@ -50,6 +51,7 @@ public readonly partial struct Variant
5051
public Variant(ushort value) { throw null; }
5152
public Variant(uint value) { throw null; }
5253
public Variant(ulong value) { throw null; }
54+
public bool IsNull { get { throw null; } }
5355
public System.Type? Type { get { throw null; } }
5456
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public T As<T>() { throw null; }
5557
public static Azure.Variant Create<T>(T value) { throw null; }
@@ -82,6 +84,7 @@ public readonly partial struct Variant
8284
public static explicit operator ulong? (in Azure.Variant value) { throw null; }
8385
public static explicit operator sbyte (in Azure.Variant value) { throw null; }
8486
public static explicit operator float (in Azure.Variant value) { throw null; }
87+
public static explicit operator string (in Azure.Variant value) { throw null; }
8588
public static explicit operator ushort (in Azure.Variant value) { throw null; }
8689
public static explicit operator uint (in Azure.Variant value) { throw null; }
8790
public static explicit operator ulong (in Azure.Variant value) { throw null; }
@@ -114,6 +117,7 @@ public readonly partial struct Variant
114117
public static implicit operator Azure.Variant (ulong? value) { throw null; }
115118
public static implicit operator Azure.Variant (sbyte value) { throw null; }
116119
public static implicit operator Azure.Variant (float value) { throw null; }
120+
public static implicit operator Azure.Variant (string value) { throw null; }
117121
public static implicit operator Azure.Variant (ushort value) { throw null; }
118122
public static implicit operator Azure.Variant (uint value) { throw null; }
119123
public static implicit operator Azure.Variant (ulong value) { throw null; }

sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public readonly partial struct Variant
1919
{
2020
private readonly object _dummy;
2121
private readonly int _dummyPrimitive;
22+
public static readonly Azure.Variant Null;
2223
public Variant(System.ArraySegment<byte> segment) { throw null; }
2324
public Variant(System.ArraySegment<char> segment) { throw null; }
2425
public Variant(bool value) { throw null; }
@@ -50,6 +51,7 @@ public readonly partial struct Variant
5051
public Variant(ushort value) { throw null; }
5152
public Variant(uint value) { throw null; }
5253
public Variant(ulong value) { throw null; }
54+
public bool IsNull { get { throw null; } }
5355
public System.Type? Type { get { throw null; } }
5456
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public T As<T>() { throw null; }
5557
public static Azure.Variant Create<T>(T value) { throw null; }
@@ -82,6 +84,7 @@ public readonly partial struct Variant
8284
public static explicit operator ulong? (in Azure.Variant value) { throw null; }
8385
public static explicit operator sbyte (in Azure.Variant value) { throw null; }
8486
public static explicit operator float (in Azure.Variant value) { throw null; }
87+
public static explicit operator string (in Azure.Variant value) { throw null; }
8588
public static explicit operator ushort (in Azure.Variant value) { throw null; }
8689
public static explicit operator uint (in Azure.Variant value) { throw null; }
8790
public static explicit operator ulong (in Azure.Variant value) { throw null; }
@@ -114,6 +117,7 @@ public readonly partial struct Variant
114117
public static implicit operator Azure.Variant (ulong? value) { throw null; }
115118
public static implicit operator Azure.Variant (sbyte value) { throw null; }
116119
public static implicit operator Azure.Variant (float value) { throw null; }
120+
public static implicit operator Azure.Variant (string value) { throw null; }
117121
public static implicit operator Azure.Variant (ushort value) { throw null; }
118122
public static implicit operator Azure.Variant (uint value) { throw null; }
119123
public static implicit operator Azure.Variant (ulong value) { throw null; }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
namespace Azure
5+
{
6+
public readonly partial struct Variant
7+
{
8+
/// <summary>
9+
/// Null Variant.
10+
/// </summary>
11+
public static readonly Variant Null = new((object?)null);
12+
13+
/// <summary>
14+
/// Indicates whether the Variant is null or has a value.
15+
/// </summary>
16+
/// <returns><code>true</code> if the Variant is <code>null</code>; <code>false</code> otherwise.</returns>
17+
public bool IsNull => Type == null;
18+
}
19+
}

sdk/core/Azure.Core.Experimental/src/Variant/Variant.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,20 @@ public Variant(ArraySegment<char> segment)
951951
public static explicit operator decimal?(in Variant value) => value.As<decimal?>();
952952
#endregion
953953

954+
#region String
955+
/// <summary>
956+
/// TBD.
957+
/// </summary>
958+
/// <param name="value"></param>
959+
public static implicit operator Variant(string value) => new(value);
960+
961+
/// <summary>
962+
/// TBD.
963+
/// </summary>
964+
/// <param name="value"></param>
965+
public static explicit operator string(in Variant value) => value.As<string>();
966+
#endregion
967+
954968
#region T
955969
/// <summary>
956970
/// TBD.

sdk/core/Azure.Core.Experimental/tests/Variant/StoringArrays.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using NUnit.Framework;
66

7-
namespace Azure
7+
namespace Azure.Core.Experimental.Tests
88
{
99
public class StoringArrays
1010
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringBoolean.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33
using NUnit.Framework;
44

5-
namespace Azure
5+
namespace Azure.Core.Experimental.Tests
66
{
77
public class StoringBoolean
88
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringByte.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33
using NUnit.Framework;
44

5-
namespace Azure
5+
namespace Azure.Core.Experimental.Tests
66
{
77
public class StoringByte
88
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringChar.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3+
34
using NUnit.Framework;
45

5-
namespace Azure
6+
namespace Azure.Core.Experimental.Tests
67
{
78
public class StoringChar
89
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringDateTime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using NUnit.Framework;
66

7-
namespace Azure
7+
namespace Azure.Core.Experimental.Tests
88
{
99
public class StoringDateTime
1010
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringDateTimeOffset.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using NUnit.Framework;
66

7-
namespace Azure
7+
namespace Azure.Core.Experimental.Tests
88
{
99
public class StoringDateTimeOffset
1010
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringDecimal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using NUnit.Framework;
55

6-
namespace Azure
6+
namespace Azure.Core.Experimental.Tests
77
{
88
public class StoringDecimal
99
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringDouble.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using NUnit.Framework;
55

6-
namespace Azure
6+
namespace Azure.Core.Experimental.Tests
77
{
88
public class StoringDouble
99
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringEnum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Runtime.CompilerServices;
66
using NUnit.Framework;
77

8-
namespace Azure
8+
namespace Azure.Core.Experimental.Tests
99
{
1010
public class StoringEnum
1111
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringFloat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using NUnit.Framework;
55

6-
namespace Azure
6+
namespace Azure.Core.Experimental.Tests
77
{
88
public class StoringFloat
99
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringInt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using NUnit.Framework;
55

6-
namespace Azure
6+
namespace Azure.Core.Experimental.Tests
77
{
88
public class StoringInt
99
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringLong.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using NUnit.Framework;
55

6-
namespace Azure
6+
namespace Azure.Core.Experimental.Tests
77
{
88
public class StoringLong
99
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringNull.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using NUnit.Framework;
66

7-
namespace Azure
7+
namespace Azure.Core.Experimental.Tests
88
{
99
public class StoringNull
1010
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using NUnit.Framework;
66

7-
namespace Azure
7+
namespace Azure.Core.Experimental.Tests
88
{
99
public class StoringObject
1010
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringSByte.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using NUnit.Framework;
55

6-
namespace Azure
6+
namespace Azure.Core.Experimental.Tests
77
{
88
public class StoringSByte
99
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringShort.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using NUnit.Framework;
55

6-
namespace Azure
6+
namespace Azure.Core.Experimental.Tests
77
{
88
public class StoringShort
99
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringUInt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using NUnit.Framework;
55

6-
namespace Azure
6+
namespace Azure.Core.Experimental.Tests
77
{
88
public class StoringUInt
99
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringUShort.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using NUnit.Framework;
55

6-
namespace Azure
6+
namespace Azure.Core.Experimental.Tests
77
{
88
public class StoringUShort
99
{

sdk/core/Azure.Core.Experimental/tests/Variant/StoringUlong.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using NUnit.Framework;
55

6-
namespace Azure
6+
namespace Azure.Core.Experimental.Tests
77
{
88
public class StoringULong
99
{

sdk/core/Azure.Core.Experimental/tests/Variant/Creation.cs renamed to sdk/core/Azure.Core.Experimental/tests/Variant/VariantCreation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
using NUnit.Framework;
55

6-
namespace Azure
6+
namespace Azure.Core.Experimental.Tests
77
{
8-
public class Creation
8+
public class VariantCreation
99
{
1010
[Test]
1111
public void CreateIsAllocationFree()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using NUnit.Framework;
5+
6+
namespace Azure.Core.Experimental.Tests
7+
{
8+
public class VariantUsage
9+
{
10+
[Test]
11+
public void CanUseVariantAsString()
12+
{
13+
Variant variant = "hi";
14+
15+
Assert.AreEqual("hi", (string)variant);
16+
17+
Assert.IsTrue("hi" == (string)variant);
18+
Assert.IsTrue((string)variant == "hi");
19+
20+
Assert.AreEqual("hi", $"{(string)variant}");
21+
}
22+
23+
[Test]
24+
public void CanTestForNull()
25+
{
26+
Variant variant = Variant.Null;
27+
Assert.True(variant.IsNull);
28+
29+
Assert.True(new Variant((object)null).IsNull);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)