[backend] Manually reformat difficult-to-read switch expressions that got hard wrapped

This commit is contained in:
Laura Hausmann 2024-07-11 17:35:41 +02:00
parent f58d8da8ae
commit c67984c411
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
4 changed files with 62 additions and 52 deletions

View file

@ -139,10 +139,11 @@ public sealed class Config
MaxUploadSizeBytes = suffix switch
{
null => num,
null => num,
'k' or 'K' => num * 1024,
'm' or 'M' => num * 1024 * 1024,
'g' or 'G' => num * 1024 * 1024 * 1024,
_ => throw new Exception("Unsupported suffix, use one of: [K]ilobytes, [M]egabytes, [G]igabytes")
};
}
@ -169,10 +170,11 @@ public sealed class Config
MaxCacheSizeBytes = suffix switch
{
null => num,
null => num,
'k' or 'K' => num * 1024,
'm' or 'M' => num * 1024 * 1024,
'g' or 'G' => num * 1024 * 1024 * 1024,
_ => throw new Exception("Unsupported suffix, use one of: [K]ilobytes, [M]egabytes, [G]igabytes")
};
}
@ -229,10 +231,11 @@ public sealed class Config
MaxFileSizeBytes = suffix switch
{
null => num,
null => num,
'k' or 'K' => num * 1024,
'm' or 'M' => num * 1024 * 1024,
'g' or 'G' => num * 1024 * 1024 * 1024,
_ => throw new Exception("Unsupported suffix, use one of: [K]ilobytes, [M]egabytes, [G]igabytes")
};
}

View file

@ -29,22 +29,22 @@ public static class QueryableExtensions
if (pq is { SinceId: not null, MinId: not null })
throw GracefulException.BadRequest("Can't use sinceId and minId params simultaneously");
// @formatter:off
query = pq switch
{
{ SinceId: not null, MaxId: not null } => query
.Where(p => p.Id.IsGreaterThan(pq.SinceId) &&
p.Id.IsLessThan(pq.MaxId))
.OrderByDescending(p => p.Id),
{ MinId: not null, MaxId: not null } => query
.Where(p => p.Id.IsGreaterThan(pq.MinId) &&
p.Id.IsLessThan(pq.MaxId))
.OrderBy(p => p.Id),
{ SinceId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.SinceId))
.OrderByDescending(p => p.Id),
{ MinId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.MinId)).OrderBy(p => p.Id),
{ MaxId: not null } => query.Where(p => p.Id.IsLessThan(pq.MaxId)).OrderByDescending(p => p.Id),
_ => query.OrderByDescending(p => p.Id)
{ SinceId: not null, MaxId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.SinceId) && p.Id.IsLessThan(pq.MaxId))
.OrderByDescending(p => p.Id),
{ MinId: not null, MaxId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.MinId) && p.Id.IsLessThan(pq.MaxId))
.OrderBy(p => p.Id),
{ SinceId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.SinceId))
.OrderByDescending(p => p.Id),
{ MinId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.MinId))
.OrderBy(p => p.Id),
{ MaxId: not null } => query.Where(p => p.Id.IsLessThan(pq.MaxId))
.OrderByDescending(p => p.Id),
_ => query.OrderByDescending(p => p.Id)
};
// @formatter:on
return query.Skip(pq.Offset ?? 0).Take(Math.Min(pq.Limit ?? defaultLimit, maxLimit));
}
@ -63,24 +63,22 @@ public static class QueryableExtensions
if (pq is { SinceId: not null, MinId: not null })
throw GracefulException.BadRequest("Can't use sinceId and minId params simultaneously");
// @formatter:off
query = pq switch
{
{ SinceId: not null, MaxId: not null } => query
.Where(predicate.Compose(id => id.IsGreaterThan(pq.SinceId) &&
id.IsLessThan(pq.MaxId)))
.OrderByDescending(predicate),
{ MinId: not null, MaxId: not null } => query
.Where(predicate.Compose(id => id.IsGreaterThan(pq.MinId) &&
id.IsLessThan(pq.MaxId)))
.OrderBy(predicate),
{ SinceId: not null } => query.Where(predicate.Compose(id => id.IsGreaterThan(pq.SinceId)))
.OrderByDescending(predicate),
{ MinId: not null } => query.Where(predicate.Compose(id => id.IsGreaterThan(pq.MinId)))
.OrderBy(predicate),
{ MaxId: not null } => query.Where(predicate.Compose(id => id.IsLessThan(pq.MaxId)))
.OrderByDescending(predicate),
_ => query.OrderByDescending(predicate)
{ SinceId: not null, MaxId: not null } => query.Where(predicate.Compose(id => id.IsGreaterThan(pq.SinceId) && id.IsLessThan(pq.MaxId)))
.OrderByDescending(predicate),
{ MinId: not null, MaxId: not null } => query.Where(predicate.Compose(id => id.IsGreaterThan(pq.MinId) && id.IsLessThan(pq.MaxId)))
.OrderBy(predicate),
{ SinceId: not null } => query.Where(predicate.Compose(id => id.IsGreaterThan(pq.SinceId)))
.OrderByDescending(predicate),
{ MinId: not null } => query.Where(predicate.Compose(id => id.IsGreaterThan(pq.MinId)))
.OrderBy(predicate),
{ MaxId: not null } => query.Where(predicate.Compose(id => id.IsLessThan(pq.MaxId)))
.OrderByDescending(predicate),
_ => query.OrderByDescending(predicate)
};
// @formatter:on
return query.Skip(pq.Offset ?? 0).Take(Math.Min(pq.Limit ?? defaultLimit, maxLimit));
}
@ -124,17 +122,22 @@ public static class QueryableExtensions
maxId = res;
}
// @formatter:off
query = pq switch
{
{ SinceId: not null, MaxId: not null } => query.Where(predicate.Compose(id => id > sinceId && id < maxId))
.OrderByDescending(predicate),
{ MinId: not null, MaxId: not null } => query.Where(predicate.Compose(id => id > minId && id < maxId))
.OrderBy(predicate),
{ SinceId: not null } => query.Where(predicate.Compose(id => id > sinceId)).OrderByDescending(predicate),
{ MinId: not null } => query.Where(predicate.Compose(id => id > minId)).OrderBy(predicate),
{ MaxId: not null } => query.Where(predicate.Compose(id => id < maxId)).OrderByDescending(predicate),
_ => query.OrderByDescending(predicate)
{ MinId: not null, MaxId: not null } => query.Where(predicate.Compose(id => id > minId && id < maxId))
.OrderBy(predicate),
{ SinceId: not null } => query.Where(predicate.Compose(id => id > sinceId))
.OrderByDescending(predicate),
{ MinId: not null } => query.Where(predicate.Compose(id => id > minId))
.OrderBy(predicate),
{ MaxId: not null } => query.Where(predicate.Compose(id => id < maxId))
.OrderByDescending(predicate),
_ => query.OrderByDescending(predicate)
};
// @formatter:on
return query.Skip(pq.Offset ?? 0).Take(Math.Min(pq.Limit ?? defaultLimit, maxLimit));
}
@ -149,16 +152,18 @@ public static class QueryableExtensions
if (pq.Limit is < 1)
throw GracefulException.BadRequest("Limit cannot be less than 1");
// @formatter:off
query = pq switch
{
{ MinId: not null, MaxId: not null } => query
.Where(p => p.Id.IsGreaterThan(pq.MinId) &&
p.Id.IsLessThan(pq.MaxId))
.OrderBy(p => p.Id),
{ MinId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.MinId)).OrderBy(p => p.Id),
{ MaxId: not null } => query.Where(p => p.Id.IsLessThan(pq.MaxId)).OrderByDescending(p => p.Id),
_ => query.OrderByDescending(p => p.Id)
{ MinId: not null, MaxId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.MinId) && p.Id.IsLessThan(pq.MaxId))
.OrderBy(p => p.Id),
{ MinId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.MinId))
.OrderBy(p => p.Id),
{ MaxId: not null } => query.Where(p => p.Id.IsLessThan(pq.MaxId))
.OrderByDescending(p => p.Id),
_ => query.OrderByDescending(p => p.Id)
};
// @formatter:on
return query.Take(Math.Min(pq.Limit ?? defaultLimit, maxLimit));
}

