aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-07-17 23:10:48 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-07-17 23:10:48 -0500
commit2092189312e158cf01fc972b407624828d1bcd80 (patch)
treead48d949bff7844e8b50b38ed733330aa8757294
parenta5e40e0f29ae26de8c172fe9d29afae11832e7be (diff)
downloadslskd-2092189312e158cf01fc972b407624828d1bcd80.tar.gz
add DiffWith
-rw-r--r--src/slskd/Common/Extensions.cs46
1 files changed, 42 insertions, 4 deletions
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs
index a7a89a71..eab5b02a 100644
--- a/src/slskd/Common/Extensions.cs
+++ b/src/slskd/Common/Extensions.cs
@@ -19,12 +19,9 @@ namespace slskd
{
using System;
using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using slskd.Validation;
+ using System.Reflection;
/// <summary>
/// Extensions.
@@ -32,6 +29,47 @@ namespace slskd
public static class Extensions
{
/// <summary>
+ /// Deeply compares this object with the specified object and returns a list of properties that are different.
+ /// </summary>
+ /// <param name="left">The left side of the comparison.</param>
+ /// <param name="right">The right side of the comparison.</param>
+ /// <param name="parentFqn">The root path for recursive calls.</param>
+ /// <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())
+ {
+ 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())
+ {
+ 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 (!leftVal.Equals(rightVal))
+ {
+ differences.Add((prop, fqn, leftVal, rightVal));
+ }
+ }
+ else
+ {
+ differences.AddRange(DiffWith(leftVal, rightVal, fqn));
+ }
+ }
+
+ return differences;
+ }
+
+ /// <summary>
/// Returns the directory from the given path, regardless of separator format.
/// </summary>
/// <param name="path">The path.</param>