Skip to content

Commit 448e43d

Browse files
committed
Add MergeWith dictionary extension
1 parent e29b8ab commit 448e43d

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Collections.Generic;
2+
using SabreTools.IO.Extensions;
3+
using Xunit;
4+
5+
namespace SabreTools.IO.Test.Extensions
6+
{
7+
public class DictionaryExtensionsTests
8+
{
9+
#region MergeWith
10+
11+
[Fact]
12+
public void MergeWith_EmptySource_EmptyOther_Empty()
13+
{
14+
Dictionary<string, List<string>> source = [];
15+
Dictionary<string, List<string>> other = [];
16+
17+
source.MergeWith(other);
18+
Assert.Empty(source);
19+
}
20+
21+
[Fact]
22+
public void MergeWith_EmptySource_EmptyKeyOther_Empty()
23+
{
24+
Dictionary<string, List<string>> source = [];
25+
Dictionary<string, List<string>> other = [];
26+
other.Add("key", []);
27+
28+
source.MergeWith(other);
29+
Assert.Empty(source);
30+
}
31+
32+
[Fact]
33+
public void MergeWith_EmptySource_FilledOther_Filled()
34+
{
35+
Dictionary<string, List<string>> source = [];
36+
Dictionary<string, List<string>> other = [];
37+
other.Add("key", ["value"]);
38+
39+
source.MergeWith(other);
40+
string key = Assert.Single(source.Keys);
41+
Assert.Equal("key", key);
42+
List<string> actual = source[key];
43+
string value = Assert.Single(actual);
44+
Assert.Equal("value", value);
45+
}
46+
47+
[Fact]
48+
public void MergeWith_FilledSource_EmptyOther_Filled()
49+
{
50+
Dictionary<string, List<string>> source = [];
51+
source.Add("key", ["value"]);
52+
Dictionary<string, List<string>> other = [];
53+
54+
source.MergeWith(other);
55+
string key = Assert.Single(source.Keys);
56+
Assert.Equal("key", key);
57+
List<string> actual = source[key];
58+
string value = Assert.Single(actual);
59+
Assert.Equal("value", value);
60+
}
61+
62+
[Fact]
63+
public void MergeWith_FilledSource_FilledOther_Filled()
64+
{
65+
Dictionary<string, List<string>> source = [];
66+
source.Add("key1", ["value1"]);
67+
Dictionary<string, List<string>> other = [];
68+
other.Add("key2", ["value2"]);
69+
70+
source.MergeWith(other);
71+
Assert.Equal(2, source.Keys.Count);
72+
73+
Assert.Contains("key1", source.Keys);
74+
List<string> actualKey1 = source["key1"];
75+
string value1 = Assert.Single(actualKey1);
76+
Assert.Equal("value1", value1);
77+
78+
Assert.Contains("key2", source.Keys);
79+
List<string> actualKey2 = source["key2"];
80+
string value2 = Assert.Single(actualKey2);
81+
Assert.Equal("value2", value2);
82+
}
83+
84+
#endregion
85+
}
86+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections.Generic;
2+
3+
namespace SabreTools.IO.Extensions
4+
{
5+
public static class DictionaryExtensions
6+
{
7+
/// <summary>
8+
/// Merge a dictionary into an existing one, if possible
9+
/// </summary>
10+
/// <param name="dict">Source dictionary to add to</param>
11+
/// <param name="other">Second dictionary to add from</param>
12+
/// <remarks>This only performs a shallow copy</remarks>
13+
public static void MergeWith(this Dictionary<string, List<string>> dict, Dictionary<string, List<string>> other)
14+
{
15+
// Ignore if there are no values to append
16+
if (other.Count == 0)
17+
return;
18+
19+
// Loop through and add from the new dictionary
20+
foreach (var kvp in other)
21+
{
22+
// Ignore empty values
23+
if (kvp.Value.Count == 0)
24+
continue;
25+
26+
if (!dict.ContainsKey(kvp.Key))
27+
dict[kvp.Key] = [];
28+
29+
dict[kvp.Key].AddRange(kvp.Value);
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)