From 5a599c9f8a4995b2c2fcecbb1a886a7b37e0c111 Mon Sep 17 00:00:00 2001 From: n0099 Date: Thu, 11 Jul 2024 13:18:47 +0000 Subject: [PATCH] * fix all violation of Roslyn analyzer rules * move some clsses under namespaces `tbm.Crawler.Tieba.Crawl.Saver` into a new sub namespace `Related` @ c#/crawler --- c#/crawler/src/ExtensionMethods.cs | 25 +++++++++++-------- c#/crawler/src/GlobalUsings.cs | 1 + .../Tieba/Crawl/Facade/ThreadCrawlFacade.cs | 10 +++++--- .../{ => Related}/AuthorRevisionSaver.cs | 2 +- .../{ => Related}/ReplyContentImageSaver.cs | 2 +- .../{ => Related}/ReplySignatureSaver.cs | 2 +- .../{ => Related}/ThreadLatestReplierSaver.cs | 2 +- 7 files changed, 25 insertions(+), 19 deletions(-) rename c#/crawler/src/Tieba/Crawl/Saver/{ => Related}/AuthorRevisionSaver.cs (98%) rename c#/crawler/src/Tieba/Crawl/Saver/{ => Related}/ReplyContentImageSaver.cs (99%) rename c#/crawler/src/Tieba/Crawl/Saver/{ => Related}/ReplySignatureSaver.cs (98%) rename c#/crawler/src/Tieba/Crawl/Saver/{ => Related}/ThreadLatestReplierSaver.cs (97%) diff --git a/c#/crawler/src/ExtensionMethods.cs b/c#/crawler/src/ExtensionMethods.cs index 0f074bed..cf751682 100644 --- a/c#/crawler/src/ExtensionMethods.cs +++ b/c#/crawler/src/ExtensionMethods.cs @@ -62,6 +62,7 @@ public static void SetIfNotNull(this IDictionary dict, T1 key, T /// https://github.com/npgsql/npgsql/issues/4437 /// https://github.com/dotnet/efcore/issues/32092#issuecomment-2221633692 + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618:Generic type parameters should be documented")] public static IQueryable WhereOrContainsValues( this IQueryable queryable, IEnumerable valuesToCompare, @@ -75,6 +76,7 @@ public static IQueryable WhereOrContainsValues( innerPredicate.And(expressionFactory(valueToCompare)))))); /// https://stackoverflow.com/questions/67666649/lambda-linq-with-contains-criteria-for-multiple-keywords/67666993#67666993 + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618:Generic type parameters should be documented")] public static IQueryable FilterByItems( this IQueryable query, IEnumerable items, @@ -89,28 +91,29 @@ public static IQueryable FilterByItems( return current == null ? itemCondition +#pragma warning disable S3358 // Ternary operators should not be nested : Expression.MakeBinary(isOr ? ExpressionType.OrElse : ExpressionType.AndAlso, - current, - itemCondition); +#pragma warning restore S3358 // Ternary operators should not be nested + current, itemCondition); }) ?? Expression.Constant(false); var filterLambda = Expression.Lambda>(predicate, filterPattern.Parameters[0]); return query.Where(filterLambda); } - private class FilterByItemsExpressionReplacer(IDictionary replaceMap) : ExpressionVisitor + private sealed class FilterByItemsExpressionReplacer(IDictionary replaceMap) : ExpressionVisitor { - private readonly IDictionary _replaceMap - = replaceMap ?? throw new ArgumentNullException(nameof(replaceMap)); - - [return: NotNullIfNotNull(nameof(exp))] - public override Expression? Visit(Expression? exp) => - exp != null && _replaceMap.TryGetValue(exp, out var replacement) - ? replacement - : base.Visit(exp); + private readonly IDictionary _replaceMap = + replaceMap ?? throw new ArgumentNullException(nameof(replaceMap)); public static Expression Replace(Expression expr, Expression toReplace, Expression toExpr) => new FilterByItemsExpressionReplacer(new Dictionary {{toReplace, toExpr}}) .Visit(expr); + + [return: NotNullIfNotNull(nameof(node))] + public override Expression? Visit(Expression? node) => + node != null && _replaceMap.TryGetValue(node, out var replacement) + ? replacement + : base.Visit(node); } } diff --git a/c#/crawler/src/GlobalUsings.cs b/c#/crawler/src/GlobalUsings.cs index 3ac5a9a2..77a6014e 100644 --- a/c#/crawler/src/GlobalUsings.cs +++ b/c#/crawler/src/GlobalUsings.cs @@ -45,6 +45,7 @@ global using tbm.Crawler.Tieba.Crawl.Parser.Post; global using tbm.Crawler.Tieba.Crawl.Saver; global using tbm.Crawler.Tieba.Crawl.Saver.Post; +global using tbm.Crawler.Tieba.Crawl.Saver.Related; global using tbm.Crawler.Worker; global using tbm.Shared; global using tbm.Shared.Db; diff --git a/c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs b/c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs index 0988a7c4..322063c0 100644 --- a/c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs +++ b/c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs @@ -48,15 +48,17 @@ join parsed in Posts.Values on (Tid)inResponse.Tid equals parsed.Tid var nameShow = t.inResponse.LastReplyer.NameShow.NullIfEmpty(); // LastReplyer will be null when LivePostType != "", but LastTimeInt will have expected timestamp value - var latestReplierEntity = t.inResponse.LastReplyer == null ? null : new LatestReplier() + var latestReplierEntity = t.inResponse.LastReplyer == null ? null : new LatestReplier { Name = name, +#pragma warning disable S3358 // Ternary operators should not be nested DisplayName = name == nameShow ? null : nameShow +#pragma warning restore S3358 // Ternary operators should not be nested }; var uniqueLatestReplier = ThreadLatestReplierSaver.UniqueLatestReplier.FromLatestReplier(latestReplierEntity); - t.parsed.LatestReplier = _latestRepliersKeyByUnique.TryGetValue(uniqueLatestReplier, out var existingLatestReplier) - ? existingLatestReplier - : _latestRepliersKeyByUnique[uniqueLatestReplier] = latestReplierEntity; + var isExists = _latestRepliersKeyByUnique.TryGetValue(uniqueLatestReplier, out var existingLatestReplier); + if (!isExists) _latestRepliersKeyByUnique[uniqueLatestReplier] = latestReplierEntity; + t.parsed.LatestReplier = isExists ? existingLatestReplier : latestReplierEntity; }); } diff --git a/c#/crawler/src/Tieba/Crawl/Saver/AuthorRevisionSaver.cs b/c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs similarity index 98% rename from c#/crawler/src/Tieba/Crawl/Saver/AuthorRevisionSaver.cs rename to c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs index 5897bf71..6371ecd9 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/AuthorRevisionSaver.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs @@ -1,4 +1,4 @@ -namespace tbm.Crawler.Tieba.Crawl.Saver; +namespace tbm.Crawler.Tieba.Crawl.Saver.Related; // locks only using AuthorRevision.Fid and Uid, ignoring TriggeredBy // this prevents inserting multiple entities with similar time and other fields with the same values diff --git a/c#/crawler/src/Tieba/Crawl/Saver/ReplyContentImageSaver.cs b/c#/crawler/src/Tieba/Crawl/Saver/Related/ReplyContentImageSaver.cs similarity index 99% rename from c#/crawler/src/Tieba/Crawl/Saver/ReplyContentImageSaver.cs rename to c#/crawler/src/Tieba/Crawl/Saver/Related/ReplyContentImageSaver.cs index dbcbcfcc..9944bf79 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/ReplyContentImageSaver.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/Related/ReplyContentImageSaver.cs @@ -1,4 +1,4 @@ -namespace tbm.Crawler.Tieba.Crawl.Saver; +namespace tbm.Crawler.Tieba.Crawl.Saver.Related; public sealed class ReplyContentImageSaver(ILogger logger) : IDisposable { diff --git a/c#/crawler/src/Tieba/Crawl/Saver/ReplySignatureSaver.cs b/c#/crawler/src/Tieba/Crawl/Saver/Related/ReplySignatureSaver.cs similarity index 98% rename from c#/crawler/src/Tieba/Crawl/Saver/ReplySignatureSaver.cs rename to c#/crawler/src/Tieba/Crawl/Saver/Related/ReplySignatureSaver.cs index 8a411057..98fb0c48 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/ReplySignatureSaver.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/Related/ReplySignatureSaver.cs @@ -1,6 +1,6 @@ using System.IO.Hashing; -namespace tbm.Crawler.Tieba.Crawl.Saver; +namespace tbm.Crawler.Tieba.Crawl.Saver.Related; public class ReplySignatureSaver( ILogger logger, diff --git a/c#/crawler/src/Tieba/Crawl/Saver/ThreadLatestReplierSaver.cs b/c#/crawler/src/Tieba/Crawl/Saver/Related/ThreadLatestReplierSaver.cs similarity index 97% rename from c#/crawler/src/Tieba/Crawl/Saver/ThreadLatestReplierSaver.cs rename to c#/crawler/src/Tieba/Crawl/Saver/Related/ThreadLatestReplierSaver.cs index cace23b9..d8cfa431 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/ThreadLatestReplierSaver.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/Related/ThreadLatestReplierSaver.cs @@ -1,4 +1,4 @@ -namespace tbm.Crawler.Tieba.Crawl.Saver; +namespace tbm.Crawler.Tieba.Crawl.Saver.Related; public class ThreadLatestReplierSaver(SaverLocks.New saverLocksFactory) {