using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using System.Text.Json; namespace Core.Config; public class ConfigCollection { private readonly ImmutableDictionary _configs; public ConfigCollection(JsonDocument json, Type type) { _configs = LoadConfigs(json, type); } public int Count => _configs.Count; public TConfig At(int index) where TConfig : IConfig { return (TConfig)_configs.Values.ElementAt(index); } public IEnumerable Enumerate() where TConfig : IConfig { return _configs.Values.Cast(); } public bool TryGet(int identifier, [NotNullWhen(true)] out TConfig? config) where TConfig : IConfig { bool result = _configs.TryGetValue(identifier, out IConfig? cfg); config = (TConfig?)cfg; return result; } private static ImmutableDictionary LoadConfigs(JsonDocument json, Type type) { var builder = ImmutableDictionary.CreateBuilder(); foreach (JsonElement element in json.RootElement.EnumerateArray()) { if (element.ValueKind != JsonValueKind.Object) throw new InvalidDataException($"LoadConfigs: expected array of {JsonValueKind.Object}, got array of {element.ValueKind}"); IConfig configItem = (element.Deserialize(type) as IConfig)!; builder.Add(configItem.Identifier, configItem); } return builder.ToImmutable(); } }