Skip to content

Commit

Permalink
Исправляет некоторые предупреждения
Browse files Browse the repository at this point in the history
  • Loading branch information
inyutin-maxim committed Nov 19, 2024
1 parent 152c42c commit 99b0767
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 38 deletions.
2 changes: 1 addition & 1 deletion VkNet.Tests/Categories/Ads/AdsGetAdsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void GetAds_GenerateAdsCorrectly()
ads.Should()
.NotBeEmpty();

var ad = ads.FirstOrDefault();
var ad = ads.First();

ads.Should()
.ContainSingle();
Expand Down
10 changes: 6 additions & 4 deletions VkNet/Enums/SafetyEnums/Privacy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using VkNet.Utils;
using VkNet.Utils.JsonConverter;
Expand All @@ -9,7 +10,7 @@ namespace VkNet.Enums.SafetyEnums;
/// Уровень доступа к комментированию альбома
/// </summary>
[JsonConverter(typeof(SafetyEnumJsonConverter))]
public sealed class Privacy : SafetyEnum<Privacy>
public sealed partial class Privacy : SafetyEnum<Privacy>
{
/// <summary>
/// Доступно всем пользователям.
Expand Down Expand Up @@ -77,6 +78,8 @@ public sealed class Privacy : SafetyEnum<Privacy>
/// <returns>
/// Уровень доступа к комментированию альбома
/// </returns>
[SuppressMessage("Performance", "CA1866:Использовать перегрузку символов", Justification = "Не поддерживается в netstandard2.0")]
[SuppressMessage("Performance", "SYSLIB1045:Преобразовать в \"GeneratedRegexAttribute\".", Justification = "Не поддерживается в netstandard2.0")]
public new static Privacy FromJson(VkResponse response)
{
switch (response.ToString())
Expand Down Expand Up @@ -122,11 +125,10 @@ public sealed class Privacy : SafetyEnum<Privacy>
{
var input = response.ToString();
var idPattern = new Regex(pattern: @"([\d]+)");
long id;

long.TryParse(idPattern.Match(input: input)
.Groups[groupnum: 1]
.Value, out id);
.Value, out var id);

if (input.StartsWith(value: "list"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ private static Dictionary<string, string> ParseInputs(HtmlNode formNode)
{
var inputs = new Dictionary<string, string>();

foreach (var node in formNode.SelectNodes("//input"))
foreach (var nodeAttributes in formNode.SelectNodes("//input").Select(x => x.Attributes))
{
var nameAttribute = node.Attributes["name"];
var valueAttribute = node.Attributes["value"];
var nameAttribute = nodeAttributes["name"];
var valueAttribute = nodeAttributes["value"];

var name = nameAttribute is not null
? nameAttribute.Value
Expand Down
7 changes: 3 additions & 4 deletions VkNet/Infrastructure/JsonConfigure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ internal static JObject ToJObject(this string answer)
{
using var stringReader = new StringReader(answer);

using JsonReader jsonReader = new JsonTextReader(stringReader)
{
MaxDepth = null
};
using JsonReader jsonReader = new JsonTextReader(stringReader);

jsonReader.MaxDepth = null;

return JObject.Load(jsonReader);
}
Expand Down
8 changes: 2 additions & 6 deletions VkNet/Utils/JsonConverter/SafetyEnumJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
var methods = result.GetType()
.GetMethods(bindingAttr: BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Static|BindingFlags.FlattenHierarchy);

result = methods
.FirstOrDefault(predicate: x => x.Name == "FromJsonString")
?.Invoke(result, new object[]
{
$"{value}"
});
result = Array.Find(methods, x => x.Name == "FromJsonString")
?.Invoke(result, [$"{value}"]);

var fields = result?.GetType()
.GetFields();
Expand Down
16 changes: 6 additions & 10 deletions VkNet/Utils/JsonConverter/TolerantStringEnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ namespace VkNet.Utils.JsonConverter;
public class TolerantStringEnumConverter : StringEnumConverter
{
/// <inheritdoc />
public TolerantStringEnumConverter()
{
NamingStrategy = new SnakeCaseNamingStrategy();
}
public TolerantStringEnumConverter() => NamingStrategy = new SnakeCaseNamingStrategy();

/// <inheritdoc />
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
Expand All @@ -22,9 +19,11 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
{
return base.ReadJson(reader, objectType, existingValue, serializer);
}
catch(System.Exception e)
catch (System.Exception e)
{
var parameter = serializer.Context.TryGetTypeData(typeof(TolerantStringEnumConverter), out var s) ? (bool?)s : null;
var parameter = serializer.Context.TryGetTypeData(typeof(TolerantStringEnumConverter), out var s)
? (bool?) s
: null;

if (IsNullableType(objectType) && parameter is true)
{
Expand All @@ -36,8 +35,5 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
}
}

private static bool IsNullableType(Type t)
{
return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>);
}
private static bool IsNullableType(Type t) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>);
}
6 changes: 5 additions & 1 deletion VkNet/Utils/JsonConverter/VkDefaultJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,9 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
/// <c> true </c> if this instance can convert the specified object type;
/// otherwise, <c> false </c>.
/// </returns>
public override bool CanConvert(Type objectType) => !objectType.IsArray && objectType.IsSerializable;
public override bool CanConvert(Type objectType) => objectType is
{
IsArray: false,
IsSerializable: true
};
}
18 changes: 9 additions & 9 deletions VkNet/Utils/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,9 @@ public static string PrettyPrintJson(string json)
{
var jObject = json.ToJObject();



foreach (var key in keysToHide)
foreach (var key in keysToHide.Where(x => jObject.ContainsKey(x)))
{
if (jObject.ContainsKey(key))
{
jObject[key] = hidden;
}
jObject[key] = hidden;
}

return jObject.ToString(Formatting.Indented);
Expand Down Expand Up @@ -223,6 +218,7 @@ private static string ToCamelCase(this string str) => str.Split(new[]
{
return null;
}

return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>);
}

Expand All @@ -237,11 +233,13 @@ public static bool IsNullableStringEnum(Type t)
{
var isNull = IsNullableType(t.GenericTypeArguments.FirstOrDefault());
StringEnumAttribute myAttribute = null;

if (isNull is null)
{
isNull = true;
}
if(isNull is false)

if (isNull is false)
{
myAttribute = (StringEnumAttribute) Attribute.GetCustomAttribute(t.GenericTypeArguments.FirstOrDefault(),
typeof(StringEnumAttribute));
Expand All @@ -263,11 +261,13 @@ public static bool IsTolerantEnum(Type t)
{
var isNull = IsNullableType(t.GenericTypeArguments.FirstOrDefault());
JsonConverterAttribute myAttribute = null;

if (isNull is null)
{
isNull = true;
}
if(isNull is false)

if (isNull is false)
{
myAttribute = (JsonConverterAttribute) Attribute.GetCustomAttribute(t.GenericTypeArguments.FirstOrDefault(),
typeof(JsonConverterAttribute));
Expand Down
1 change: 1 addition & 0 deletions VkNet/VkNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
<LangVersion>latest</LangVersion>
<NoWarn>S1133;S1135;S2346</NoWarn>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit 99b0767

Please sign in to comment.