From ecd2dab0a2550c80ae054576d9c05ddb24d5e47b Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Wed, 17 Jul 2024 15:48:21 +0200 Subject: Add TrySetProviderId extension --- .../Entities/ProviderIdsExtensions.cs | 323 ++++++++++++--------- 1 file changed, 180 insertions(+), 143 deletions(-) (limited to 'MediaBrowser.Model/Entities') diff --git a/MediaBrowser.Model/Entities/ProviderIdsExtensions.cs b/MediaBrowser.Model/Entities/ProviderIdsExtensions.cs index 1c73091f0d..479ec7712d 100644 --- a/MediaBrowser.Model/Entities/ProviderIdsExtensions.cs +++ b/MediaBrowser.Model/Entities/ProviderIdsExtensions.cs @@ -3,177 +3,214 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; -namespace MediaBrowser.Model.Entities +namespace MediaBrowser.Model.Entities; + +/// +/// Class ProviderIdsExtensions. +/// +public static class ProviderIdsExtensions { /// - /// Class ProviderIdsExtensions. + /// Case insensitive dictionary of string representation. + /// + private static readonly Dictionary _metadataProviderEnumDictionary = + Enum.GetValues() + .ToDictionary( + enumValue => enumValue.ToString(), + enumValue => enumValue.ToString(), + StringComparer.OrdinalIgnoreCase); + + /// + /// Checks if this instance has an id for the given provider. + /// + /// The instance. + /// The of the provider name. + /// true if a provider id with the given name was found; otherwise false. + public static bool HasProviderId(this IHasProviderIds instance, string name) + => instance.TryGetProviderId(name, out _); + + /// + /// Checks if this instance has an id for the given provider. + /// + /// The instance. + /// The provider. + /// true if a provider id with the given name was found; otherwise false. + public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider) + => instance.HasProviderId(provider.ToString()); + + /// + /// Gets a provider id. /// - public static class ProviderIdsExtensions + /// The instance. + /// The name. + /// The provider id. + /// true if a provider id with the given name was found; otherwise false. + public static bool TryGetProviderId(this IHasProviderIds instance, string name, [NotNullWhen(true)] out string? id) { - /// - /// Case insensitive dictionary of string representation. - /// - private static readonly Dictionary _metadataProviderEnumDictionary = - Enum.GetValues() - .ToDictionary( - enumValue => enumValue.ToString(), - enumValue => enumValue.ToString(), - StringComparer.OrdinalIgnoreCase); - - /// - /// Checks if this instance has an id for the given provider. - /// - /// The instance. - /// The of the provider name. - /// true if a provider id with the given name was found; otherwise false. - public static bool HasProviderId(this IHasProviderIds instance, string name) - { - ArgumentNullException.ThrowIfNull(instance); + ArgumentNullException.ThrowIfNull(instance); - return instance.TryGetProviderId(name, out _); + if (instance.ProviderIds is null) + { + id = null; + return false; } - /// - /// Checks if this instance has an id for the given provider. - /// - /// The instance. - /// The provider. - /// true if a provider id with the given name was found; otherwise false. - public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider) + var foundProviderId = instance.ProviderIds.TryGetValue(name, out id); + // This occurs when searching with Identify (and possibly in other places) + if (string.IsNullOrEmpty(id)) { - return instance.HasProviderId(provider.ToString()); + id = null; + foundProviderId = false; } - /// - /// Gets a provider id. - /// - /// The instance. - /// The name. - /// The provider id. - /// true if a provider id with the given name was found; otherwise false. - public static bool TryGetProviderId(this IHasProviderIds instance, string name, [NotNullWhen(true)] out string? id) + return foundProviderId; + } + + /// + /// Gets a provider id. + /// + /// The instance. + /// The provider. + /// The provider id. + /// true if a provider id with the given name was found; otherwise false. + public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [NotNullWhen(true)] out string? id) + { + return instance.TryGetProviderId(provider.ToString(), out id); + } + + /// + /// Gets a provider id. + /// + /// The instance. + /// The name. + /// System.String. + public static string? GetProviderId(this IHasProviderIds instance, string name) + { + instance.TryGetProviderId(name, out string? id); + return id; + } + + /// + /// Gets a provider id. + /// + /// The instance. + /// The provider. + /// System.String. + public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider) + { + return instance.GetProviderId(provider.ToString()); + } + + /// + /// Sets a provider id. + /// + /// The instance. + /// The name, this should not contain a '=' character. + /// The value. + /// Due to how deserialization from the database works the name can not contain '='. + /// true if the provider id got set successfully; otherwise, false. + public static bool TrySetProviderId(this IHasProviderIds instance, string? name, string? value) + { + ArgumentNullException.ThrowIfNull(instance); + + // When name contains a '=' it can't be deserialized from the database + if (string.IsNullOrWhiteSpace(name) + || string.IsNullOrWhiteSpace(value) + || name.Contains('=', StringComparison.Ordinal)) { - ArgumentNullException.ThrowIfNull(instance); - - if (instance.ProviderIds is null) - { - id = null; - return false; - } - - var foundProviderId = instance.ProviderIds.TryGetValue(name, out id); - // This occurs when searching with Identify (and possibly in other places) - if (string.IsNullOrEmpty(id)) - { - id = null; - foundProviderId = false; - } - - return foundProviderId; + return false; } - /// - /// Gets a provider id. - /// - /// The instance. - /// The provider. - /// The provider id. - /// true if a provider id with the given name was found; otherwise false. - public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [NotNullWhen(true)] out string? id) + // Ensure it exists + instance.ProviderIds ??= new Dictionary(StringComparer.OrdinalIgnoreCase); + + // Match on internal MetadataProvider enum string values before adding arbitrary providers + if (_metadataProviderEnumDictionary.TryGetValue(name, out var enumValue)) { - return instance.TryGetProviderId(provider.ToString(), out id); + instance.ProviderIds[enumValue] = value; } - - /// - /// Gets a provider id. - /// - /// The instance. - /// The name. - /// System.String. - public static string? GetProviderId(this IHasProviderIds instance, string name) + else { - instance.TryGetProviderId(name, out string? id); - return id; + instance.ProviderIds[name] = value; } - /// - /// Gets a provider id. - /// - /// The instance. - /// The provider. - /// System.String. - public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider) + return true; + } + + /// + /// Sets a provider id. + /// + /// The instance. + /// The provider. + /// The value. + /// true if the provider id got set successfully; otherwise, false. + public static bool TrySetProviderId(this IHasProviderIds instance, MetadataProvider provider, string? value) + => instance.TrySetProviderId(provider.ToString(), value); + + /// + /// Sets a provider id. + /// + /// The instance. + /// The name, this should not contain a '=' character. + /// The value. + /// Due to how deserialization from the database works the name can not contain '='. + public static void SetProviderId(this IHasProviderIds instance, string name, string value) + { + ArgumentNullException.ThrowIfNull(instance); + ArgumentException.ThrowIfNullOrWhiteSpace(name); + ArgumentException.ThrowIfNullOrWhiteSpace(value); + + // When name contains a '=' it can't be deserialized from the database + if (name.Contains('=', StringComparison.Ordinal)) { - return instance.GetProviderId(provider.ToString()); + throw new ArgumentException("Provider id name cannot contain '='", nameof(name)); } - /// - /// Sets a provider id. - /// - /// The instance. - /// The name, this should not contain a '=' character. - /// The value. - /// Due to how deserialization from the database works the name can not contain '='. - public static void SetProviderId(this IHasProviderIds instance, string name, string value) + // Ensure it exists + instance.ProviderIds ??= new Dictionary(StringComparer.OrdinalIgnoreCase); + + // Match on internal MetadataProvider enum string values before adding arbitrary providers + if (_metadataProviderEnumDictionary.TryGetValue(name, out var enumValue)) { - ArgumentNullException.ThrowIfNull(instance); - ArgumentException.ThrowIfNullOrEmpty(name); - ArgumentException.ThrowIfNullOrEmpty(value); - - // When name contains a '=' it can't be deserialized from the database - if (name.Contains('=', StringComparison.Ordinal)) - { - throw new ArgumentException("Provider id name cannot contain '='", nameof(name)); - } - - // Ensure it exists - instance.ProviderIds ??= new Dictionary(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[enumValue] = value; } - - /// - /// Sets a provider id. - /// - /// The instance. - /// The provider. - /// The value. - public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value) + else { - instance.SetProviderId(provider.ToString(), value); + instance.ProviderIds[name] = value; } + } - /// - /// Removes a provider id. - /// - /// The instance. - /// The name. - public static void RemoveProviderId(this IHasProviderIds instance, string name) - { - ArgumentNullException.ThrowIfNull(instance); - ArgumentException.ThrowIfNullOrEmpty(name); + /// + /// Sets a provider id. + /// + /// The instance. + /// The provider. + /// The value. + public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value) + => instance.SetProviderId(provider.ToString(), value); - instance.ProviderIds?.Remove(name); - } + /// + /// Removes a provider id. + /// + /// The instance. + /// The name. + public static void RemoveProviderId(this IHasProviderIds instance, string name) + { + ArgumentNullException.ThrowIfNull(instance); + ArgumentException.ThrowIfNullOrEmpty(name); - /// - /// Removes a provider id. - /// - /// The instance. - /// The provider. - public static void RemoveProviderId(this IHasProviderIds instance, MetadataProvider provider) - { - ArgumentNullException.ThrowIfNull(instance); + instance.ProviderIds?.Remove(name); + } - instance.ProviderIds?.Remove(provider.ToString()); - } + /// + /// Removes a provider id. + /// + /// The instance. + /// The provider. + public static void RemoveProviderId(this IHasProviderIds instance, MetadataProvider provider) + { + ArgumentNullException.ThrowIfNull(instance); + + instance.ProviderIds?.Remove(provider.ToString()); } } -- cgit v1.2.3