Skip to content

Commit

Permalink
fix: use maps for ids (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
maranmaran authored Jul 19, 2023
1 parent 83f53f3 commit ff27f4a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 42 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.4</Version>
<Version>2.1.5</Version>
<Authors>Marko Urh</Authors>
<Company>Perun</Company>
<Copyright>Copyright (c) 2023 Marko Urh and other authors.</Copyright>
Expand Down
11 changes: 8 additions & 3 deletions source/Perun.Differ.Tests/DifferTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using AutoBogus;
Expand Down Expand Up @@ -293,7 +294,8 @@ public void ComplexNestedIterable_ListIdDefined_DeleteFirstItem_DeleteDiffRather
[Fact]
public void ComplexNestedIterable_ListIdDefined_MutateArray_DeleteDiffRatherThanUpdateDiff()
{
var faker = new AutoFaker<ComplexWithId>();
var faker = new AutoFaker<ComplexWithId>()
.RuleForType(typeof(string), f => Guid.NewGuid().ToString());

var left = new ComplexIterableWithId
{
Expand All @@ -317,8 +319,11 @@ public void ComplexNestedIterable_ListIdDefined_MutateArray_DeleteDiffRatherThan
[Fact]
public void ComplexDeeplyNestedIterableWithId()
{
var faker = new AutoFaker<ComplexDeeplyNestedIterableWithId>();
var faker2 = new AutoFaker<ComplexDeeplyNestedIterableWithId.Level1>();
var faker = new AutoFaker<ComplexDeeplyNestedIterableWithId>()
.RuleForType(typeof(string), f => Guid.NewGuid().ToString());

var faker2 = new AutoFaker<ComplexDeeplyNestedIterableWithId.Level1>()
.RuleForType(typeof(string), f => Guid.NewGuid().ToString());

var left = faker.Generate();
var right = JsonSerializer.Deserialize<ComplexDeeplyNestedIterableWithId>(JsonSerializer.Serialize(left));
Expand Down
58 changes: 20 additions & 38 deletions source/Perun.Differ/DifferDotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,53 +204,35 @@ DiffActions actions
// key based
if (idProperty != null)
{
var sortedL = leftArr.OrderBy(x => idProperty?.GetValue(x)).ToArray();
var sortedR = rightArr.OrderBy(x => idProperty?.GetValue(x)).ToArray();
var leftMap = leftArr.ToDictionary(idProperty.GetValue);
var rightMap = rightArr.ToDictionary(idProperty.GetValue);

var mapPairs = leftMap.Concat(rightMap).GroupBy(x => x.Key)
.ToDictionary(
x => x.Key,
x => (
Left: leftMap.ContainsKey(x.Key) ? leftMap[x.Key] : null,
Right: rightMap.ContainsKey(x.Key) ? rightMap[x.Key] : null
)
);

var l = 0;
var r = 0;
while (l < sortedL.Length || r < sortedR.Length)
for (var i = 0; i < mapPairs.Count; i++)
{
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 keyR = curR != null ? idProperty.GetValue(curR) : null;

if (!Equals(keyL, keyR))
{
var nextIdx = curL != null ? l : r;
var nextR = curL != null ? null : curR;

DiffRecursive(
path + $"{nextIdx}.",
customPath + $"{nextIdx}.",
prop,
underlyingType,
curL,
nextR,
diffs,
actions
);

var _ = curL != null ? l++ : r++;
continue;
}
var pair = mapPairs.ElementAt(i);
var curL = pair.Value.Left;
var curR = pair.Value.Right;

DiffRecursive(
path + $"{l}.",
customPath + $"{l}.",
path + $"{i}.",
customPath + $"{i}.",
prop,
underlyingType,
curL,
curL,
curR,
diffs,
actions
);

l++;
r++;
}
);
}

return true;
}
Expand Down

0 comments on commit ff27f4a

Please sign in to comment.