Skip to content

Commit de27d26

Browse files
authored
Implement ToString for Variant (#38561)
* Implement ToString * use if/else * Export API * Update changelog for release * update changelog * bools * pr fb * nit * nit
1 parent dc01a99 commit de27d26

File tree

6 files changed

+133
-0
lines changed

6 files changed

+133
-0
lines changed

sdk/core/Azure.Core.Experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Renamed `Azure.Value` to `Azure.Variant`.
99
- Added cast operators to/from string to `Variant`.
1010
- Added `Variant.Null` and `variant.IsNull` APIs to `Variant`.
11+
- Added `ToString` implementation to `Variant`.
1112

1213
## 0.1.0-preview.29 (2023-08-07)
1314

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public readonly partial struct Variant
121121
public static implicit operator Azure.Variant (ushort value) { throw null; }
122122
public static implicit operator Azure.Variant (uint value) { throw null; }
123123
public static implicit operator Azure.Variant (ulong value) { throw null; }
124+
public override string? ToString() { throw null; }
124125
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public bool TryGetValue<T>(out T value) { throw null; }
125126
}
126127
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public readonly partial struct Variant
121121
public static implicit operator Azure.Variant (ushort value) { throw null; }
122122
public static implicit operator Azure.Variant (uint value) { throw null; }
123123
public static implicit operator Azure.Variant (ulong value) { throw null; }
124+
public override string? ToString() { throw null; }
124125
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public bool TryGetValue<T>(out T value) { throw null; }
125126
}
126127
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public readonly partial struct Variant
121121
public static implicit operator Azure.Variant (ushort value) { throw null; }
122122
public static implicit operator Azure.Variant (uint value) { throw null; }
123123
public static implicit operator Azure.Variant (ulong value) { throw null; }
124+
public override string? ToString() { throw null; }
124125
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public bool TryGetValue<T>(out T value) { throw null; }
125126
}
126127
}

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,5 +1388,94 @@ private readonly T CastTo<T>()
13881388
return value;
13891389
}
13901390
#endregion
1391+
1392+
/// <inheritdoc/>
1393+
public override string? ToString()
1394+
{
1395+
string? result;
1396+
1397+
if (_object == null)
1398+
{
1399+
result = "null";
1400+
}
1401+
else if (Type == typeof(byte))
1402+
{
1403+
result = As<byte>().ToString();
1404+
}
1405+
else if (Type == typeof(sbyte))
1406+
{
1407+
result = As<sbyte>().ToString();
1408+
}
1409+
else if (Type == typeof(bool))
1410+
{
1411+
result = ((bool)this) ? "true" : "false";
1412+
}
1413+
else if (Type == typeof(char))
1414+
{
1415+
result = As<char>().ToString();
1416+
}
1417+
else if (Type == typeof(short))
1418+
{
1419+
result = As<short>().ToString();
1420+
}
1421+
else if (Type == typeof(int))
1422+
{
1423+
result = As<int>().ToString();
1424+
}
1425+
else if (Type == typeof(long))
1426+
{
1427+
result = As<long>().ToString();
1428+
}
1429+
else if (Type == typeof(ushort))
1430+
{
1431+
result = As<ushort>().ToString();
1432+
}
1433+
else if (Type == typeof(uint))
1434+
{
1435+
result = As<uint>().ToString();
1436+
}
1437+
else if (Type == typeof(ulong))
1438+
{
1439+
result = As<ulong>().ToString();
1440+
}
1441+
else if (Type == typeof(float))
1442+
{
1443+
result = As<float>().ToString();
1444+
}
1445+
else if (Type == typeof(double))
1446+
{
1447+
result = As<double>().ToString();
1448+
}
1449+
else if (Type == typeof(decimal))
1450+
{
1451+
result = As<decimal>().ToString();
1452+
}
1453+
else if (Type == typeof(DateTime))
1454+
{
1455+
result = As<DateTime>().ToString();
1456+
}
1457+
else if (Type == typeof(DateTimeOffset))
1458+
{
1459+
result = As<DateTimeOffset>().ToString();
1460+
}
1461+
else if (Type == typeof(string))
1462+
{
1463+
result = (string)this;
1464+
}
1465+
else if (Type == typeof(ArraySegment<byte>))
1466+
{
1467+
result = ((ArraySegment<byte>)this).ToString();
1468+
}
1469+
else if (Type == typeof(ArraySegment<char>))
1470+
{
1471+
result = ((ArraySegment<char>)this).ToString();
1472+
}
1473+
else
1474+
{
1475+
result = _object.ToString();
1476+
}
1477+
1478+
return result;
1479+
}
13911480
}
13921481
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System;
5+
using System.Collections.Generic;
6+
using System.Drawing;
47
using NUnit.Framework;
58

69
namespace Azure.Core.Experimental.Tests
@@ -28,5 +31,42 @@ public void CanTestForNull()
2831

2932
Assert.True(new Variant((object)null).IsNull);
3033
}
34+
35+
[TestCaseSource(nameof(VariantValues))]
36+
public void CanGetAsString(Variant v, Variant s)
37+
{
38+
string value = (string)s;
39+
Assert.AreEqual(value, v.ToString());
40+
}
41+
42+
#region Helpers
43+
public static IEnumerable<Variant[]> VariantValues()
44+
{
45+
yield return new Variant[] { (byte)42, "42" };
46+
yield return new Variant[] { (sbyte)42, "42" };
47+
yield return new Variant[] { (short)42, "42" };
48+
yield return new Variant[] { (ushort)42, "42" };
49+
yield return new Variant[] { 42U, "42" };
50+
yield return new Variant[] { 42UL, "42" };
51+
yield return new Variant[] { 42, "42" };
52+
yield return new Variant[] { 42L, "42" };
53+
yield return new Variant[] { 1.0, "1" };
54+
yield return new Variant[] { 1.1D, "1.1" };
55+
yield return new Variant[] { 1.1F, "1.1" };
56+
yield return new Variant[] { Variant.Null, "null" };
57+
yield return new Variant[] { 'a', "a" };
58+
yield return new Variant[] { true, "true" };
59+
yield return new Variant[] { false, "false" };
60+
DateTimeOffset dateTime = new(2002, 8, 9, 10, 11, 12, TimeSpan.Zero);
61+
yield return new Variant[] { dateTime.DateTime, dateTime.DateTime.ToString() };
62+
yield return new Variant[] { dateTime, dateTime.ToString() };
63+
ArraySegment<byte> bytes = new("variant"u8.ToArray());
64+
yield return new Variant[] { bytes, bytes.ToString() };
65+
ArraySegment<char> chars = new("variant".AsSpan().ToArray());
66+
yield return new Variant[] { chars, chars.ToString() };
67+
yield return new Variant[] { new(Color.Blue), Color.Blue.ToString() };
68+
}
69+
70+
#endregion
3171
}
3272
}

0 commit comments

Comments
 (0)