-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add flag converters nullable serialization (#857)
- Loading branch information
1 parent
55c82fb
commit c6e3b33
Showing
6 changed files
with
205 additions
and
32 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
src/Microsoft.SqlTools.DataProtocol.Contracts/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Microsoft.SqlTools.Hosting.UnitTests")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Microsoft.SqlTools.DataProtocol.Contracts/Utilities/NullableUtils.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using System; | ||
|
||
namespace Microsoft.SqlTools.DataProtocol.Contracts.Utilities | ||
{ | ||
internal static class NullableUtils | ||
{ | ||
/// <summary> | ||
/// Determines whether the type is <see cref="Nullable{T}"/>. | ||
/// </summary> | ||
/// <param name="t">The type.</param> | ||
/// <returns> | ||
/// <c>true</c> if <paramref name="t"/> is <see cref="Nullable{T}"/>; otherwise, <c>false</c>. | ||
/// </returns> | ||
public static bool IsNullable(Type t) | ||
{ | ||
return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>); | ||
} | ||
|
||
/// <summary> | ||
/// Unwraps the <see cref="Nullable{T}"/> if necessary and returns the underlying value type. | ||
/// </summary> | ||
/// <param name="t">The type.</param> | ||
/// <returns>The underlying value type the <see cref="Nullable{T}"/> type was produced from, | ||
/// or the <paramref name="t"/> type if the type is not <see cref="Nullable{T}"/>. | ||
/// </returns> | ||
public static Type GetUnderlyingTypeIfNullable(Type t) | ||
{ | ||
return IsNullable(t) ? Nullable.GetUnderlyingType(t) : t; | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
test/Microsoft.SqlTools.Hosting.UnitTests/Contracts/Utilities/FlagsIntConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using Microsoft.SqlTools.DataProtocol.Contracts.Utilities; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using Xunit; | ||
|
||
namespace Microsoft.SqlTools.Hosting.UnitTests.Contracts.Utilities | ||
{ | ||
public class FlagsIntConverterTests | ||
{ | ||
[Fact] | ||
public void NullableValueCanBeDeserialized() | ||
{ | ||
var jsonObject = JObject.Parse("{\"optionalValue\": [1, 2]}"); | ||
var contract = jsonObject.ToObject<DataContract>(); | ||
Assert.NotNull(contract); | ||
Assert.NotNull(contract.OptionalValue); | ||
Assert.Equal(TestFlags.FirstItem | TestFlags.SecondItem, contract.OptionalValue); | ||
} | ||
|
||
[Fact] | ||
public void RegularValueCanBeDeserialized() | ||
{ | ||
var jsonObject = JObject.Parse("{\"Value\": [1, 3]}"); | ||
var contract = jsonObject.ToObject<DataContract>(); | ||
Assert.NotNull(contract); | ||
Assert.Equal(TestFlags.FirstItem | TestFlags.ThirdItem, contract.Value); | ||
} | ||
|
||
[Fact] | ||
public void ExplicitNullCanBeDeserialized() | ||
{ | ||
var jsonObject = JObject.Parse("{\"optionalValue\": null}"); | ||
var contract = jsonObject.ToObject<DataContract>(); | ||
Assert.NotNull(contract); | ||
Assert.Null(contract.OptionalValue); | ||
} | ||
|
||
[Flags] | ||
[JsonConverter(typeof(FlagsIntConverter))] | ||
private enum TestFlags | ||
{ | ||
[FlagsIntConverter.SerializeValue(1)] | ||
FirstItem = 1 << 0, | ||
|
||
[FlagsIntConverter.SerializeValue(2)] | ||
SecondItem = 1 << 1, | ||
|
||
[FlagsIntConverter.SerializeValue(3)] | ||
ThirdItem = 1 << 2, | ||
} | ||
|
||
private class DataContract | ||
{ | ||
public TestFlags? OptionalValue { get; set; } | ||
|
||
public TestFlags Value { get; set; } | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
test/Microsoft.SqlTools.Hosting.UnitTests/Contracts/Utilities/FlagsStringConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using System; | ||
using Microsoft.SqlTools.DataProtocol.Contracts.Utilities; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
|
||
namespace Microsoft.SqlTools.Hosting.UnitTests.Contracts.Utilities | ||
{ | ||
public class FlagsStringConverterTests | ||
{ | ||
[Fact] | ||
public void NullableValueCanBeDeserialized() | ||
{ | ||
var jsonObject = JObject.Parse("{\"optionalValue\": [\"First\", \"Second\"]}"); | ||
var contract = jsonObject.ToObject<DataContract>(); | ||
Assert.NotNull(contract); | ||
Assert.NotNull(contract.OptionalValue); | ||
Assert.Equal(TestFlags.FirstItem | TestFlags.SecondItem, contract.OptionalValue); | ||
} | ||
|
||
[Fact] | ||
public void RegularValueCanBeDeserialized() | ||
{ | ||
var jsonObject = JObject.Parse("{\"Value\": [\"First\", \"Third\"]}"); | ||
var contract = jsonObject.ToObject<DataContract>(); | ||
Assert.NotNull(contract); | ||
Assert.Equal(TestFlags.FirstItem | TestFlags.ThirdItem, contract.Value); | ||
} | ||
|
||
[Fact] | ||
public void ExplicitNullCanBeDeserialized() | ||
{ | ||
var jsonObject = JObject.Parse("{\"optionalValue\": null}"); | ||
var contract = jsonObject.ToObject<DataContract>(); | ||
Assert.NotNull(contract); | ||
Assert.Null(contract.OptionalValue); | ||
} | ||
|
||
[Flags] | ||
[JsonConverter(typeof(FlagsStringConverter))] | ||
private enum TestFlags | ||
{ | ||
[FlagsStringConverter.SerializeValue("First")] | ||
FirstItem = 1 << 0, | ||
|
||
[FlagsStringConverter.SerializeValue("Second")] | ||
SecondItem = 1 << 1, | ||
|
||
[FlagsStringConverter.SerializeValue("Third")] | ||
ThirdItem = 1 << 2, | ||
} | ||
|
||
private class DataContract | ||
{ | ||
public TestFlags? OptionalValue { get; set; } | ||
|
||
public TestFlags Value { get; set; } | ||
} | ||
} | ||
} |