Skip to content

Commit

Permalink
* fix all violation of Roslyn analyzer rules and ReSharper inspections
Browse files Browse the repository at this point in the history
* rename variable `nonTracked` to `notTracked` in linq query syntax @ `SaverChangeSet.AllAfter`
* fix outdated comments @ SaverWithRevision.cs
@ crawler

* rename params of `(Except|Intersect)ByKey()` from `first` & `second` to `pairs` & `keys` @ shared/ExtensionMethods.cs
@ c#
  • Loading branch information
n0099 committed Jul 24, 2024
1 parent 2ed6374 commit c2f3a7e
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 26 deletions.
2 changes: 0 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/PostSaver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using LinqKit;

namespace tbm.Crawler.Tieba.Crawl.Saver.Post;

public abstract class PostSaver<TPost, TBaseRevision, TPostId>(
Expand Down
2 changes: 0 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using LinqKit;

namespace tbm.Crawler.Tieba.Crawl.Saver.Post;

public partial class ThreadSaver(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ private void Save<TPost, TRevision, TValue>(

// locking key only using AuthorRevision.Fid and Uid, ignoring TriggeredBy
// this prevents inserting multiple entities with similar time and other fields with the same values
// ReSharper disable once NotAccessedPositionalProperty.Global
// ReSharper disable once NotAccessedPositionalProperty.Global
public record UniqueAuthorRevision(Fid Fid, Uid Uid);

Check warning on line 96 in c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs

View workflow job for this annotation

GitHub Actions / runs-on (ubuntu-latest) / ReSharper

"[NotAccessedPositionalProperty.Global] Positional property 'tbm.Crawler.Tieba.Crawl.Saver.Related.AuthorRevisionSaver.UniqueAuthorRevision.Uid' is never accessed (except in implicit Equals/ToString implementations)" on /home/runner/work/open-tbm/open-tbm/c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs(96,4832)

Check warning on line 96 in c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs

View workflow job for this annotation

GitHub Actions / runs-on (macos-latest) / ReSharper

"[NotAccessedPositionalProperty.Global] Positional property 'tbm.Crawler.Tieba.Crawl.Saver.Related.AuthorRevisionSaver.UniqueAuthorRevision.Uid' is never accessed (except in implicit Equals/ToString implementations)" on /Users/runner/work/open-tbm/open-tbm/c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs(96,4832)

Check warning on line 96 in c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs

View workflow job for this annotation

GitHub Actions / runs-on (windows-latest) / ReSharper

"[NotAccessedPositionalProperty.Global] Positional property 'tbm.Crawler.Tieba.Crawl.Saver.Related.AuthorRevisionSaver.UniqueAuthorRevision.Uid' is never accessed (except in implicit Equals/ToString implementations)" on D:\a\open-tbm\open-tbm\c#\crawler\src\Tieba\Crawl\Saver\Related\AuthorRevisionSaver.cs(96,4832)

private sealed class LatestAuthorRevisionProjection<TValue>
Expand Down
6 changes: 3 additions & 3 deletions c#/crawler/src/Tieba/Crawl/Saver/SaverChangeSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class SaverChangeSet<TPost>(

// https://stackoverflow.com/questions/3404975/left-outer-join-in-linq/23558389#23558389
public IReadOnlyCollection<TPost> AllAfter { get; } = (
from nonTracked in existingAfterAndNewlyAdded
from notTracked in existingAfterAndNewlyAdded
join inTracking in existingAfterInTracking
on postIdSelector(nonTracked) equals postIdSelector(inTracking) into inTrackings
on postIdSelector(notTracked) equals postIdSelector(inTracking) into inTrackings
from inTracking in inTrackings.DefaultIfEmpty()
select inTracking ?? nonTracked).ToList().AsReadOnly();
select inTracking ?? notTracked).ToList().AsReadOnly();
}
20 changes: 10 additions & 10 deletions c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,8 @@ public partial class SaverWithRevision<TBaseRevision, TRevisionId>
}
public partial class SaverWithRevision<TBaseRevision, TRevisionId>
{
private static bool IsTimestampingFieldName(string name) => name is nameof(BasePost.LastSeenAt)
or nameof(TimestampedEntity.CreatedAt) or nameof(TimestampedEntity.UpdatedAt);

protected abstract NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName);

protected record ExistingAndNewEntity<TEntity>(TEntity Existing, TEntity New) where TEntity : RowVersionedEntity;
protected record MaybeExistingAndNewEntity<TEntity>(TEntity? Existing, TEntity New) where TEntity : RowVersionedEntity;