View file

@ -66,15 +66,16 @@ public class WebFingerService(
else if (query.StartsWith('@'))
{
proto = "https";
var split = query.Split('@');
// @formatter:off
domain = split.Length switch
{
< 2 or > 3 => throw new GracefulException(HttpStatusCode.BadRequest, $"Invalid query: {query}"),
2 => throw new GracefulException(HttpStatusCode.BadRequest,
$"Can't run WebFinger for local user: {query}"),
_ => split[2]
2 => throw new GracefulException(HttpStatusCode.BadRequest, $"Can't run WebFinger for local user: {query}"),
_ => split[2]
};
// @formatter:on
}
else
{

View file

@ -153,14 +153,15 @@ file static class QueryableExtensions
this IQueryable<InboxQueryResult> query, Enums.FederationMode mode, DatabaseContext db
)
{
// @formatter:off
Expression<Func<InboxQueryResult, bool>> expr = mode switch
{
Enums.FederationMode.BlockList => u =>
u.Host == null || !db.BlockedInstances.Any(p => u.Host == p.Host || u.Host.EndsWith("." + p.Host)),
Enums.FederationMode.AllowList => u =>
u.Host == null || db.AllowedInstances.Any(p => u.Host == p.Host || u.Host.EndsWith("." + p.Host)),
Enums.FederationMode.BlockList => u => u.Host == null || !db.BlockedInstances.Any(p => u.Host == p.Host || u.Host.EndsWith("." + p.Host)),
Enums.FederationMode.AllowList => u => u.Host == null || db.AllowedInstances.Any(p => u.Host == p.Host || u.Host.EndsWith("." + p.Host)),
_ => throw new ArgumentOutOfRangeException(nameof(mode), mode, null)
};
// @formatter:on
return query.Where(expr);
}