From 413cc4ac134b301b3229cb393fb0b86f4f5197de Mon Sep 17 00:00:00 2001 From: skwasjer <11424653+skwasjer@users.noreply.github.com> Date: Wed, 9 Oct 2024 05:08:42 +0200 Subject: [PATCH] fix: sta warnings --- src/IbanNet.CodeGen/Wikipedia/Loader.cs | 17 ++++++++++++----- src/IbanNet.CodeGen/Wikipedia/ParseResult.cs | 8 ++++---- src/IbanNet.CodeGen/Wikipedia/WikiResponse.cs | 4 ++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/IbanNet.CodeGen/Wikipedia/Loader.cs b/src/IbanNet.CodeGen/Wikipedia/Loader.cs index c6771fa..503feae 100644 --- a/src/IbanNet.CodeGen/Wikipedia/Loader.cs +++ b/src/IbanNet.CodeGen/Wikipedia/Loader.cs @@ -7,17 +7,24 @@ namespace IbanNet.CodeGen.Wikipedia; public static class Loader { + private static readonly JsonSerializerOptions JsonSerializerOptions = new() { PropertyNameCaseInsensitive = true }; + public static WikiResult GetWikiData() { +#pragma warning disable S1075 var uri = new Uri("https://en.wikipedia.org/w/api.php?format=json&action=parse&page=International_Bank_Account_Number§ion=16", UriKind.Absolute); +#pragma warning restore S1075 HttpWebRequest req = WebRequest.CreateHttp(uri); using WebResponse response = req.GetResponse(); using var ms = new MemoryStream(); - response.GetResponseStream().CopyTo(ms); + response.GetResponseStream()!.CopyTo(ms); byte[] buffer = ms.ToArray(); - var jsonOpts = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; - WikiResponse? wikiResponse = JsonSerializer.Deserialize(buffer, jsonOpts); + WikiResponse? wikiResponse = JsonSerializer.Deserialize(buffer, JsonSerializerOptions); + if (wikiResponse is null) + { + throw new InvalidOperationException("Unexpected response."); + } var tableRegex = new Regex("(]*?>.*<\\/table>)", RegexOptions.Singleline); Match tableMatch = tableRegex.Match(wikiResponse.Parse.Text["*"]); @@ -25,7 +32,7 @@ public static WikiResult GetWikiData() var doc = new HtmlDocument(); doc.LoadHtml($"
${tableMatch.Value}
"); - IEnumerable? records = doc.DocumentNode.SelectNodes("//tr") + IEnumerable records = doc.DocumentNode.SelectNodes("//tr") .GroupBy(e => e.ParentNode) .Select(g => g.Where(e => e.Element("td") != null)) .Select(rows => rows.Select(r => @@ -38,7 +45,7 @@ public static WikiResult GetWikiData() Pattern = cells[2].InnerText.Trim().Replace(" ", "") }; })) - .SelectMany(_ => _); + .SelectMany(x => x); return new WikiResult(records, wikiResponse.Parse); } diff --git a/src/IbanNet.CodeGen/Wikipedia/ParseResult.cs b/src/IbanNet.CodeGen/Wikipedia/ParseResult.cs index 68291ac..3206d56 100644 --- a/src/IbanNet.CodeGen/Wikipedia/ParseResult.cs +++ b/src/IbanNet.CodeGen/Wikipedia/ParseResult.cs @@ -1,8 +1,8 @@ namespace IbanNet.CodeGen.Wikipedia; -public class ParseResult +public sealed record ParseResult { - public int PageId { get; set; } - public int RevId { get; set; } - public Dictionary Text { get; set; } + public int PageId { get; init; } + public int RevId { get; init; } + public Dictionary Text { get; init; } = []; } diff --git a/src/IbanNet.CodeGen/Wikipedia/WikiResponse.cs b/src/IbanNet.CodeGen/Wikipedia/WikiResponse.cs index d1521fc..f5ddf8c 100644 --- a/src/IbanNet.CodeGen/Wikipedia/WikiResponse.cs +++ b/src/IbanNet.CodeGen/Wikipedia/WikiResponse.cs @@ -1,6 +1,6 @@ namespace IbanNet.CodeGen.Wikipedia; -public class WikiResponse +public sealed record WikiResponse { - public ParseResult Parse { get; set; } + public ParseResult Parse { get; init; } = default!; }