Skip to content

Commit

Permalink
Helper to merge YAMLs
Browse files Browse the repository at this point in the history
  • Loading branch information
Piedone committed Nov 16, 2023
1 parent 3f1f974 commit 805d731
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Lombiq.Tests.UI/SecurityScanning/YamlDocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,33 @@ public static YamlSequenceNode GetJobs(this YamlDocument yamlDocument) =>
/// </summary>
/// <returns><see cref="Task.CompletedTask"/>.</returns>
public static Task CompletedTaskAsync(this YamlDocument yamlDocument) => Task.CompletedTask;

/// <summary>
/// Merge the given <see cref="YamlDocument"/> into the current one.
/// </summary>
/// <param name="overrideDocument">
/// The <see cref="YamlDocument"/> to merge from, which overrides the current one.
/// </param>
/// <returns>The merged <see cref="YamlDocument"/>.</returns>
public static YamlDocument MergeFrom(this YamlDocument baseDocument, YamlDocument overrideDocument)
{
var baseMapping = baseDocument.GetRootNode();
var overrideMapping = overrideDocument.GetRootNode();

foreach (var entry in overrideMapping.Children)
{
if (baseMapping.Children.ContainsKey(entry.Key))
{
// Override existing property.
baseMapping.Children[entry.Key] = entry.Value;
}
else
{
// Add new property.
baseMapping.Children.Add(entry.Key, entry.Value);
}
}

return baseDocument;
}
}
2 changes: 1 addition & 1 deletion Lombiq.Tests.UI/SecurityScanning/YamlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Lombiq.Tests.UI.SecurityScanning;

internal static class YamlHelper
public static class YamlHelper
{
public static YamlDocument LoadDocument(string yamlFilePath)
{
Expand Down

0 comments on commit 805d731

Please sign in to comment.