Skip to content

Commit

Permalink
* fix all violation of Roslyn analyzer rules
Browse files Browse the repository at this point in the history
* move some clsses under namespaces `tbm.Crawler.Tieba.Crawl.Saver` into a new sub namespace `Related`
@ c#/crawler
  • Loading branch information
n0099 committed Jul 11, 2024
1 parent 7b20d0f commit 5a599c9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
25 changes: 14 additions & 11 deletions c#/crawler/src/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static void SetIfNotNull<T1, T2>(this IDictionary<T1, T2> dict, T1 key, T

/// <see>https://github.com/npgsql/npgsql/issues/4437</see>
/// <see>https://github.com/dotnet/efcore/issues/32092#issuecomment-2221633692</see>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618:Generic type parameters should be documented")]
public static IQueryable<TEntity> WhereOrContainsValues<TEntity, TToCompare>(
this IQueryable<TEntity> queryable,
IEnumerable<TToCompare> valuesToCompare,
Expand All @@ -75,6 +76,7 @@ public static IQueryable<TEntity> WhereOrContainsValues<TEntity, TToCompare>(
innerPredicate.And(expressionFactory(valueToCompare))))));

/// <see>https://stackoverflow.com/questions/67666649/lambda-linq-with-contains-criteria-for-multiple-keywords/67666993#67666993</see>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618:Generic type parameters should be documented")]
public static IQueryable<T> FilterByItems<T, TItem>(
this IQueryable<T> query,
IEnumerable<TItem> items,
Expand All @@ -89,28 +91,29 @@ public static IQueryable<T> FilterByItems<T, TItem>(
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<Func<T, bool>>(predicate, filterPattern.Parameters[0]);

return query.Where(filterLambda);
}

private class FilterByItemsExpressionReplacer(IDictionary<Expression, Expression> replaceMap) : ExpressionVisitor
private sealed class FilterByItemsExpressionReplacer(IDictionary<Expression, Expression> replaceMap) : ExpressionVisitor
{
private readonly IDictionary<Expression, Expression> _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<Expression, Expression> _replaceMap =
replaceMap ?? throw new ArgumentNullException(nameof(replaceMap));

public static Expression Replace(Expression expr, Expression toReplace, Expression toExpr) =>
new FilterByItemsExpressionReplacer(new Dictionary<Expression, Expression> {{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);
}
}
1 change: 1 addition & 0 deletions c#/crawler/src/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace tbm.Crawler.Tieba.Crawl.Saver;
namespace tbm.Crawler.Tieba.Crawl.Saver.Related;

public sealed class ReplyContentImageSaver(ILogger<ReplyContentImageSaver> logger) : IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ReplySignatureSaver> logger,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace tbm.Crawler.Tieba.Crawl.Saver;
namespace tbm.Crawler.Tieba.Crawl.Saver.Related;

public class ThreadLatestReplierSaver(SaverLocks<ThreadLatestReplierSaver.UniqueLatestReplier>.New saverLocksFactory)
{
Expand Down

0 comments on commit 5a599c9

Please sign in to comment.