Skip to content

Commit

Permalink
add support to diff one objects that may be null references
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuck committed Sep 19, 2023
1 parent f9a2b49 commit ae9ae91
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/YuckQi.Domain.Services/YuckQi.Domain.Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>6.4.1</Version>
<Version>6.4.2</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library for bootstrapping a domain services project.</Description>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>6.4.1</Version>
<Version>6.4.2</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library providing domain validation fundamentals.</Description>
<Nullable>enable</Nullable>
Expand Down
15 changes: 13 additions & 2 deletions src/YuckQi.Domain/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ public static class ObjectExtensions
public static Dictionary<PropertyInfo, (Object?, Object?)> GetDifferencesByProperty<T>(this T a, T b)
{
var properties = GetObjectProperties(typeof(T));
var values = properties.Select(t => new KeyValuePair<PropertyInfo, (Object?, Object?)>(t, (t.GetValue(a), t.GetValue(b))));
var values = properties.Select(t =>
{
var valueA = a != null ? t.GetValue(a) : null;
var valueB = b != null ? t.GetValue(b) : null;
return new KeyValuePair<PropertyInfo, (Object?, Object?)>(t, (valueA, valueB));
});
var result = values.Where(t => ! Equals(t.Value.Item1, t.Value.Item2)).ToDictionary(t => t.Key, t => t.Value);

return result;
Expand All @@ -22,7 +28,12 @@ public static class ObjectExtensions
public static HashSet<PropertyInfo> GetNonNullProperties<T>(this T a)
{
var properties = GetObjectProperties(typeof(T));
var values = properties.Select(t => new KeyValuePair<PropertyInfo, Object?>(t, t.GetValue(a)));
var values = properties.Select(t =>
{
var valueA = a != null ? t.GetValue(a) : null;
return new KeyValuePair<PropertyInfo, Object?>(t, valueA);
});
var result = values.Where(t => t.Value != null).Select(t => t.Key).ToHashSet();

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/YuckQi.Domain/YuckQi.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>6.4.1</Version>
<Version>6.4.2</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library for bootstrapping a domain model project.</Description>
<Nullable>enable</Nullable>
Expand Down
29 changes: 19 additions & 10 deletions test/YuckQi.Domain.UnitTests/Extensions/ObjectExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ public class ObjectExtensionTests
[SetUp]
public void Setup() { }

[Test]
public void Object_WithSomeNonNullProperties_HasExpectedValue()
{
var a = new { a = new Int32?(), b = 1234, c = "hello", d = new Int64?(9999) };
var result = a.GetNonNullProperties();

Assert.That(result, Has.Exactly(3).Items);
Assert.That(result.Select(t => t.Name), Does.Not.Contain("a"));
}

[Test]
public void Objects_WhereOneIsNull_HaveAllPropertyDifferences()
{
var a = new { a = 123, b = "test" };
var result = a.GetDifferencesByProperty(default);

Assert.That(result, Has.Exactly(2).Items);
}

[Test]
public void Objects_WithDifferentStringValues_HaveOneDifference()
{
Expand All @@ -22,14 +41,4 @@ public void Objects_WithDifferentStringValues_HaveOneDifference()
Assert.That(result.Single().Value.Item1, Is.EqualTo("test"));
Assert.That(result.Single().Value.Item2, Is.EqualTo("hello"));
}

[Test]
public void Object_WithSomeNonNullProperties_HasExpectedValue()
{
var a = new { a = new Int32?(), b = 1234, c = "hello", d = new Int64?(9999) };
var result = a.GetNonNullProperties();

Assert.That(result, Has.Exactly(3).Items);
Assert.That(result.Select(t => t.Name), Does.Not.Contain("a"));
}
}

0 comments on commit ae9ae91

Please sign in to comment.