From 29dd0e4953e0c8dc4f7a01117e16f94991eeaa2d Mon Sep 17 00:00:00 2001 From: Marko Urh Date: Wed, 12 Jul 2023 15:03:40 +0200 Subject: [PATCH] fix: null check (#9) --- source/Directory.Build.props | 2 +- source/Perun.Differ/DifferDotNet.cs | 46 ++++++++++++++--------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/source/Directory.Build.props b/source/Directory.Build.props index 664f411..8fb23bb 100644 --- a/source/Directory.Build.props +++ b/source/Directory.Build.props @@ -1,7 +1,7 @@ - 2.1.0 + 2.1.1 Marko Urh Perun Copyright (c) 2023 Marko Urh and other authors. diff --git a/source/Perun.Differ/DifferDotNet.cs b/source/Perun.Differ/DifferDotNet.cs index 46feb5f..078a637 100644 --- a/source/Perun.Differ/DifferDotNet.cs +++ b/source/Perun.Differ/DifferDotNet.cs @@ -7,7 +7,7 @@ namespace Differ.DotNet { - [PublicAPI] + [PublicAPI] public sealed class DifferDotNet { /// @@ -21,7 +21,7 @@ public static IEnumerable Diff(T left, T right) where T : class { var differences = DiffRecursive( string.Empty, - string.Empty, + string.Empty, null, typeof(T), left, @@ -99,8 +99,8 @@ DiffActions actions // Check for KeepInDiff attribute if (prop.GetCustomAttribute() is { } keepAtt) { - actions |= keepAtt.IgnoreIfNoSiblingOrChildDiff - ? DiffActions.KeepOptional + actions |= keepAtt.IgnoreIfNoSiblingOrChildDiff + ? DiffActions.KeepOptional : DiffActions.Keep; } @@ -108,7 +108,7 @@ DiffActions actions DiffRecursive( fullPath + ".", customFullPath + ".", - prop, + prop, prop.PropertyType, leftObj != null ? prop.GetValue(leftObj) : null, rightObj != null ? prop.GetValue(rightObj) : null, @@ -125,7 +125,7 @@ DiffActions actions private static bool HandleIterable( string path, string customPath, - PropertyInfo prop, + PropertyInfo prop, Type type, T leftObj, T rightObj, DiffCollection diffs, @@ -145,17 +145,17 @@ DiffActions actions // Check for DiffArrayIdPropertyName attribute PropertyInfo idProperty = null; - if (prop.GetCustomAttribute() is { } idAttr) + if (prop != null && prop.GetCustomAttribute() 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) @@ -163,7 +163,7 @@ DiffActions actions 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) @@ -174,7 +174,7 @@ DiffActions actions DiffRecursive( path + $"{nextIdx}.", customPath + $"{nextIdx}.", - prop, + prop, underlyingType, curL, nextR, @@ -184,8 +184,8 @@ DiffActions actions var _ = curL != null ? l++ : r++; continue; - } - + } + DiffRecursive( path + $"{l}.", customPath + $"{l}.", @@ -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++) { @@ -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)) @@ -261,8 +261,8 @@ DiffActions actions if (exitCond) { return true; - } - + } + diffs.Diffs.Add(diff.FullPath, diff); return true; }