using System.Collections.Immutable; using System.Text.Json; namespace NahidaImpact.Common.Data.Excel; public class ExcelTable { public int Count => _items.Length; private readonly ImmutableArray _items; public ExcelTable(JsonDocument document, Type type) { _items = LoadData(document, type); } private static ImmutableArray LoadData(JsonDocument document, Type type) { ImmutableArray.Builder items = ImmutableArray.CreateBuilder(); foreach (JsonElement element in document.RootElement.EnumerateArray()) { if (element.ValueKind != JsonValueKind.Object) throw new ArgumentException($"ExcelTable::LoadData - expected an object, got {element.ValueKind}"); ExcelItem deserialized = (element.Deserialize(type) as ExcelItem)!; items.Add(deserialized); } return items.ToImmutable(); } public TExcel GetItemAt(int index) where TExcel : ExcelItem { return (_items[index] as TExcel)!; } public TExcel? GetItemById(uint id) where TExcel : ExcelItem { foreach (ExcelItem item in _items) { if (item.ExcelId == id) return item as TExcel; } return null; } }