Skip to content

Commit

Permalink
fix: null check (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
maranmaran authored Jul 12, 2023
1 parent 9868b03 commit 29dd0e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion source/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>2.1.0</Version>
<Version>2.1.1</Version>
<Authors>Marko Urh</Authors>
<Company>Perun</Company>
<Copyright>Copyright (c) 2023 Marko Urh and other authors.</Copyright>
Expand Down
46 changes: 23 additions & 23 deletions source/Perun.Differ/DifferDotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Differ.DotNet
{
[PublicAPI]
[PublicAPI]
public sealed class DifferDotNet
{
/// <summary>
Expand All @@ -21,7 +21,7 @@ public static IEnumerable<Difference> Diff<T>(T left, T right) where T : class
{
var differences = DiffRecursive(
string.Empty,
string.Empty,
string.Empty,
null,
typeof(T),
left,
Expand Down Expand Up @@ -99,16 +99,16 @@ DiffActions actions
// Check for KeepInDiff attribute
if (prop.GetCustomAttribute<KeepInDiffAttribute>() is { } keepAtt)
{
actions |= keepAtt.IgnoreIfNoSiblingOrChildDiff
? DiffActions.KeepOptional
actions |= keepAtt.IgnoreIfNoSiblingOrChildDiff
? DiffActions.KeepOptional
: DiffActions.Keep;
}

// Recurse on sub-objects
DiffRecursive(
fullPath + ".",
customFullPath + ".",
prop,
prop,
prop.PropertyType,
leftObj != null ? prop.GetValue(leftObj) : null,
rightObj != null ? prop.GetValue(rightObj) : null,
Expand All @@ -125,7 +125,7 @@ DiffActions actions
private static bool HandleIterable<T>(
string path,
string customPath,
PropertyInfo prop,
PropertyInfo prop,
Type type,
T leftObj, T rightObj,
DiffCollection diffs,
Expand All @@ -145,25 +145,25 @@ DiffActions actions

// Check for DiffArrayIdPropertyName attribute
PropertyInfo idProperty = null;
if (prop.GetCustomAttribute<DiffCollectionId>() is { } idAttr)
if (prop != null && prop.GetCustomAttribute<DiffCollectionId>() is { } idAttr)
{
idProperty = underlyingType?.GetProperty(idAttr.Name);
}

}

// key based
if (idProperty != null)
{
var sortedL = leftArr.OrderBy(x => idProperty?.GetValue(x)).ToArray();
var sortedR = rightArr.OrderBy(x => idProperty?.GetValue(x)).ToArray();

var sortedL = leftArr.OrderBy(x => idProperty?.GetValue(x)).ToArray();
var sortedR = rightArr.OrderBy(x => idProperty?.GetValue(x)).ToArray();

var l = 0;
var r = 0;
while (l < sortedL.Length || r < sortedR.Length)
{
var curL = l < sortedL.Length ? leftArr[l] : null;
var curR = r < sortedR.Length ? rightArr[r] : null;

var keyL = curL != null ? idProperty.GetValue(curL) : null;
var keyL = curL != null ? idProperty.GetValue(curL) : null;
var keyR = curR != null ? idProperty.GetValue(curR) : null;

if (keyL != keyR)
Expand All @@ -174,7 +174,7 @@ DiffActions actions
DiffRecursive(
path + $"{nextIdx}.",
customPath + $"{nextIdx}.",
prop,
prop,
underlyingType,
curL,
nextR,
Expand All @@ -184,8 +184,8 @@ DiffActions actions

var _ = curL != null ? l++ : r++;
continue;
}

}

DiffRecursive(
path + $"{l}.",
customPath + $"{l}.",
Expand All @@ -195,15 +195,15 @@ DiffActions actions
curR,
diffs,
actions
);
);

l++;
r++;
}
}

return true;
}

}

// index based
for (var i = 0; i < Math.Max(leftArr.Length, rightArr.Length); i++)
{
Expand Down Expand Up @@ -244,7 +244,7 @@ DiffActions actions
path.Substring(0, path.Length - 1),
customPath.Substring(0, customPath.Length - 1),
leftObj,
rightObj
rightObj
);

if (actions.HasFlag(DiffActions.Keep) || actions.HasFlag(DiffActions.KeepOptional))
Expand All @@ -261,8 +261,8 @@ DiffActions actions
if (exitCond)
{
return true;
}

}

diffs.Diffs.Add(diff.FullPath, diff);
return true;
}
Expand Down

0 comments on commit 29dd0e4

Please sign in to comment.