From b13163e7e0b15005b760e6cda19a3f008ff33fcb Mon Sep 17 00:00:00 2001 From: alejandro Date: Wed, 20 May 2020 12:27:44 +0200 Subject: [PATCH] nulables --- Signum.Engine/Engine/Synchronizer.cs | 6 +-- Signum.Engine/Linq/AliasGenerator.cs | 5 ++- Signum.Engine/Linq/DbExpressions.Sql.cs | 2 +- .../ExpressionVisitor/DbExpressionComparer.cs | 6 +-- Signum.Engine/Schema/ObjectName.cs | 21 ++++++++-- .../DynamicQuery/Tokens/QueryToken.cs | 2 +- Signum.Entities/EnumEntity.cs | 7 +++- Signum.Entities/Lite.cs | 4 +- Signum.Entities/PropertyRoute.cs | 6 ++- .../EntityStructuralEqualityComparer.cs | 9 ++-- .../DataStructures/HashsetComparer.cs | 8 +++- .../DataStructures/LambdaComparer.cs | 42 +++++++++++++++++-- .../ReferenceEqualityComparer.cs | 3 +- .../DataStructures/ScopedDictionary.cs | 3 +- .../ExpressionTrees/ExpressionComparer.cs | 4 +- Signum.Utilities/Reflection/GenericInvoker.cs | 8 +++- 16 files changed, 104 insertions(+), 32 deletions(-) diff --git a/Signum.Engine/Engine/Synchronizer.cs b/Signum.Engine/Engine/Synchronizer.cs index 6a353d8740..0a7875ef19 100644 --- a/Signum.Engine/Engine/Synchronizer.cs +++ b/Signum.Engine/Engine/Synchronizer.cs @@ -27,15 +27,15 @@ public static void Synchronize( if (!oldExists) { - createNew?.Invoke(key, newVal); + createNew?.Invoke(key, newVal!); } else if (!newExists) { - removeOld?.Invoke(key, oldVal); + removeOld?.Invoke(key, oldVal!); } else { - merge?.Invoke(key, newVal, oldVal); + merge?.Invoke(key, newVal!, oldVal!); } } } diff --git a/Signum.Engine/Linq/AliasGenerator.cs b/Signum.Engine/Linq/AliasGenerator.cs index f3d52d3852..93b8d9267d 100644 --- a/Signum.Engine/Linq/AliasGenerator.cs +++ b/Signum.Engine/Linq/AliasGenerator.cs @@ -82,8 +82,11 @@ internal Alias(ObjectName objectName) this.ObjectName = objectName; } - public bool Equals(Alias other) + public bool Equals(Alias? other) { + if (other == null) + return false; + return this.Name == other.Name && object.Equals(this.ObjectName, other.ObjectName); } diff --git a/Signum.Engine/Linq/DbExpressions.Sql.cs b/Signum.Engine/Linq/DbExpressions.Sql.cs index 33065fc1b9..fd060690ed 100644 --- a/Signum.Engine/Linq/DbExpressions.Sql.cs +++ b/Signum.Engine/Linq/DbExpressions.Sql.cs @@ -254,7 +254,7 @@ public override string ToString() } public override bool Equals(object? obj) => obj is ColumnExpression ce && Equals(ce); - public bool Equals(ColumnExpression other) + public bool Equals(ColumnExpression? other) { return other != null && other.Alias == Alias && other.Name == Name; } diff --git a/Signum.Engine/Linq/ExpressionVisitor/DbExpressionComparer.cs b/Signum.Engine/Linq/ExpressionVisitor/DbExpressionComparer.cs index 20f8453ca5..88de4bc541 100644 --- a/Signum.Engine/Linq/ExpressionVisitor/DbExpressionComparer.cs +++ b/Signum.Engine/Linq/ExpressionVisitor/DbExpressionComparer.cs @@ -30,7 +30,7 @@ protected DbExpressionComparer(ScopedDictionary? parameterScope = null, ScopedDictionary? aliasScope = null, bool checkParameterNames = false) + public static bool AreEqual(Expression? a, Expression? b, ScopedDictionary? parameterScope = null, ScopedDictionary? aliasScope = null, bool checkParameterNames = false) { return new DbExpressionComparer(parameterScope, aliasScope, checkParameterNames ).Compare(a, b); } @@ -169,7 +169,7 @@ protected virtual bool CompareAlias(Alias? a, Alias? b) if (aliasMap != null) { - if (aliasMap.TryGetValue(a, out Alias mapped)) + if (aliasMap.TryGetValue(a, out Alias? mapped)) return mapped == b; } return a == b; @@ -566,7 +566,7 @@ public DbExpressionsEqualityComparer(bool checkParameterNames) this.checkParameterNames = checkParameterNames; } - public bool Equals(E x, E y) + public bool Equals(E? x, E? y) { return DbExpressionComparer.AreEqual(x, y, checkParameterNames: this.checkParameterNames); } diff --git a/Signum.Engine/Schema/ObjectName.cs b/Signum.Engine/Schema/ObjectName.cs index 4d4005371c..83512fdfa5 100644 --- a/Signum.Engine/Schema/ObjectName.cs +++ b/Signum.Engine/Schema/ObjectName.cs @@ -43,8 +43,11 @@ public override string ToString() } public override bool Equals(object? obj) => obj is ServerName sn && Equals(sn); - public bool Equals(ServerName other) + public bool Equals(ServerName? other) { + if (other == null) + return false; + return other.Name == Name; } @@ -93,8 +96,11 @@ public override string ToString() public override bool Equals(object? obj) => obj is DatabaseName dn && Equals(dn); - public bool Equals(DatabaseName other) + public bool Equals(DatabaseName? other) { + if (other == null) + return false; + return other.Name == Name && object.Equals(Server, other.Server); } @@ -163,8 +169,12 @@ public override string ToString() } public override bool Equals(object? obj) => obj is SchemaName sn && Equals(sn); - public bool Equals(SchemaName other) + public bool Equals(SchemaName? other) { + + if (other == null) + return false; + return other.Name == Name && object.Equals(Database, other.Database); } @@ -218,8 +228,11 @@ public override string ToString() } public override bool Equals(object? obj) => obj is ObjectName on && Equals(on); - public bool Equals(ObjectName other) + public bool Equals(ObjectName? other) { + if (other == null) + return false; + return other.Name == Name && object.Equals(Schema, other.Schema); } diff --git a/Signum.Entities/DynamicQuery/Tokens/QueryToken.cs b/Signum.Entities/DynamicQuery/Tokens/QueryToken.cs index 653d9fd145..ee78e6b6d1 100644 --- a/Signum.Entities/DynamicQuery/Tokens/QueryToken.cs +++ b/Signum.Entities/DynamicQuery/Tokens/QueryToken.cs @@ -369,7 +369,7 @@ public override bool Equals(object? obj) return obj is QueryToken && obj.GetType() == this.GetType() && Equals((QueryToken)obj); } - public bool Equals(QueryToken other) + public bool Equals(QueryToken? other) { return other != null && other.QueryName.Equals(this.QueryName) && other.FullKey() == this.FullKey(); } diff --git a/Signum.Entities/EnumEntity.cs b/Signum.Entities/EnumEntity.cs index 411dd0b74a..460b5ba5b8 100644 --- a/Signum.Entities/EnumEntity.cs +++ b/Signum.Entities/EnumEntity.cs @@ -49,9 +49,12 @@ public override string ToString() return Enum.IsDefined(typeof(T), en) ? en.ToString() : (this.toStr ?? en.ToString()); //for aux sync } - public bool Equals(EnumEntity other) + public bool Equals(EnumEntity? other) { - return EqualityComparer.Default.Equals(ToEnum(), other.ToEnum()); + if (other == null) + return false; + + return EqualityComparer.Default.Equals(ToEnum(), other!.ToEnum()); } public static implicit operator EnumEntity(T enumerable) diff --git a/Signum.Entities/Lite.cs b/Signum.Entities/Lite.cs index 21e9bbf0b8..b03fbe1b64 100644 --- a/Signum.Entities/Lite.cs +++ b/Signum.Entities/Lite.cs @@ -217,9 +217,9 @@ public string KeyLong() return "{0};{1};{2}".FormatWith(TypeEntity.GetCleanName(this.EntityType), this.Id, this.ToString()); } - public int CompareTo(Lite other) + public int CompareTo(Lite? other) { - return ToString()!.CompareTo(other.ToString()); + return ToString()!.CompareTo(other?.ToString()); } public int CompareTo(object? obj) diff --git a/Signum.Entities/PropertyRoute.cs b/Signum.Entities/PropertyRoute.cs index adc8a5303b..1b7038411a 100644 --- a/Signum.Entities/PropertyRoute.cs +++ b/Signum.Entities/PropertyRoute.cs @@ -467,8 +467,12 @@ public override int GetHashCode() } public override bool Equals(object? obj) => obj is PropertyRoute pr && Equals(pr); - public bool Equals(PropertyRoute other) + public bool Equals(PropertyRoute? other) { + + if (other==null) + return false; + if (other.PropertyRouteType != this.PropertyRouteType) return false; diff --git a/Signum.Entities/Reflection/EntityStructuralEqualityComparer.cs b/Signum.Entities/Reflection/EntityStructuralEqualityComparer.cs index bdaa7b330e..9ab443cc77 100644 --- a/Signum.Entities/Reflection/EntityStructuralEqualityComparer.cs +++ b/Signum.Entities/Reflection/EntityStructuralEqualityComparer.cs @@ -5,6 +5,7 @@ using System.Collections; using Signum.Utilities.Reflection; using Signum.Utilities; +using System.Diagnostics.CodeAnalysis; namespace Signum.Entities.Reflection { @@ -42,7 +43,7 @@ public void Complete(IEqualityComparerResolver resolver, PropertyInfo? pi) MixinDeclarations.GetMixinDeclarations(typeof(T)).ToDictionary(t => t, t => resolver.GetEqualityComparer(t, null)); } - public override bool Equals(T x, T y) + public override bool Equals(T? x, T? y) { if ((x == null) != (y == null)) return false; @@ -115,7 +116,7 @@ public void Complete(IEqualityComparerResolver resolver, PropertyInfo? pi) comparer: resolver.GetEqualityComparer(((PropertyInfo)a.MemberInfo).PropertyType, (PropertyInfo)a.MemberInfo))); } - public override bool Equals(T x, T y) + public override bool Equals([AllowNull] T x, [AllowNull] T y) { if ((x == null) != (y == null)) return false; @@ -178,7 +179,7 @@ public void Complete(IEqualityComparerResolver resolver, PropertyInfo? pi) ElementComparer = (IEqualityComparer)resolver.GetEqualityComparer(typeof(T), pi); } - public override bool Equals(IList x, IList y) + public override bool Equals(IList? x, IList? y) { if ((x == null) != (y == null)) return false; @@ -224,7 +225,7 @@ public void Complete(IEqualityComparerResolver resolver, PropertyInfo? pi) ElementComparer = (IEqualityComparer)resolver.GetEqualityComparer(typeof(T), pi); } - public override bool Equals(IList mx, IList my) + public override bool Equals(IList? mx, IList? my) { if ((mx == null) != (my == null)) return false; diff --git a/Signum.Utilities/DataStructures/HashsetComparer.cs b/Signum.Utilities/DataStructures/HashsetComparer.cs index 0c1d6707b5..ed176710f0 100644 --- a/Signum.Utilities/DataStructures/HashsetComparer.cs +++ b/Signum.Utilities/DataStructures/HashsetComparer.cs @@ -8,8 +8,14 @@ namespace Signum.Utilities.DataStructures [Serializable] public class HashSetComparer : IEqualityComparer>, IEqualityComparer { - public bool Equals(HashSet x, HashSet y) + public bool Equals(HashSet? x, HashSet? y) { + if (x == null && y == null) + return true; + + if (x == null || y == null) + return false; + return x.SetEquals(y); } diff --git a/Signum.Utilities/DataStructures/LambdaComparer.cs b/Signum.Utilities/DataStructures/LambdaComparer.cs index ff106e9108..cf5dd2a181 100644 --- a/Signum.Utilities/DataStructures/LambdaComparer.cs +++ b/Signum.Utilities/DataStructures/LambdaComparer.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections; +using System.Diagnostics.CodeAnalysis; namespace Signum.Utilities.DataStructures { @@ -27,13 +28,30 @@ public LambdaComparer(Func func, IEqualityComparer? equalityComparer = this.comparer = comparer ?? Comparer.Default; } - public int Compare(T x, T y) + public int Compare([AllowNull] T x, [AllowNull] T y) { + if (x == null && y == null) + return 0; + + if (x == null ) + return -1; + + if (y == null) + return 1; + + return comparer.Compare(func(x), func(y)); } - public bool Equals(T x, T y) + public bool Equals([AllowNull] T x, [AllowNull] T y) { + if (x == null && y == null) + return true; + + if (x == null || y == null) + return false; + + return equalityComparer.Equals(func(x), func(y)); } @@ -86,8 +104,17 @@ public CombineComparer(IComparer comparer1, IComparer comparer2) this.comparer2 = comparer2; } - public int Compare(T x, T y) + public int Compare([AllowNull] T x, [AllowNull] T y) { + if (x == null && y == null) + return 0; + + if (x == null) + return -1; + + if (y == null) + return 1; + return comparer1.Compare(x, y).DefaultToNull() ?? comparer2.Compare(x, y); } @@ -113,8 +140,15 @@ public CombineEqualityComparer(IEqualityComparer comparer1, IEqualityComparer this.comparer2 = comparer2; } - public bool Equals(T x, T y) + public bool Equals([AllowNull] T x, [AllowNull] T y) { + + if (x == null && y == null) + return true; + + if (x == null || y == null) + return false; + return comparer1.Equals(x, y) && comparer2.Equals(x,y); } diff --git a/Signum.Utilities/DataStructures/ReferenceEqualityComparer.cs b/Signum.Utilities/DataStructures/ReferenceEqualityComparer.cs index 1903a35511..fe7ec3807e 100644 --- a/Signum.Utilities/DataStructures/ReferenceEqualityComparer.cs +++ b/Signum.Utilities/DataStructures/ReferenceEqualityComparer.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Collections; +using System.Diagnostics.CodeAnalysis; namespace Signum.Utilities.DataStructures { @@ -23,7 +24,7 @@ public int GetHashCode(T item) return RuntimeHelpers.GetHashCode(item); } - public bool Equals(T x, T y) + public bool Equals([AllowNull] T x, [AllowNull] T y) { return object.ReferenceEquals(x, y); } diff --git a/Signum.Utilities/DataStructures/ScopedDictionary.cs b/Signum.Utilities/DataStructures/ScopedDictionary.cs index c4931fc348..1a569db64f 100644 --- a/Signum.Utilities/DataStructures/ScopedDictionary.cs +++ b/Signum.Utilities/DataStructures/ScopedDictionary.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace Signum.Utilities.DataStructures { @@ -30,7 +31,7 @@ public void Add(TKey key, TValue value) this.map.Add(key, value); } - public bool TryGetValue(TKey key, out TValue value) + public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) { for (ScopedDictionary? scope = this; scope != null; scope = scope.previous) { diff --git a/Signum.Utilities/ExpressionTrees/ExpressionComparer.cs b/Signum.Utilities/ExpressionTrees/ExpressionComparer.cs index c77f53b666..6d1616b3bb 100644 --- a/Signum.Utilities/ExpressionTrees/ExpressionComparer.cs +++ b/Signum.Utilities/ExpressionTrees/ExpressionComparer.cs @@ -149,7 +149,7 @@ protected virtual bool CompareParameter(ParameterExpression a, ParameterExpressi { if (parameterScope != null) { - if (parameterScope.TryGetValue(a, out ParameterExpression mapped)) + if (parameterScope.TryGetValue(a, out ParameterExpression? mapped)) return mapped == b; } return a == b; @@ -322,7 +322,7 @@ public ExpressionsEqualityComparer(bool checkParameterNames) this.checkParameterNames = checkParameterNames; } - public bool Equals(E x, E y) + public bool Equals(E? x, E? y) { return ExpressionComparer.AreEqual(x, y, parameterScope: null, checkParameterNames: checkParameterNames); } diff --git a/Signum.Utilities/Reflection/GenericInvoker.cs b/Signum.Utilities/Reflection/GenericInvoker.cs index 1a17a968a6..8feacc5dfb 100644 --- a/Signum.Utilities/Reflection/GenericInvoker.cs +++ b/Signum.Utilities/Reflection/GenericInvoker.cs @@ -36,8 +36,14 @@ public T GetInvoker(params Type[] types) class TypeArrayEqualityComparer : IEqualityComparer { public static readonly TypeArrayEqualityComparer Instance = new TypeArrayEqualityComparer(); - public bool Equals(Type[] x, Type[] y) + public bool Equals(Type[]? x, Type[]? y) { + if (x == null && y == null) + return true; + + if (x == null || y == null) + return false; + for (int i = 0; i < x.Length; i++) { if (!x[i].Equals(y[i]))