mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-23 17:24:12 -05:00
Move unused forked code to external directory (#1192)
* Move unused forked code to external directory * Fix SLN build errors * Add back resource provider core since it's referenced by main resource provider project * Update PackageProjects step of pipeline
This commit is contained in:
92
external/Microsoft.SqlTools.DataProtocol.Contracts/Utilities/FlagsIntConverter.cs
vendored
Normal file
92
external/Microsoft.SqlTools.DataProtocol.Contracts/Utilities/FlagsIntConverter.cs
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// 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 System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Microsoft.SqlTools.DataProtocol.Contracts.Utilities
|
||||
{
|
||||
internal class FlagsIntConverter : JsonConverter
|
||||
{
|
||||
public override bool CanWrite => true;
|
||||
public override bool CanRead => true;
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType.IsEnum && objectType.GetCustomAttribute(typeof(FlagsAttribute)) != null;
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
var jToken = JToken.Load(reader);
|
||||
if (jToken.Type == JTokenType.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int[] values = ((JArray)jToken).Values<int>().ToArray();
|
||||
var pureType = NullableUtils.GetUnderlyingTypeIfNullable(objectType);
|
||||
|
||||
FieldInfo[] enumFields = pureType.GetFields(BindingFlags.Public | BindingFlags.Static);
|
||||
int setFlags = 0;
|
||||
foreach (FieldInfo enumField in enumFields)
|
||||
{
|
||||
int enumValue = (int)enumField.GetValue(null);
|
||||
|
||||
// If there is a serialize value set for the enum value, look for that instead of the int value
|
||||
SerializeValueAttribute serializeValue = enumField.GetCustomAttribute<SerializeValueAttribute>();
|
||||
int searchValue = serializeValue?.Value ?? enumValue;
|
||||
if (Array.IndexOf(values, searchValue) >= 0)
|
||||
{
|
||||
setFlags |= enumValue;
|
||||
}
|
||||
}
|
||||
|
||||
return Enum.ToObject(pureType, setFlags);
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
FieldInfo[] enumFields = value.GetType().GetFields(BindingFlags.Public | BindingFlags.Static);
|
||||
List<int> setFlags = new List<int>();
|
||||
foreach (FieldInfo enumField in enumFields)
|
||||
{
|
||||
// Make sure the flag is set before doing expensive reflection
|
||||
int enumValue = (int)enumField.GetValue(null);
|
||||
if (((int)value & enumValue) == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// If there is a serialize value set for the member, use that instead of the int value
|
||||
SerializeValueAttribute serializeValue = enumField.GetCustomAttribute<SerializeValueAttribute>();
|
||||
int flagValue = serializeValue?.Value ?? enumValue;
|
||||
setFlags.Add(flagValue);
|
||||
}
|
||||
|
||||
string joinedFlags = string.Join(", ", setFlags);
|
||||
writer.WriteRawValue($"[{joinedFlags}]");
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
internal class SerializeValueAttribute : Attribute
|
||||
{
|
||||
public int Value { get; }
|
||||
|
||||
public SerializeValueAttribute(int value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
101
external/Microsoft.SqlTools.DataProtocol.Contracts/Utilities/FlagsStringConverter.cs
vendored
Normal file
101
external/Microsoft.SqlTools.DataProtocol.Contracts/Utilities/FlagsStringConverter.cs
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
//
|
||||
// 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 System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Microsoft.SqlTools.DataProtocol.Contracts.Utilities
|
||||
{
|
||||
internal class FlagsStringConverter : JsonConverter
|
||||
{
|
||||
public override bool CanWrite => true;
|
||||
public override bool CanRead => true;
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType.IsEnum && objectType.GetCustomAttribute(typeof(FlagsAttribute)) != null;
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
JToken jToken = JToken.Load(reader);
|
||||
if (jToken.Type == JTokenType.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string[] values = ((JArray)jToken).Values<string>().ToArray();
|
||||
var pureType = NullableUtils.GetUnderlyingTypeIfNullable(objectType);
|
||||
|
||||
FieldInfo[] enumFields = pureType.GetFields(BindingFlags.Public | BindingFlags.Static);
|
||||
int setFlags = 0;
|
||||
foreach (FieldInfo enumField in enumFields)
|
||||
{
|
||||
// If there is a serialize value set for the enum value, look for the instead of the int value
|
||||
SerializeValueAttribute serializeValue = enumField.GetCustomAttribute<SerializeValueAttribute>();
|
||||
string searchValue = serializeValue?.Value ?? enumField.Name;
|
||||
if (serializer.ContractResolver is CamelCasePropertyNamesContractResolver)
|
||||
{
|
||||
searchValue = char.ToLowerInvariant(searchValue[0]) + searchValue.Substring(1);
|
||||
}
|
||||
|
||||
// If the value is in the json array, or the int value into the flags
|
||||
if (Array.IndexOf(values, searchValue) >= 0)
|
||||
{
|
||||
setFlags |= (int)enumField.GetValue(null);
|
||||
}
|
||||
}
|
||||
|
||||
return Enum.ToObject(pureType, setFlags);
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
FieldInfo[] enumFields = value.GetType().GetFields(BindingFlags.Public | BindingFlags.Static);
|
||||
List<string> setFlags = new List<string>();
|
||||
foreach (FieldInfo enumField in enumFields)
|
||||
{
|
||||
// Make sure the flag is set before doing any other work
|
||||
int enumValue = (int)enumField.GetValue(null);
|
||||
if (((int)value & enumValue) == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// If there is a serialize value set for the member, use that instead of the int value
|
||||
SerializeValueAttribute serializeValue = enumField.GetCustomAttribute<SerializeValueAttribute>();
|
||||
string flagValue = serializeValue?.Value ?? enumField.Name;
|
||||
if (serializer.ContractResolver is CamelCasePropertyNamesContractResolver)
|
||||
{
|
||||
flagValue = char.ToLowerInvariant(flagValue[0]) + flagValue.Substring(1);
|
||||
}
|
||||
setFlags.Add($"\"{flagValue}\"");
|
||||
}
|
||||
|
||||
string joinedFlags = string.Join(", ", setFlags);
|
||||
writer.WriteRawValue($"[{joinedFlags}]");
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
internal class SerializeValueAttribute : Attribute
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
public SerializeValueAttribute(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
36
external/Microsoft.SqlTools.DataProtocol.Contracts/Utilities/NullableUtils.cs
vendored
Normal file
36
external/Microsoft.SqlTools.DataProtocol.Contracts/Utilities/NullableUtils.cs
vendored
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
103
external/Microsoft.SqlTools.DataProtocol.Contracts/Utilities/Union.cs
vendored
Normal file
103
external/Microsoft.SqlTools.DataProtocol.Contracts/Utilities/Union.cs
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
//
|
||||
// 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 Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Microsoft.SqlTools.DataProtocol.Contracts.Utilities
|
||||
{
|
||||
internal interface IUnion
|
||||
{
|
||||
object Value { get; set; }
|
||||
}
|
||||
|
||||
[JsonConverter(typeof(UnionJsonSerializer))]
|
||||
public class Union<T1, T2> : IUnion
|
||||
{
|
||||
protected T1 value1;
|
||||
protected T2 value2;
|
||||
|
||||
internal Union() {}
|
||||
|
||||
public object Value
|
||||
{
|
||||
get => (object) value1 ?? value2;
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentException();
|
||||
}
|
||||
|
||||
if (value is T1)
|
||||
{
|
||||
value1 = (T1) value;
|
||||
}
|
||||
else if (value is T2)
|
||||
{
|
||||
value2 = (T2) value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class UnionJsonSerializer : JsonConverter
|
||||
{
|
||||
public override bool CanRead => true;
|
||||
public override bool CanWrite => true;
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
writer.WriteValue(((IUnion)value).Value);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
// Check for null first
|
||||
JToken jToken = JToken.Load(reader);
|
||||
if (jToken.Type == JTokenType.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Cast to an object
|
||||
JObject jObject = (JObject)jToken;
|
||||
|
||||
// Try to convert to
|
||||
object value = null;
|
||||
foreach (Type t in objectType.GenericTypeArguments)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = jObject.ToObject(t);
|
||||
break;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Ignore any failure to cast, we'll throw if we exhaust all the conversion options
|
||||
}
|
||||
}
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
throw new InvalidCastException();
|
||||
}
|
||||
|
||||
IUnion result = (IUnion)Activator.CreateInstance(objectType);
|
||||
result.Value = value;
|
||||
return result;
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType.IsAssignableFrom(typeof(IUnion));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user