diff --git a/ChaosBot/ConfigHelpers/Configuration.cs b/ChaosBot/ConfigHelpers/Configuration.cs index 2260457..fb17914 100644 --- a/ChaosBot/ConfigHelpers/Configuration.cs +++ b/ChaosBot/ConfigHelpers/Configuration.cs @@ -54,20 +54,19 @@ namespace ChaosBot.ConfigHelpers */ public T GetValue(string key, T defaultValue, ulong? guildId = null) { - bool keyExists = TryGetKeyFromRegexMatch(key, out string realKey); + string realKey = EnsureKeyExistsAndTypeCorrect(key); - if (!keyExists) - throw new ArgumentException($"Configuration does not contain key '{key}'"); - - if (!(ConfigurationFlags[realKey].type == typeof(T))) - throw new ArgumentException($"Configuration flag '{realKey}<{ConfigurationFlags[realKey]}>' does not have type '{typeof(T)}'"); - if (guildId.HasValue) return ConfigurationRepository.GetValue(realKey, guildId.Value, defaultValue); return _appSettingsWrapper.GetValue(realKey, defaultValue); } + public T GetValue(string key) + { + return GetValue(key, default); + } + public T GetValueGlobalDefault(string key, ulong? guildId = null) { bool keyExists = TryGetKeyFromRegexMatch(key, out string realKey); @@ -80,11 +79,6 @@ namespace ChaosBot.ConfigHelpers return GetValue(key, defaultValue); } - public T GetValue(string key) - { - return GetValue(key, default); - } - public IConfigurationSection GetSection(string key) { return _appSettingsWrapper.GetSection(key); @@ -99,6 +93,19 @@ namespace ChaosBot.ConfigHelpers return ConfigurationFlags.ToImmutableDictionary(); } + private string EnsureKeyExistsAndTypeCorrect(string key) + { + bool keyExists = TryGetKeyFromRegexMatch(key, out string realKey); + + if (!keyExists) + throw new ArgumentException($"Configuration does not contain key '{key}'"); + + if (!(ConfigurationFlags[realKey].type == typeof(T))) + throw new ArgumentException( + $"Configuration flag '{realKey}<{ConfigurationFlags[realKey]}>' does not have type '{typeof(T)}'"); + return realKey; + } + private bool TryGetKeyFromRegexMatch(string key, out string realKey) { if (ConfigurationFlags.ContainsKey(key))