Skip to content

Commit

Permalink
* fix all violations of Roslyn analyzer rule `AV1130: Return type in …
Browse files Browse the repository at this point in the history
…method signature should be an interface to an unchangeable collection` @ c#
  • Loading branch information
n0099 committed Mar 27, 2024
1 parent a62e931 commit 88d054f
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 20 deletions.
6 changes: 4 additions & 2 deletions c#/crawler/src/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ namespace tbm.Crawler;
public abstract partial class Helper
#pragma warning restore AV1708 // Type name contains term that should be avoided
{
[SuppressMessage("Member Design", "AV1130:Return type in method signature should be an interface to an unchangeable collection")]
public static byte[]? SerializedProtoBufOrNullIfEmpty(IMessage? protoBuf) =>
protoBuf == null || protoBuf.CalculateSize() == 0 ? null : protoBuf.ToByteArray();

[SuppressMessage("Member Design", "AV1130:Return type in method signature should be an interface to an unchangeable collection")]
public static byte[]? SerializedProtoBufWrapperOrNullIfEmpty<T>
(IEnumerable<T>? valuesToWrap, Func<IMessage?> wrapperFactory) where T : class, IMessage =>
valuesToWrap?.Select(message => message.CalculateSize()).Sum() is 0 or null
? null
: SerializedProtoBufOrNullIfEmpty(wrapperFactory());

public static RepeatedField<Content>? ParseThenUnwrapPostContent(byte[]? serializedProtoBuf) =>
public static IReadOnlyList<Content>? ParseThenUnwrapPostContent(byte[]? serializedProtoBuf) =>
serializedProtoBuf == null ? null : PostContentWrapper.Parser.ParseFrom(serializedProtoBuf).Value;

public static PostContentWrapper? WrapPostContent(RepeatedField<Content>? contents) =>
public static PostContentWrapper? WrapPostContent(IReadOnlyList<Content>? contents) =>
contents == null ? null : new() {Value = {contents}};

public static void GetNowTimestamp(out Time now) => now = GetNowTimestamp();
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/SonicPusher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public SonicPusher(ILogger<SonicPusher> logger, IConfiguration config)

public void Dispose() => Ingest.Dispose();

public float PushPost(Fid fid, string type, PostId id, RepeatedField<Content>? content)
public float PushPost(Fid fid, string type, PostId id, IReadOnlyList<Content>? content)
{
if (!_config.GetValue("Enabled", false)) return 0;
var stopwatch = new Stopwatch();
Expand Down Expand Up @@ -61,7 +61,7 @@ public float PushPost(Fid fid, string type, PostId id, RepeatedField<Content>? c
public void PushPostWithCancellationToken<T>(
IReadOnlyCollection<T> posts, Fid fid, string postType,
Func<T, PostId> postIdSelector,
Func<T, RepeatedField<Content>?> postContentSelector,
Func<T, IReadOnlyList<Content>?> postContentSelector,
CancellationToken stoppingToken = default)
{
try
Expand Down
6 changes: 3 additions & 3 deletions c#/crawler/src/Tieba/Crawl/Crawler/BaseCrawler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ public abstract partial class BaseCrawler<TResponse, TPostProtoBuf>
public abstract Exception FillExceptionData(Exception e);

// ReSharper disable once UnusedParameter.Global
public abstract IList<TPostProtoBuf> GetValidPosts(TResponse response, CrawlRequestFlag flag);
public abstract IReadOnlyList<TPostProtoBuf> GetValidPosts(TResponse response, CrawlRequestFlag flag);
public abstract TbClient.Page? GetResponsePage(TResponse response);
protected abstract RepeatedField<TPostProtoBuf> GetResponsePostList(TResponse response);
protected abstract IReadOnlyList<TPostProtoBuf> GetResponsePostList(TResponse response);
protected abstract int GetResponseErrorCode(TResponse response);
protected abstract IEnumerable<Request> GetRequestsForPage(Page page, CancellationToken stoppingToken = default);

Expand All @@ -31,7 +31,7 @@ protected void ValidateOtherErrorCode(TResponse response)
throw new TiebaException("Error from tieba client.") {Data = {{"raw", response}}};
}

protected IList<TPostProtoBuf> EnsureNonEmptyPostList(TResponse response, string exceptionMessage)
protected IReadOnlyList<TPostProtoBuf> EnsureNonEmptyPostList(TResponse response, string exceptionMessage)
{
var posts = GetResponsePostList(response);
return posts.Count != 0 ? posts : throw new EmptyPostListException(exceptionMessage);
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Crawler/ReplyCrawler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public override Exception FillExceptionData(Exception e)
return e;
}

public override IList<Reply> GetValidPosts(ReplyResponse response, CrawlRequestFlag flag)
public override IReadOnlyList<Reply> GetValidPosts(ReplyResponse response, CrawlRequestFlag flag)
{
if (response.Error.Errorno is 4 or 350008)
throw new EmptyPostListException("Thread already deleted when crawling reply.");
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Crawler/SubReplyCrawler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public override Exception FillExceptionData(Exception e)
return e;
}

public override IList<SubReply> GetValidPosts(SubReplyResponse response, CrawlRequestFlag flag)
public override IReadOnlyList<SubReply> GetValidPosts(SubReplyResponse response, CrawlRequestFlag flag)
{
switch (response.Error.Errorno)
{
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Crawler/ThreadCrawler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override Exception FillExceptionData(Exception e)
return e;
}

public override IList<Thread> GetValidPosts(ThreadResponse response, CrawlRequestFlag flag)
public override IReadOnlyList<Thread> GetValidPosts(ThreadResponse response, CrawlRequestFlag flag)
{
ValidateOtherErrorCode(response);
return EnsureNonEmptyPostList(response,
Expand Down
6 changes: 3 additions & 3 deletions c#/crawler/src/Tieba/Crawl/CrawlerLocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CrawlerLocks(ILogger<CrawlerLocks> logger, IConfiguration config, s
public static IEnumerable<string> RegisteredLocks { get; } = ["thread", "threadLate", "reply", "subReply"];
public string LockType { get; } = lockType;

public ISet<Page> AcquireRange(LockId lockId, IEnumerable<Page> pages)
public IReadOnlySet<Page> AcquireRange(LockId lockId, IEnumerable<Page> pages)
{
var acquiredPages = pages.ToHashSet();
lock (_crawling)
Expand Down Expand Up @@ -85,14 +85,14 @@ public void AcquireFailed(LockId lockId, Page page, FailureCount failureCount)
}
}

public IDictionary<LockId, IDictionary<Page, FailureCount>> RetryAllFailed()
public IReadOnlyDictionary<LockId, IReadOnlyDictionary<Page, FailureCount>> RetryAllFailed()
{
lock (_failed)
{
var deepCloneOfFailed = _failed.ToDictionary(pair => pair.Key, pair =>
{
lock (pair.Value)
return (IDictionary<Page, FailureCount>)new Dictionary<Page, FailureCount>(pair.Value);
return (IReadOnlyDictionary<Page, FailureCount>)new Dictionary<Page, FailureCount>(pair.Value);
});
_failed.Clear();
return deepCloneOfFailed;
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Parser/BaseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public abstract class BaseParser<TPost, TPostProtoBuf>
where TPostProtoBuf : class, IMessage<TPostProtoBuf>
{
public void ParsePosts(
CrawlRequestFlag requestFlag, IList<TPostProtoBuf> inPosts,
CrawlRequestFlag requestFlag, IReadOnlyList<TPostProtoBuf> inPosts,
out IDictionary<PostId, TPost> outPosts, out IList<TbClient.User> outUsers)
{
if (ShouldSkipParse(requestFlag))
Expand All @@ -29,7 +29,7 @@ public void ParsePosts(
// ReSharper disable once UnusedMemberInSuper.Global
protected abstract TPost Convert(TPostProtoBuf inPost);
protected abstract IEnumerable<TPost> ParsePostsInternal
(IList<TPostProtoBuf> inPosts, IList<TbClient.User?> outUsers);
(IReadOnlyList<TPostProtoBuf> inPosts, IList<TbClient.User?> outUsers);
protected virtual bool ShouldSkipParse(CrawlRequestFlag requestFlag) => false;
protected abstract PostId PostIdSelector(TPost post);
}
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Parser/ReplyParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public partial class ReplyParser(ILogger<ReplyParser> logger)
protected override PostId PostIdSelector(ReplyPost post) => post.Pid;

protected override IEnumerable<ReplyPost> ParsePostsInternal
(IList<Reply> inPosts, IList<TbClient.User?> outUsers) => inPosts.Select(Convert);
(IReadOnlyList<Reply> inPosts, IList<TbClient.User?> outUsers) => inPosts.Select(Convert);

protected override ReplyPost Convert(Reply inPost)
{
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Parser/SubReplyParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class SubReplyParser : BaseParser<SubReplyPost, SubReply>
protected override PostId PostIdSelector(SubReplyPost post) => post.Spid;

protected override IEnumerable<SubReplyPost> ParsePostsInternal
(IList<SubReply> inPosts, IList<TbClient.User?> outUsers)
(IReadOnlyList<SubReply> inPosts, IList<TbClient.User?> outUsers)
{
outUsers.AddRange(inPosts.Select(sr => sr.Author));
return inPosts.Select(Convert);
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Parser/ThreadParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ protected override bool ShouldSkipParse(CrawlRequestFlag requestFlag) =>
requestFlag == CrawlRequestFlag.ThreadClientVersion602;

protected override IEnumerable<ThreadPost> ParsePostsInternal
(IList<Thread> inPosts, IList<TbClient.User?> outUsers) => inPosts.Select(Convert);
(IReadOnlyList<Thread> inPosts, IList<TbClient.User?> outUsers) => inPosts.Select(Convert);

protected override ThreadPost Convert(Thread inPost)
{
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Worker/RetryCrawlWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override async Task DoWork(CancellationToken stoppingToken)
}
}

private Func<KeyValuePair<CrawlerLocks.LockId, IDictionary<Page, FailureCount>>, Task> RetryFailed
private Func<KeyValuePair<CrawlerLocks.LockId, IReadOnlyDictionary<Page, FailureCount>>, Task> RetryFailed
(string lockType, CancellationToken stoppingToken = default) => async failedPagesKeyByLockId =>
{
if (stoppingToken.IsCancellationRequested) return;
Expand All @@ -55,7 +55,7 @@ await RetrySubReply(fid, tid.Value, pid.Value, pages,
};

private async Task RetryThreadLate(
IDictionary<CrawlerLocks.LockId, IDictionary<Page, FailureCount>> failureCountWithPagesKeyByLockId,
IReadOnlyDictionary<CrawlerLocks.LockId, IReadOnlyDictionary<Page, FailureCount>> failureCountWithPagesKeyByLockId,
CancellationToken stoppingToken = default)
{
await using var threadLate = threadLateCrawlerAndSaverFactory();
Expand Down

0 comments on commit 88d054f

Please sign in to comment.