Skip to content

Commit d6356e8

Browse files
committed
Implement ICollection/IList on KVArrayValue
1 parent 20e18e8 commit d6356e8

File tree

2 files changed

+55
-22
lines changed

2 files changed

+55
-22
lines changed

ValveKeyValue/ValveKeyValue.Test/TextKV3/BasicKV3TestCases.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ public void DeserializesArray()
9595
Assert.That(data["arrayNoSpace"].ValueType, Is.EqualTo(KVValueType.Array));
9696
Assert.That(data["arrayMixedTypes"].ValueType, Is.EqualTo(KVValueType.Array));
9797

98+
var arrayValue = (KVArrayValue)data["arrayValue"];
99+
100+
Assert.That(arrayValue.Count, Is.EqualTo(2));
101+
Assert.That(arrayValue[0].ToString(), Is.EqualTo("a"));
102+
Assert.That(arrayValue[1].ToString(), Is.EqualTo("b"));
103+
98104
// TODO: Test all the children values
99105
}
100106

ValveKeyValue/ValveKeyValue/KVArrayValue.cs

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace ValveKeyValue
66
{
7-
class KVArrayValue : KVValue, IEnumerable<KVValue>
7+
public class KVArrayValue : KVValue, IEnumerable<KVValue>, ICollection<KVValue>, IList<KVValue>
88
{
99
public KVArrayValue()
1010
{
@@ -15,16 +15,26 @@ public KVArrayValue()
1515

1616
public override KVValueType ValueType => KVValueType.Array;
1717

18-
/*
18+
public int Count => children.Count;
19+
20+
public bool IsReadOnly => false;
21+
1922
public override KVValue this[string key]
23+
{
24+
get { throw new NotSupportedException($"The indexer on a {nameof(KVArrayValue)} can only be used on integer keys, not strings."); }
25+
}
26+
27+
public KVValue this[int key]
2028
{
2129
get
2230
{
23-
Require.NotNull(key, nameof(key));
24-
return Get(key)?.Value;
31+
return children[key];
32+
}
33+
set
34+
{
35+
children[key] = value;
2536
}
2637
}
27-
*/
2838

2939
public void Add(KVValue value)
3040
{
@@ -38,23 +48,6 @@ public void AddRange(IEnumerable<KVValue> values)
3848
children.AddRange(values);
3949
}
4050

41-
/*
42-
public KVObject Get(string name)
43-
{
44-
Require.NotNull(name, nameof(name));
45-
return children.FirstOrDefault(c => c.Name == name);
46-
}
47-
48-
public void Set(string name, KVValue value)
49-
{
50-
Require.NotNull(name, nameof(name));
51-
Require.NotNull(value, nameof(value));
52-
53-
children.RemoveAll(kv => kv.Name == name);
54-
children.Add(new KVObject(name, value));
55-
}
56-
*/
57-
5851
#region IEnumerable<KVValue>
5952

6053
public IEnumerator<KVValue> GetEnumerator() => children.GetEnumerator();
@@ -155,5 +148,39 @@ public override ulong ToUInt64(IFormatProvider provider)
155148
#endregion
156149

157150
public override string ToString() => "[Array]";
151+
152+
public void Clear() => children.Clear();
153+
154+
public bool Contains(KVValue item)
155+
{
156+
Require.NotNull(item, nameof(item));
157+
return children.Contains(item);
158+
}
159+
160+
public void CopyTo(KVValue[] array, int arrayIndex)
161+
{
162+
Require.NotNull(array, nameof(array));
163+
children.CopyTo(array, arrayIndex);
164+
}
165+
166+
public bool Remove(KVValue item)
167+
{
168+
Require.NotNull(item, nameof(item));
169+
return children.Remove(item);
170+
}
171+
172+
public int IndexOf(KVValue item)
173+
{
174+
Require.NotNull(item, nameof(item));
175+
return children.IndexOf(item);
176+
}
177+
178+
public void Insert(int index, KVValue item)
179+
{
180+
Require.NotNull(item, nameof(item));
181+
children.Insert(index, item);
182+
}
183+
184+
public void RemoveAt(int index) => children.RemoveAt(index);
158185
}
159186
}

0 commit comments

Comments
 (0)