Skip to content

Commit

Permalink
Updating to new collection initialization syntax, and fixing other wa…
Browse files Browse the repository at this point in the history
…rnings
  • Loading branch information
monoman committed Feb 14, 2024
1 parent 12ce53f commit b59fb40
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static IEnumerable<T> RequireNonNulls<T>(this IEnumerable<T?>? items, [Ca
? items.Safe().Cast<T>()
: throw new ArgumentException("List must not contain null elements", parameterName);

public static IEnumerable<T> Safe<T>(this IEnumerable<T>? values) => values ?? Enumerable.Empty<T>();
public static IEnumerable<T> Safe<T>(this IEnumerable<T>? values) => values ?? [];

public static bool SafeAny<T>([NotNullWhen(true)] this IEnumerable<T>? values) => values?.Any() ?? false;

Expand Down Expand Up @@ -187,7 +187,7 @@ public static async Task<IEnumerable<T>> WithDefaultAsync<T>(
Func<Task<IEnumerable<T>>> getAlternativeValuesAsync) where T : class =>
await WithDefaultAsync(getValuesAsync is not null
? (await getValuesAsync.ConfigureAwait(false))
: Enumerable.Empty<T>(),
: [],
getAlternativeValuesAsync).ConfigureAwait(false);

public static async Task<IEnumerable<T>> WithDefaultAsync<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static async Task<IEnumerable<T>> WhenAllAsync<T>(this IEnumerable<Task<T

public static async Task<IEnumerable<T>> WhenAllSequentialAsync<T>(this IEnumerable<Task<T>> tasks) {
if (tasks.None())
return Enumerable.Empty<T>();
return [];
var results = new List<T>();
foreach (var task in tasks)
results.Add(await task.ConfigureAwait(false));
Expand All @@ -76,7 +76,7 @@ public static async Task WhenAllSequentialAsync(this IEnumerable<Task> tasks) {

public static async Task<IEnumerable<T>> WhenAllParallelAsync<T>(this IEnumerable<Task<T>> tasks, int degree) {
if (tasks.None())
return Enumerable.Empty<T>();
return [];
var results = new List<T>();
foreach (var chunk in tasks.Chunk(degree)) {
var chunkResults = await Task.WhenAll(chunk).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public JsonCustomConverter() { }
public override bool CanConvert(Type typeToConvert) =>
typeToConvert.Required() == typeof(T) || typeToConvert.IsSubclassOf(typeof(T));

#pragma warning disable IDE0072 // Add missing cases
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch {
JsonTokenType.Null => T.Empty,
JsonTokenType.String => reader.GetString().ParseAs<T>(),
_ => throw new NotSupportedException(),
};
#pragma warning restore IDE0072 // Add missing cases

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) =>
writer.Required().WriteStringValue(value.TextualRepresentation);
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Commons/Types/System/PageOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ public record PageOf<T>(IEnumerable<T> Items, ushort Page, byte PageSize, ushort
public PageOf(IEnumerable<T> items, bool lastToFirst) : this(items.Required(), 0, 0, (ushort)(items.SafeAny() ? 1 : 0), lastToFirst) {
}

public static PageOf<T> Empty { get; } = new PageOf<T>(Enumerable.Empty<T>(), 0, 0, 0, false);
public static PageOf<T> Empty { get; } = new PageOf<T>([], 0, 0, 0, false);
}

0 comments on commit b59fb40

Please sign in to comment.