Finish CoreConfig
MODIFY
WalletWasabi.Tests/UnitTests/BitcoinCore/ConfigTests.cs
WalletWasabi.Tests/UnitTests/BitcoinCore/ConfigTests.cs
@@ -25,6 +25,19 @@ bar
var coreConfig = new CoreConfig();
coreConfig.TryAdd(testConfig);
+ var expectedConfig =
+@"foo = buz
+
+foo bar = buz quxx
+
+too = 1
+foo
+bar
+#qoo=boo
+";
+
+ Assert.Equal(expectedConfig, coreConfig.ToString());
+
var configDic = coreConfig.ToDictionary();
Assert.True(configDic.TryGetValue("foo", out string v1));
@@ -80,16 +93,31 @@ bar
Assert.Equal("bar", fooValue);
Assert.Equal("0", tooValue);
- var expectedConfig =
-@"foo=bar
-foo = bar
- foo = bar
+ expectedConfig =
+@"foo = buz
+
foo bar = buz quxx
+too = 1
foo
+bar
#qoo=boo
moo = 1
-too = 0";
+";
+
+ Assert.Equal(expectedConfig, coreConfig.ToString());
+
+ var expectedConfig2 =
+@"foo bar = buz quxx
+
+foo
+bar
+#qoo=boo
+moo = 1
+foo = bar
+too = 0
+";
+ Assert.Equal(expectedConfig2, coreConfig2.ToString());
}
}
}

MODIFY
WalletWasabi/BitcoinCore/CoreConfig.cs
WalletWasabi/BitcoinCore/CoreConfig.cs
@@ -24,29 +24,28 @@ namespace WalletWasabi.BitcoinCore
return myDic;
}
- public override string ToString() => $"{string.Join(Environment.NewLine, Lines)}{Environment.NewLine}"; // Good practice to end with a newline.
+ public override string ToString() => $"{Guard.Correct(string.Join(Environment.NewLine, Lines))}{Environment.NewLine}"; // Good practice to end with a newline.
/// <summary>
/// TryAdd and AddOrUpdate are sisters. TryAdd always considers the first occurrence of a key as valid, while AddOrUpdate considers the last occurrence of it.
/// </summary>
public void TryAdd(string configString)
{
- configString = Guard.Correct(configString);
- foreach (var line in configString.Split(new char[] { '\r', '\n' }, StringSplitOptions.None))
+ foreach (var line in ParseConfigLines(configString))
{
- var currentLine = new CoreConfigLine(line);
- if (currentLine.HasKeyValuePair)
+ if (line.HasKeyValuePair)
{
- var foundLine = Lines.FirstOrDefault(x => x.Key == currentLine.Key);
+ var foundLine = Lines.FirstOrDefault(x => x.Key == line.Key);
if (foundLine is null)
{
- Lines.Add(currentLine);
+ Lines.Add(line);
}
}
else
{
- Lines.Add(currentLine);
+ Lines.Add(line);
}
+ RemoveFirstEmptyDuplication();
}
}
@@ -55,28 +54,56 @@ namespace WalletWasabi.BitcoinCore
/// </summary>
public void AddOrUpdate(string configString)
{
- configString = Guard.Correct(configString);
- foreach (var line in configString.Split(new char[] { '\r', '\n' }, StringSplitOptions.None))
+ foreach (var line in ParseConfigLines(configString))
{
- var currentLine = new CoreConfigLine(line);
- if (currentLine.HasKeyValuePair)
+ if (line.HasKeyValuePair)
{
- var foundLine = Lines.FirstOrDefault(x => x.Key == currentLine.Key);
+ var foundLine = Lines.FirstOrDefault(x => x.Key == line.Key);
if (foundLine is null)
{
- Lines.Add(currentLine);
+ Lines.Add(line);
}
else
{
Lines.Remove(foundLine);
- Lines.Add(currentLine);
+ Lines.Add(line);
}
}
else
{
- Lines.Add(currentLine);
+ Lines.Add(line);
+ }
+ RemoveFirstEmptyDuplication();
}
}
+
+ private void RemoveFirstEmptyDuplication()
+ {
+ CoreConfigLine toRemove = null;
+ for (int i = 1; i < Lines.Count; i++)
+ {
+ CoreConfigLine currentLine = Lines[i];
+ CoreConfigLine prevLine = Lines[i - 1];
+ if (string.IsNullOrWhiteSpace(currentLine.Line) && string.IsNullOrWhiteSpace(prevLine.Line))
+ {
+ toRemove = currentLine;
+ break;
+ }
+ }
+ if (toRemove != null)
+ {
+ Lines.Remove(toRemove);
+ }
+ }
+
+ private static IEnumerable<CoreConfigLine> ParseConfigLines(string configString)
+ {
+ configString = Guard.Correct(configString);
+ var allLines = configString.Split('\n', StringSplitOptions.None);
+ return allLines
+ .Select(x => Guard.Correct(x))
+ .Where((x, i) => i == 0 || x != allLines[i - 1])
+ .Select(x => new CoreConfigLine(x));
}
}
}

MODIFY
WalletWasabi/BitcoinCore/CoreConfigLine.cs
WalletWasabi/BitcoinCore/CoreConfigLine.cs
@@ -36,6 +36,7 @@ namespace WalletWasabi.BitcoinCore
Key = parts[0];
Value = parts[1];
+ Line = $"{Key} = {Value}";
}
public override string ToString() => Line;

Finish CoreConfig