From 8ac68e894613b909ab04db12a4c5eb81131e73da Mon Sep 17 00:00:00 2001 From: Bogdan Date: Thu, 23 Nov 2023 04:08:59 +0200 Subject: [PATCH] Fixed: (Cardigann) Use of the default value for new select settings Fixes #1929 --- .../Definitions/Cardigann/CardigannBase.cs | 80 ++++++++++--------- .../Indexers/IndexerResource.cs | 32 ++------ 2 files changed, 48 insertions(+), 64 deletions(-) diff --git a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs index 70cec36f6..d61f413d7 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs @@ -292,56 +292,60 @@ namespace NzbDrone.Core.Indexers.Definitions.Cardigann foreach (var setting in _definition.Settings) { var name = ".Config." + setting.Name; - var value = Settings.ExtraFieldData.GetValueOrDefault(setting.Name, setting.Default); - if ((setting.Type != "password" && setting.Name != "apikey" && setting.Name != "rsskey") && indexerLogging) + object defaultValue = setting.Type switch { - _logger.Trace($"{name} got value {value.ToJson()}"); - } + "select" => setting.Options.OrderBy(x => x.Key).Select(x => x.Key).ToList().IndexOf(setting.Default).ToString().ParseInt64() ?? 0, + _ => setting.Default + }; - if (setting.Type == "text" || setting.Type == "password") + var value = Settings.ExtraFieldData.GetValueOrDefault(setting.Name, defaultValue); + + if (indexerLogging && setting.Type != "password" && setting.Name != "apikey" && setting.Name != "rsskey") { - variables[name] = value; + _logger.Trace($"{name} got value {value.ToJson()}"); } - else if (setting.Type == "checkbox") - { - if (value is string stringValue && bool.TryParse(stringValue, out var result)) - { - value = result; - } - variables[name] = (bool)value ? ".True" : null; - } - else if (setting.Type == "select") + switch (setting.Type) { - if (indexerLogging) - { - _logger.Trace($"Setting options: {setting.Options.ToJson()}"); - } + case "text": + case "password": + variables[name] = value; + break; + case "checkbox": + if (value is string stringValue && bool.TryParse(stringValue, out var result)) + { + value = result; + } + + variables[name] = (bool)value ? ".True" : null; + break; + case "select": + if (indexerLogging) + { + _logger.Trace($"Setting options: {setting.Options.ToJson()}"); + } - var sorted = setting.Options.OrderBy(x => x.Key).ToList(); - var selected = sorted[(int)(long)value]; + var sorted = setting.Options.OrderBy(x => x.Key).ToList(); + var selected = sorted[(int)(long)value]; - if (indexerLogging) - { - _logger.Debug($"Selected option: {selected.ToJson()}"); - } + if (indexerLogging) + { + _logger.Debug($"Selected option: {selected.ToJson()}"); + } - variables[name] = selected.Key; - } - else if (setting.Type == "info") - { - variables[name] = value; - } - else if (setting.Type == "cardigannCaptcha") - { - } - else - { - throw new NotSupportedException($"Type {setting.Type} is not supported."); + variables[name] = selected.Key; + break; + case "info": + variables[name] = value; + break; + case "cardigannCaptcha": + break; + default: + throw new NotSupportedException($"Type {setting.Type} is not supported."); } - if (setting.Type != "password" && setting.Name != "apikey" && setting.Name != "rsskey" && indexerLogging && variables.ContainsKey(name)) + if (indexerLogging && setting.Type != "password" && setting.Name != "apikey" && setting.Name != "rsskey" && variables.ContainsKey(name)) { _logger.Debug($"Setting {setting.Name} to {variables[name].ToJson()}"); } diff --git a/src/Prowlarr.Api.V1/Indexers/IndexerResource.cs b/src/Prowlarr.Api.V1/Indexers/IndexerResource.cs index 9ba47c50b..832d7f219 100644 --- a/src/Prowlarr.Api.V1/Indexers/IndexerResource.cs +++ b/src/Prowlarr.Api.V1/Indexers/IndexerResource.cs @@ -152,25 +152,12 @@ namespace Prowlarr.Api.V1.Indexers private object MapValue(SettingsField setting, object value) { - if (setting.Type == "select") + return setting.Type switch { - return value.ToString().ParseInt64() ?? 0; - } - else if (setting.Type == "checkbox") - { - if (bool.TryParse(value.ToString(), out var result)) - { - return result; - } - else - { - return false; - } - } - else - { - return value?.ToString() ?? string.Empty; - } + "select" => value.ToString().ParseInt64() ?? 0, + "checkbox" => bool.TryParse(value.ToString(), out var result) && result, + _ => value?.ToString() ?? string.Empty + }; } private Field MapField(SettingsField setting, int order) @@ -196,14 +183,7 @@ namespace Prowlarr.Api.V1.Indexers } else if (setting.Type == "checkbox") { - if (bool.TryParse(setting.Default, out var value)) - { - field.Value = value; - } - else - { - field.Value = false; - } + field.Value = bool.TryParse(setting.Default, out var value) && value; } else {