using System; using System.Collections.Generic; using System.Collections.Immutable; using ChaosBot.Repositories; using Microsoft.Extensions.Configuration; namespace ChaosBot { public class Configuration { private readonly IConfiguration _appSettingsWrapper; private static readonly Dictionary ConfigurationFlags = new Dictionary { {"LevelUp:Enabled", typeof(bool)}, }; public Configuration() { _appSettingsWrapper = Program.AppSettingsHandler; if (_appSettingsWrapper == null) throw new NullReferenceException("Program.AppSettingsHandler is unset"); } /** * Gets the configuration value associated with a key in an optional guild * Configuration key does not exist * Configuration key does not have the provided type * Configuration value */ public T GetValue(string key, T defaultValue, ulong? guildId = null) { if (!ConfigurationFlags.ContainsKey(key)) throw new ArgumentException($"Configuration does not contain key '{key}'"); if (!(ConfigurationFlags[key] == typeof(T))) throw new ArgumentException($"Configuration flag '{key}<{ConfigurationFlags[key]}>' does not have type '{typeof(T)}'"); if (guildId.HasValue) return ConfigurationRepository.GetValue(key, guildId.Value, defaultValue); return _appSettingsWrapper.GetValue(key, defaultValue); } public T GetValue(string key) { return GetValue(key, default); } public IConfigurationSection GetSection(string key) { return _appSettingsWrapper.GetSection(key); } /** * Get the available configuration flags * Immutable dictionary of config-key/type pairs */ public ImmutableDictionary GetConfigurationFlags() { return ConfigurationFlags.ToImmutableDictionary(); } } }