Skip to content

Commit 83d64d8

Browse files
committed
Make setting null create a null object instead of removing
1 parent 7be27b9 commit 83d64d8

3 files changed

Lines changed: 22 additions & 33 deletions

File tree

ValveKeyValue/ValveKeyValue.Test/ConversionCoverageTestCase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void DictBackedCollectionSetChildViaIndexer()
240240
}
241241

242242
[Test]
243-
public void DictBackedCollectionSetNullRemovesKey()
243+
public void DictBackedCollectionSetNullStoresNullValue()
244244
{
245245
var kv3Text = "<!-- kv3 encoding:text:version{e21c7f3c-8a33-41c5-9977-a76d3a32aa0d} format:generic:version{7412167c-06e9-4698-aff2-e63eb59037e7} -->\n{\n\tkey1 = \"value1\"\n\tkey2 = 42\n}";
246246
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(kv3Text));
@@ -250,8 +250,8 @@ public void DictBackedCollectionSetNullRemovesKey()
250250

251251
data["key1"] = null;
252252

253-
Assert.That(data.ContainsKey("key1"), Is.False);
254-
Assert.That(data.Count, Is.EqualTo(1));
253+
Assert.That(data.Count, Is.EqualTo(2));
254+
Assert.That(data["key1"].IsNull, Is.True);
255255
Assert.That((int)data["key2"], Is.EqualTo(42));
256256
}
257257

ValveKeyValue/ValveKeyValue.Test/EdgeCaseTestCase.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,30 @@ public void IndexerSetRemovesDuplicatesAndKeepsFirstPosition()
8080
#region Indexer set with null removes child
8181

8282
[Test]
83-
public void IndexerSetNullRemovesChildFromListCollection()
83+
public void IndexerSetNullStoresNullValue()
8484
{
8585
var obj = KVObject.ListCollection();
8686
obj.Add("a", "1");
8787
obj.Add("b", "2");
8888

8989
obj["a"] = null;
9090

91-
Assert.That(obj.ContainsKey("a"), Is.False);
92-
Assert.That(obj.Count, Is.EqualTo(1));
91+
Assert.That(obj.Count, Is.EqualTo(2));
92+
Assert.That(obj["a"].IsNull, Is.True);
9393
Assert.That((string)obj["b"], Is.EqualTo("2"));
9494
}
9595

9696
[Test]
97-
public void IndexerSetNullOnMissingKeyDoesNotThrow()
97+
public void IndexerSetNullOnMissingKeyStoresNullValue()
9898
{
9999
var obj = KVObject.ListCollection();
100100
obj.Add("a", "1");
101101

102102
obj["nonexistent"] = null;
103103

104-
Assert.That(obj.Count, Is.EqualTo(1));
104+
Assert.That(obj.Count, Is.EqualTo(2));
105105
Assert.That((string)obj["a"], Is.EqualTo("1"));
106+
Assert.That(obj["nonexistent"].IsNull, Is.True);
106107
}
107108

108109
#endregion
@@ -594,15 +595,16 @@ public void RemoveDuplicateKeysFromListCollectionRemovesAll()
594595
#region SetChild null on dict-backed missing key is no-op
595596

596597
[Test]
597-
public void IndexerSetNullOnMissingKeyInDictCollectionIsNoOp()
598+
public void IndexerSetNullOnMissingKeyInDictCollectionStoresNullValue()
598599
{
599600
var obj = KVObject.Collection();
600601
obj.Add("a", 1);
601602

602603
obj["nonexistent"] = null;
603604

604-
Assert.That(obj.Count, Is.EqualTo(1));
605+
Assert.That(obj.Count, Is.EqualTo(2));
605606
Assert.That((int)obj["a"], Is.EqualTo(1));
607+
Assert.That(obj["nonexistent"].IsNull, Is.True);
606608
}
607609

608610
#endregion

ValveKeyValue/ValveKeyValue/KVObject.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -512,42 +512,29 @@ internal List<KeyValuePair<string, KVObject>> GetCollectionList()
512512

513513
private void SetChild(string key, KVObject value)
514514
{
515+
value ??= Null();
516+
515517
switch (_ref)
516518
{
517519
case Dictionary<string, KVObject> dict:
518-
if (value == null)
519-
{
520-
dict.Remove(key);
521-
}
522-
else
523-
{
524-
dict[key] = value;
525-
}
526-
520+
dict[key] = value;
527521
break;
528522
case List<KeyValuePair<string, KVObject>> list when ValueType == KVValueType.Collection:
529523
var firstIndex = list.FindIndex(c => c.Key == key);
530524
if (firstIndex >= 0)
531525
{
532-
if (value != null)
533-
{
534-
list[firstIndex] = new KeyValuePair<string, KVObject>(key, value);
526+
list[firstIndex] = new KeyValuePair<string, KVObject>(key, value);
535527

536-
// Remove any remaining duplicates after the replaced entry
537-
for (var i = list.Count - 1; i > firstIndex; i--)
528+
// Remove any remaining duplicates after the replaced entry
529+
for (var i = list.Count - 1; i > firstIndex; i--)
530+
{
531+
if (list[i].Key == key)
538532
{
539-
if (list[i].Key == key)
540-
{
541-
list.RemoveAt(i);
542-
}
533+
list.RemoveAt(i);
543534
}
544535
}
545-
else
546-
{
547-
list.RemoveAll(c => c.Key == key);
548-
}
549536
}
550-
else if (value != null)
537+
else
551538
{
552539
list.Add(new KeyValuePair<string, KVObject>(key, value));
553540
}

0 commit comments

Comments
 (0)