From 59f87bb49ba88ab081939d6c05219bade0e7bf06 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 17 Mar 2020 17:50:43 +0100 Subject: [PATCH] Nullable fixes. --- .../Apps/Indexes/AppsIndex.cs | 4 +-- .../Contents/GraphQL/Types/Extensions.cs | 2 +- .../Contents/Queries/QueryExecutionContext.cs | 4 +-- .../Rules/Guards/RuleTriggerValidator.cs | 2 +- .../Rules/Indexes/RulesIndex.cs | 2 +- .../Schemas/Indexes/SchemasIndex.cs | 2 +- .../EventSourcing/CosmosDbSubscription.cs | 28 +++++++++---------- .../CollectionExtensions.cs | 21 ++++++++++++-- .../Queries/Optimizer.cs | 2 +- .../Queries/TransformVisitor.cs | 2 +- backend/tools/Migrate_01/MigrationPath.cs | 3 +- 11 files changed, 44 insertions(+), 28 deletions(-) diff --git a/backend/src/Squidex.Domain.Apps.Entities/Apps/Indexes/AppsIndex.cs b/backend/src/Squidex.Domain.Apps.Entities/Apps/Indexes/AppsIndex.cs index 688d2204f..3474524d7 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Apps/Indexes/AppsIndex.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Apps/Indexes/AppsIndex.cs @@ -80,7 +80,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes await Task.WhenAll(ids .Select(GetAppAsync)); - return apps.Where(x => x != null).ToList(); + return apps.NotNull().ToList(); } } @@ -98,7 +98,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes .SelectMany(x => x).Distinct() .Select(GetAppAsync)); - return apps.Where(x => x != null).ToList(); + return apps.NotNull().ToList(); } } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Extensions.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Extensions.cs index ba0bd5d59..6d0e5b1e3 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Extensions.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Extensions.cs @@ -44,7 +44,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types { var contents = await Task.WhenAll(keys.Select(dataLoader.LoadAsync)); - return contents.Where(x => x != null).ToList(); + return contents.NotNull().ToList(); } } } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/QueryExecutionContext.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/QueryExecutionContext.cs index 22e9398dd..01589a7f0 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/QueryExecutionContext.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/QueryExecutionContext.cs @@ -113,7 +113,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Queries } } - return ids.Select(cachedAssets.GetOrDefault).Where(x => x != null).ToList(); + return ids.Select(cachedAssets.GetOrDefault).NotNull().ToList(); } public virtual async Task> GetReferencedContentsAsync(ICollection ids) @@ -132,7 +132,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Queries } } - return ids.Select(cachedContents.GetOrDefault).Where(x => x != null).ToList(); + return ids.Select(cachedContents.GetOrDefault).NotNull().ToList(); } } } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Rules/Guards/RuleTriggerValidator.cs b/backend/src/Squidex.Domain.Apps.Entities/Rules/Guards/RuleTriggerValidator.cs index 7206d002f..7af78b81b 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Rules/Guards/RuleTriggerValidator.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Rules/Guards/RuleTriggerValidator.cs @@ -90,7 +90,7 @@ namespace Squidex.Domain.Apps.Entities.Rules.Guards var checkErrors = await Task.WhenAll(tasks); - errors.AddRange(checkErrors.Where(x => x != null)); + errors.AddRange(checkErrors.NotNull()); } return errors; diff --git a/backend/src/Squidex.Domain.Apps.Entities/Rules/Indexes/RulesIndex.cs b/backend/src/Squidex.Domain.Apps.Entities/Rules/Indexes/RulesIndex.cs index 42c240f2a..11f7b5dff 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Rules/Indexes/RulesIndex.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Rules/Indexes/RulesIndex.cs @@ -43,7 +43,7 @@ namespace Squidex.Domain.Apps.Entities.Rules.Indexes await Task.WhenAll( ids.Select(GetRuleAsync)); - return rules.Where(x => x != null).ToList(); + return rules.NotNull().ToList(); } } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Schemas/Indexes/SchemasIndex.cs b/backend/src/Squidex.Domain.Apps.Entities/Schemas/Indexes/SchemasIndex.cs index f440674ea..2b374b1d9 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Schemas/Indexes/SchemasIndex.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Schemas/Indexes/SchemasIndex.cs @@ -44,7 +44,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes await Task.WhenAll( ids.Select(id => GetSchemaAsync(appId, id, allowDeleted))); - return schemas.Where(x => x != null).ToList(); + return schemas.NotNull().ToList(); } } diff --git a/backend/src/Squidex.Infrastructure.Azure/EventSourcing/CosmosDbSubscription.cs b/backend/src/Squidex.Infrastructure.Azure/EventSourcing/CosmosDbSubscription.cs index 407d526d6..bb0cc23f7 100644 --- a/backend/src/Squidex.Infrastructure.Azure/EventSourcing/CosmosDbSubscription.cs +++ b/backend/src/Squidex.Infrastructure.Azure/EventSourcing/CosmosDbSubscription.cs @@ -56,22 +56,10 @@ namespace Squidex.Infrastructure.EventSourcing { try { - Collection CreateCollection(string name) - { - var collection = new Collection(); - - collection.CollectionName = name; - collection.DatabaseName = store.DatabaseId; - collection.MasterKey = store.MasterKey; - collection.Uri = store.ServiceUri; - - return collection; - } - var processor = await new Builder() - .WithFeedCollection(CreateCollection(Constants.Collection)) - .WithLeaseCollection(CreateCollection(Constants.LeaseCollection)) + .WithFeedCollection(CreateCollection(store, Constants.Collection)) + .WithLeaseCollection(CreateCollection(store, Constants.LeaseCollection)) .WithHostName(hostName) .WithProcessorOptions(new Options { StartFromBeginning = fromBeginning, LeasePrefix = hostName }) .WithObserverFactory(this) @@ -88,6 +76,18 @@ namespace Squidex.Infrastructure.EventSourcing }); } + private static Collection CreateCollection(CosmosDbEventStore store, string name) + { + var collection = new Collection(); + + collection.CollectionName = name; + collection.DatabaseName = store.DatabaseId; + collection.MasterKey = store.MasterKey; + collection.Uri = store.ServiceUri; + + return collection; + } + public IChangeFeedObserver CreateObserver() { return this; diff --git a/backend/src/Squidex.Infrastructure/CollectionExtensions.cs b/backend/src/Squidex.Infrastructure/CollectionExtensions.cs index 622abf576..6f4c32193 100644 --- a/backend/src/Squidex.Infrastructure/CollectionExtensions.cs +++ b/backend/src/Squidex.Infrastructure/CollectionExtensions.cs @@ -30,7 +30,7 @@ namespace Squidex.Infrastructure public static IEnumerable SortList(this IEnumerable input, Func idProvider, IReadOnlyList ids) where T : class { - return ids.Select(id => input.FirstOrDefault(x => Equals(idProvider(x), id))).Where(x => x != null); + return ids.Select(id => input.FirstOrDefault(x => Equals(idProvider(x), id))).NotNull(); } public static IEnumerable Duplicates(this IEnumerable input) @@ -80,6 +80,11 @@ namespace Squidex.Infrastructure return source ?? Enumerable.Empty(); } + public static IEnumerable NotNull(this IEnumerable source) where T : class + { + return source.Where(x => x != null)!; + } + public static IEnumerable Concat(this IEnumerable source, T value) { return source.Concat(Enumerable.Repeat(value, 1)); @@ -261,7 +266,7 @@ namespace Squidex.Infrastructure } } - public sealed class KeyValuePairComparer : IEqualityComparer> + public sealed class KeyValuePairComparer : IEqualityComparer> where TKey : notnull { private readonly IEqualityComparer keyComparer; private readonly IEqualityComparer valueComparer; @@ -279,7 +284,17 @@ namespace Squidex.Infrastructure public int GetHashCode(KeyValuePair obj) { - return keyComparer.GetHashCode(obj.Key) ^ valueComparer.GetHashCode(obj.Value); + return keyComparer.GetHashCode(obj.Key) ^ ValueHashCode(obj); + } + + private int ValueHashCode(KeyValuePair obj) + { + if (Equals(obj.Value, null)) + { + return 0; + } + + return valueComparer.GetHashCode(obj.Value); } } } diff --git a/backend/src/Squidex.Infrastructure/Queries/Optimizer.cs b/backend/src/Squidex.Infrastructure/Queries/Optimizer.cs index 1514135a4..23bfd513d 100644 --- a/backend/src/Squidex.Infrastructure/Queries/Optimizer.cs +++ b/backend/src/Squidex.Infrastructure/Queries/Optimizer.cs @@ -24,7 +24,7 @@ namespace Squidex.Infrastructure.Queries public override FilterNode? Visit(LogicalFilter nodeIn) { - var pruned = nodeIn.Filters.Select(x => x.Accept(this)!).Where(x => x != null).ToList(); + var pruned = nodeIn.Filters.Select(x => x.Accept(this)!).NotNull().ToList(); if (pruned.Count == 1) { diff --git a/backend/src/Squidex.Infrastructure/Queries/TransformVisitor.cs b/backend/src/Squidex.Infrastructure/Queries/TransformVisitor.cs index 8d6f9b311..6ac548db6 100644 --- a/backend/src/Squidex.Infrastructure/Queries/TransformVisitor.cs +++ b/backend/src/Squidex.Infrastructure/Queries/TransformVisitor.cs @@ -18,7 +18,7 @@ namespace Squidex.Infrastructure.Queries public override FilterNode? Visit(LogicalFilter nodeIn) { - var inner = nodeIn.Filters.Select(x => x.Accept(this)!).Where(x => x != null).ToList(); + var inner = nodeIn.Filters.Select(x => x.Accept(this)!).NotNull().ToList(); return new LogicalFilter(nodeIn.Type, inner); } diff --git a/backend/tools/Migrate_01/MigrationPath.cs b/backend/tools/Migrate_01/MigrationPath.cs index 0702df37f..c062df194 100644 --- a/backend/tools/Migrate_01/MigrationPath.cs +++ b/backend/tools/Migrate_01/MigrationPath.cs @@ -11,6 +11,7 @@ using System.Linq; using Microsoft.Extensions.DependencyInjection; using Migrate_01.Migrations; using Migrate_01.Migrations.MongoDb; +using Squidex.Infrastructure; using Squidex.Infrastructure.Migrations; namespace Migrate_01 @@ -32,7 +33,7 @@ namespace Migrate_01 return (CurrentVersion, null); } - var migrations = ResolveMigrators(version).Where(x => x != null).ToList(); + var migrations = ResolveMigrators(version).NotNull().ToList(); return (CurrentVersion, migrations); }