protected IEnumerable<ExistingAndNewEntity<TEntity>> SaveNewEntities<TEntity>(
CrawlerDbContext db,
IReadOnlyCollection<MaybeExistingAndNewEntity<TEntity>> maybeEntities)
Expand Down Expand Up @@ -103,7 +97,7 @@ on newNavigation.Metadata.Name equals existingNavigation.Metadata.Name
// rollback changes that overwrite original values with the default value 0 or null
// for all fields of TimestampedEntity and BasePost.LastSeenAt
// this will also affect the entity instance which entityInTracking references to it
// this will also affect the entity instance which existingEntity references to it
entityEntry.Properties
.Where(prop => prop.IsModified && IsTimestampingFieldName(prop.Metadata.Name))
.ForEach(prop => prop.IsModified = false);
Expand Down Expand Up @@ -141,7 +135,7 @@ protected void SaveExistingEntityRevisions<TEntity, TRevision>(
pName, p.OriginalValue, p.CurrentValue)))
{
p.IsModified = false;
continue; // skip following revision check
continue; // skip any further revision check
}
if (FieldRevisionIgnorance(
pName, p.OriginalValue, p.CurrentValue)
Expand All @@ -151,7 +145,7 @@ protected void SaveExistingEntityRevisions<TEntity, TRevision>(
if (!IRevisionProperties.Cache[typeof(TRevision)].TryGetValue(pName, out var revisionProp))
{
object? ToHexWhenByteArray(object? value) =>
static object? ToHexWhenByteArray(object? value) =>
value is byte[] bytes ? bytes.ToHex() : value;
logger.LogWarning("Updating field {} is not existing in revision table, " +
"newValue={}, oldValue={}, newObject={}, oldObject={}",
Expand All @@ -163,7 +157,7 @@ protected void SaveExistingEntityRevisions<TEntity, TRevision>(
{
revision ??= revisionFactory(existingEntity);
// quote from MSDN https://learn.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.setvalue
// https://learn.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.setvalue
// If the property type of this PropertyInfo object is a value type and value is null
// the property will be set to the default value for that type.
// https://stackoverflow.com/questions/3049477/propertyinfo-setvalue-and-nulls
Expand Down Expand Up @@ -195,4 +189,10 @@ protected void SaveExistingEntityRevisions<TEntity, TRevision>(
.GroupBy(pair => pair.Key, pair => pair.Value)
.ForEach(g => AddSplitRevisionsDelegatesKeyByEntityType.Value[g.Key](db, g));
}

private static bool IsTimestampingFieldName(string name) => name is nameof(BasePost.LastSeenAt)
or nameof(TimestampedEntity.CreatedAt) or nameof(TimestampedEntity.UpdatedAt);

protected record ExistingAndNewEntity<TEntity>(TEntity Existing, TEntity New) where TEntity : RowVersionedEntity;
protected record MaybeExistingAndNewEntity<TEntity>(TEntity? Existing, TEntity New) where TEntity : RowVersionedEntity;
}
12 changes: 6 additions & 6 deletions c#/shared/src/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public static IEnumerable<TValue> Values<TKey, TValue>(this IEnumerable<KeyValue
pairs.Select(i => i.Value);

public static IEnumerable<KeyValuePair<TKey, TValue>> ExceptByKey<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> first,
IEnumerable<TKey> second) =>
first.ExceptBy(second, pair => pair.Key);
this IEnumerable<KeyValuePair<TKey, TValue>> pairs,
IEnumerable<TKey> keys) =>
pairs.ExceptBy(keys, pair => pair.Key);
public static IEnumerable<KeyValuePair<TKey, TValue>> IntersectByKey<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> first,
IEnumerable<TKey> second) =>
first.IntersectBy(second, pair => pair.Key);
this IEnumerable<KeyValuePair<TKey, TValue>> pairs,
IEnumerable<TKey> keys) =>
pairs.IntersectBy(keys, pair => pair.Key);

public static IEnumerable<T> Flatten2<T>(this IEnumerable<IEnumerable<T>> source) =>
source.SelectMany(i => i);
Expand Down
6 changes: 3 additions & 3 deletions c#/shared/tbm.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.11" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="SuperLinq" Version="6.1.0" />
<PackageReference Include="SuperLinq" Version="6.2.0" />
</ItemGroup>
<Choose>
<When Condition="'$(Configuration)' == 'Release'">
Expand All @@ -18,12 +18,12 @@
<PackageReference ExcludeAssets="compile" Include="codecracker.CSharp" Version="1.1.0" />
<PackageReference ExcludeAssets="compile" Include="CSharpGuidelinesAnalyzer" Version="3.8.5" />
<PackageReference ExcludeAssets="compile" Include="IDisposableAnalyzers" Version="4.0.8" />
<PackageReference ExcludeAssets="compile" Include="Meziantou.Analyzer" Version="2.0.160" />
<PackageReference ExcludeAssets="compile" Include="Meziantou.Analyzer" Version="2.0.161" />
<PackageReference ExcludeAssets="compile" Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0" />
<PackageReference ExcludeAssets="compile" Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.10.48" />
<PackageReference ExcludeAssets="compile" Include="Roslynator.Analyzers" Version="4.12.4" />
<PackageReference ExcludeAssets="compile" Include="SharpSource" Version="1.24.0" />
<PackageReference ExcludeAssets="compile" Include="SonarAnalyzer.CSharp" Version="9.28.0.94264" />
<PackageReference ExcludeAssets="compile" Include="SonarAnalyzer.CSharp" Version="9.30.0.95878" />
<PackageReference ExcludeAssets="compile" Include="StyleCop.Analyzers.Unstable" Version="1.2.0.556" />
</ItemGroup>
</When>
Expand Down

0 comments on commit c2f3a7e

Please sign in to comment.