Skip to content

Commit

Permalink
Added some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Markeli committed Oct 29, 2024
1 parent 267e9e7 commit 7757bec
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 42 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
<PropertyGroup>
<VersionMajor>1</VersionMajor>
<VersionMinor>1</VersionMinor>
<VersionMajor>2</VersionMajor>
<VersionMinor>0</VersionMinor>
<BuildNumber>$(BuildNumber)</BuildNumber>
<BuildNumber Condition="'$(BuildNumber)' == ''">0</BuildNumber>
<PackageVersion>$(VersionMajor).$(VersionMinor).$(BuildNumber)$(VersionTag)</PackageVersion>
Expand Down
6 changes: 3 additions & 3 deletions Mindbox.YandexTracker.Abstractions/Entities/Issue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ public sealed record Issue
/// Кастомные поля задачи
/// </summary>
/// <remarks>
/// Необходимость JsonNode заключается в том, что мы не знаем какое объект придет к нам с сервера под заданным ключем.
/// Чтобы не поломать работу было принято использовать JToken и ограничить доступ к словарю через GetCustomField и
/// SetCustomField
/// Используем JsonElement, потому что на этапе компиляции мы не знаем, какой объект придет к нам с сервера под
/// заданным ключиком. Чтобы не поломать работу было принято использовать JsonElement и ограничить доступ к словарю через
/// GetCustomField и SetCustomField.
/// </remarks>
internal Dictionary<string, JsonElement> CustomFields { get; init; } = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Mindbox.YandexTracker.JsonConverters;

/// <summary>
/// Кастомный конвертер для сериализации/десериализации enum'ов с атрибутом EnumMemberAttribute.
/// </summary>
/// <remarks>
/// При конвертации в json значение берется из атрибута.
/// </remarks>
internal class EnumWithEnumMemberAttributeJsonConverter<T> : JsonConverter<T> where T: Enum
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string? enumString = reader.GetString();
for (var i = 0; i < typeToConvert.GetFields().Length; i++)
{
var field = typeToConvert.GetFields()[i];

if (field.GetCustomAttribute<EnumMemberAttribute>()?.Value == enumString)
{
return (T)field.GetValue(null)!;
}
}

if (enumString == null) throw new JsonException($"Unknown value for {nameof(QueueLocalFieldType)} ({enumString})");

return (T)Enum.Parse(typeToConvert, enumString, true);
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
var enumMemberAttribute = value.GetType().GetField(value.ToString())?.GetCustomAttribute<EnumMemberAttribute>();
writer.WriteStringValue(enumMemberAttribute != null ? enumMemberAttribute.Value : value.ToString());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

namespace Mindbox.YandexTracker.JsonConverters;

internal class YandexDateTimeConverter : JsonConverter<DateTime>
/// <summary>
/// Кастомный конвертер для сериализации/десериализации даты и времени в формате Yandex'а.
/// </summary>
internal class YandexDateTimeJsonConverter : JsonConverter<DateTime>
{
private const string DateFormat = "yyyy-MM-ddTHH:mm:ss.fffzzz"; // Adjust this to match your JSON date format

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

namespace Mindbox.YandexTracker.JsonConverters;

internal class YandexNullableDateTimeConverter : JsonConverter<DateTime?>
/// <summary>
/// Кастомный конвертер для сериализации/десериализации nullable даты и времени в формате Yandex'а.
/// </summary>
internal class YandexNullableDateTimeJsonConverter : JsonConverter<DateTime?>
{
private const string DateFormat = "yyyy-MM-ddTHH:mm:ss.fffzzz"; // Adjust this to match your JSON date format

Expand Down
6 changes: 3 additions & 3 deletions Mindbox.YandexTracker/YandexTrackerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public YandexTrackerClient(
{
Converters =
{
new QueueLocalFieldTypeConverter(),
new EnumWithEnumMemberAttributeJsonConverter<QueueLocalFieldType>(),
new JsonStringEnumConverter(namingPolicy: JsonNamingPolicy.CamelCase),
new YandexDateTimeConverter(),
new YandexNullableDateTimeConverter()
new YandexDateTimeJsonConverter(),
new YandexNullableDateTimeJsonConverter()
},
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Expand Down

0 comments on commit 7757bec

Please sign in to comment.