mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
99 lines
4.6 KiB
C#
99 lines
4.6 KiB
C#
#region ========================================================================= USING =====================================================================================
|
|
using Nemera.Contracts.DTO.Common;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
#endregion
|
|
|
|
namespace Nemera.Infrastructure.Common.Metadata;
|
|
|
|
/// <summary>
|
|
/// Provides the shared field-level merge operations used when aggregating the metadata returned by multiple metadata providers.
|
|
/// </summary>
|
|
internal static class MetadataMerge
|
|
{
|
|
/// <summary>
|
|
/// Gets the first non-empty value, otherwise the second value unchanged.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the values.</typeparam>
|
|
/// <param name="first">The value with priority.</param>
|
|
/// <param name="second">The fallback value.</param>
|
|
/// <returns>The first value when it is not empty, otherwise the second value unchanged.</returns>
|
|
public static T? First<T>(T? first, T? second)
|
|
{
|
|
return first is not null && !IsEmpty(first) ? first : second;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the union of the two collections, de-duplicated by the <paramref name="keySelector"/> key, keeping the items of the first collection on key conflicts.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the collection items.</typeparam>
|
|
/// <param name="first">The first collection, whose items have priority.</param>
|
|
/// <param name="second">The second collection.</param>
|
|
/// <param name="keySelector">The function used to extract the de-duplication key of an item.</param>
|
|
/// <returns>The union of the two collections, or <see langword="null"/> when both are <see langword="null"/>.</returns>
|
|
public static List<T>? Union<T>(List<T>? first, List<T>? second, Func<T, string?> keySelector)
|
|
where T : class
|
|
{
|
|
if (first is null && second is null)
|
|
return null;
|
|
|
|
Dictionary<string, T> seen = new(StringComparer.OrdinalIgnoreCase);
|
|
List<T> result = [];
|
|
|
|
void AddRange(List<T>? items)
|
|
{
|
|
foreach (T item in items ?? [])
|
|
if (seen.TryAdd(keySelector(item) ?? string.Empty, item))
|
|
result.Add(item);
|
|
}
|
|
|
|
AddRange(first);
|
|
AddRange(second);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Merges the release information of the two metadata, keeping the first non-null value of each field.
|
|
/// </summary>
|
|
/// <param name="first">The first release information, whose values have priority.</param>
|
|
/// <param name="second">The second release information, filling the fields that the first one lacks.</param>
|
|
/// <returns>The merged release information, or <see langword="null"/> when both are <see langword="null"/>.</returns>
|
|
public static ReleaseInfoDto? MergeReleaseInfo(ReleaseInfoDto? first, ReleaseInfoDto? second)
|
|
{
|
|
if (first is null && second is null)
|
|
return null;
|
|
|
|
DateOnly? originalReleaseDate = First(first?.OriginalReleaseDate, second?.OriginalReleaseDate);
|
|
int? originalReleaseYear = First(first?.OriginalReleaseYear, second?.OriginalReleaseYear);
|
|
DateOnly? reReleaseDate = First(first?.ReReleaseDate, second?.ReReleaseDate);
|
|
int? reReleaseYear = First(first?.ReReleaseYear, second?.ReReleaseYear);
|
|
|
|
// A release date and its year describe the same point in time, so the year is derived from the date whenever a date is known. Without this,
|
|
// a year from one provider could be combined with a date from another (for example a release year from the tags with the original release
|
|
// date from MusicBrainz), and the resulting inconsistent pair would be rejected when the metadata is applied.
|
|
if (originalReleaseDate is not null)
|
|
originalReleaseYear = originalReleaseDate.Value.Year;
|
|
if (reReleaseDate is not null)
|
|
reReleaseYear = reReleaseDate.Value.Year;
|
|
|
|
return new ReleaseInfoDto(
|
|
originalReleaseDate,
|
|
originalReleaseYear,
|
|
reReleaseDate,
|
|
reReleaseYear,
|
|
First(first?.ReleaseCountry, second?.ReleaseCountry),
|
|
First(first?.ReleaseVersion, second?.ReleaseVersion));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether the provided value is an empty string.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the value.</typeparam>
|
|
/// <param name="value">The value to check.</param>
|
|
/// <returns><see langword="true"/> when the value is an empty string, otherwise <see langword="false"/>.</returns>
|
|
private static bool IsEmpty<T>(T? value)
|
|
{
|
|
return value is string text && string.IsNullOrWhiteSpace(text);
|
|
}
|
|
}
|