aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-07-18 17:41:45 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-07-18 17:41:45 -0500
commitaa496feb60f34e7fab5d16314b2852dd640e998a (patch)
treebd3f8d1c5dd7dc304454e34c989c5a8986b59ee8 /src
parent2092189312e158cf01fc972b407624828d1bcd80 (diff)
downloadslskd-aa496feb60f34e7fab5d16314b2852dd640e998a.tar.gz
make DiffWith nullable friendly
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Extensions.cs12
1 files changed, 5 insertions, 7 deletions
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs
index eab5b02a..3b52c650 100644
--- a/src/slskd/Common/Extensions.cs
+++ b/src/slskd/Common/Extensions.cs
@@ -37,25 +37,23 @@ namespace slskd
/// <returns>A list of differences between the two objects.</returns>
public static IEnumerable<(PropertyInfo Property, string FQN, object Left, object Right)> DiffWith(this object left, object right, string parentFqn = null)
{
- if (left.GetType() != right.GetType())
+ if (left?.GetType() != right?.GetType())
{
- throw new InvalidCastException($"Unable to diff types {left.GetType()} and {right.GetType()}");
+ throw new InvalidCastException($"Unable to diff types {left?.GetType()} and {right?.GetType()}");
}
var differences = new List<(PropertyInfo Property, string FQN, object Left, object Right)>();
- foreach (var prop in left.GetType().GetProperties())
+ foreach (var prop in left?.GetType().GetProperties())
{
var leftVal = prop.GetValue(left);
var rightVal = prop.GetValue(right);
var propType = prop.PropertyType;
var fqn = string.IsNullOrEmpty(parentFqn) ? prop.Name : string.Join(".", parentFqn, prop.Name);
- var quasiPrimitives = new[] { typeof(string), typeof(decimal) };
-
- if (propType.IsPrimitive || quasiPrimitives.Contains(propType))
+ if (propType.IsPrimitive || Nullable.GetUnderlyingType(propType) != null || new[] { typeof(string), typeof(decimal) }.Contains(propType))
{
- if (!leftVal.Equals(rightVal))
+ if (!Equals(leftVal, rightVal))
{
differences.Add((prop, fqn, leftVal, rightVal));
}