Skip to content

Commit

Permalink
Fix #564 - sort fails when importing renamed properties
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJump committed Nov 7, 2023
1 parent c5db3b5 commit 8b0e880
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions uSync.BackOffice/SyncHandlers/SyncHandlerContainerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,26 +228,41 @@ protected override IList<LeveledFile> GetLevelOrderedFiles(string folder, IList<

var nodes = new Dictionary<Guid, LeveledFile>();
var graph = new List<GraphEdge<Guid>>();
var renames = new List<LeveledFile>();

foreach (var file in files)
{
var node = LoadNode(file);
if (node == null) continue;

var key = node.GetKey();
nodes.Add(key, new LeveledFile

var leveledFile = new LeveledFile
{
Alias = node.GetAlias(),
File = file,
Level = node.GetLevel(),
});
};

if (node.IsEmptyItem()) {
renames.Add(leveledFile);
continue;
}

// you can have circular dependencies in structure :(
// graph.AddRange(GetStructure(node).Select(x => GraphEdge.Create(key, x)));
if (nodes.TryAdd(key, leveledFile))
{
graph.AddRange(GetCompositions(node).Select(x => GraphEdge.Create(key, x)));
}
else
{
logger.LogWarning("Failed to add key when sorting, possible duplicate? {key} {alias} {file} old sort method will be used (may require additional pass)",
key, node.GetAlias(), file);

graph.AddRange(GetCompositions(node).Select(x => GraphEdge.Create(key, x)));
// fall back to the old way.
return base.GetLevelOrderedFiles(folder, actions);
}
}

var cleanGraph = graph.Where(x => x.Node != x.Edge).ToList();
var sortedList = nodes.Keys.TopologicalSort(cleanGraph);

Expand All @@ -260,8 +275,11 @@ protected override IList<LeveledFile> GetLevelOrderedFiles(string folder, IList<
if (nodes.ContainsKey(key))
result.Add(nodes[key]);
}
return result;

if (renames.Any())
result.AddRange(renames);

return result;
}

public IEnumerable<Guid> GetGraphIds(XElement node)

Check warning on line 285 in uSync.BackOffice/SyncHandlers/SyncHandlerContainerBase.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SyncHandlerContainerBase<TObject, TService>.GetGraphIds(XElement)'

Check warning on line 285 in uSync.BackOffice/SyncHandlers/SyncHandlerContainerBase.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SyncHandlerContainerBase<TObject, TService>.GetGraphIds(XElement)'
Expand Down

0 comments on commit 8b0e880

Please sign in to comment.