Prefer MetadataProvider enum as provider id key over arbitrary strings

pull/7654/head
Shadowghost 3 years ago
parent e87240b374
commit 7084541508

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace MediaBrowser.Model.Entities namespace MediaBrowser.Model.Entities
{ {
@ -9,6 +10,16 @@ namespace MediaBrowser.Model.Entities
/// </summary> /// </summary>
public static class ProviderIdsExtensions public static class ProviderIdsExtensions
{ {
/// <summary>
/// Case insensitive dictionary of <see cref="MetadataProvider"/> string representation.
/// </summary>
private static readonly Dictionary<string, string> _metadataProviderEnumDictionary =
Enum.GetValues<MetadataProvider>()
.ToDictionary(
enumValue => enumValue.ToString(),
enumValue => enumValue.ToString(),
StringComparer.OrdinalIgnoreCase);
/// <summary> /// <summary>
/// Checks if this instance has an id for the given provider. /// Checks if this instance has an id for the given provider.
/// </summary> /// </summary>
@ -108,7 +119,7 @@ namespace MediaBrowser.Model.Entities
/// <param name="instance">The instance.</param> /// <param name="instance">The instance.</param>
/// <param name="name">The name.</param> /// <param name="name">The name.</param>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
public static void SetProviderId(this IHasProviderIds instance, string name, string value) public static void SetProviderId(this IHasProviderIds instance, string name, string? value)
{ {
if (instance == null) if (instance == null)
{ {
@ -125,9 +136,17 @@ namespace MediaBrowser.Model.Entities
// Ensure it exists // Ensure it exists
instance.ProviderIds ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); instance.ProviderIds ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
// Match on internal MetadataProvider enum string values before adding arbitrary providers
if (_metadataProviderEnumDictionary.TryGetValue(name, out var enumValue))
{
instance.ProviderIds[enumValue] = value;
}
else
{
instance.ProviderIds[name] = value; instance.ProviderIds[name] = value;
} }
} }
}
/// <summary> /// <summary>
/// Sets a provider id. /// Sets a provider id.

Loading…
Cancel
Save