Skip to content

Commit 2ce8542

Browse files
committed
Public class KVCollectionValue designed for pattern matching and custom keyvalue parsing, supporting case-insensitive key lookup via Get.
1 parent eebebd2 commit 2ce8542

1 file changed

Lines changed: 57 additions & 4 deletions

File tree

ValveKeyValue/ValveKeyValue/KVCollectionValue.cs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,70 @@
11
using System.Collections;
22
using System.Linq;
33

4+
#nullable enable
45
namespace ValveKeyValue
56
{
6-
class KVCollectionValue : KVValue, IEnumerable<KVObject>
7+
/// <summary>
8+
/// Represents a collection value.
9+
/// </summary>
10+
public class KVCollectionValue : KVValue, IEnumerable<KVObject>
711
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="KVCollectionValue"/> class.
14+
/// </summary>
815
public KVCollectionValue()
916
{
1017
children = new List<KVObject>();
1118
}
1219

1320
readonly List<KVObject> children;
1421

22+
/// <inheritdoc/>
1523
public override KVValueType ValueType => KVValueType.Collection;
1624

17-
public override KVValue this[string key] => Get(key)?.Value;
25+
/// <inheritdoc/>
26+
public override KVValue? this[string key] => Get(key)?.Value;
1827

28+
/// <summary>
29+
/// Adds the specified key-value object to the collection of child elements.
30+
/// </summary>
31+
/// <param name="value">The key-value object to add to the collection. Cannot be null.</param>
1932
public void Add(KVObject value)
2033
{
2134
ArgumentNullException.ThrowIfNull(value);
2235
children.Add(value);
2336
}
2437

38+
/// <summary>
39+
/// Adds the elements of the specified collection to the end of the current collection.
40+
/// </summary>
41+
/// <param name="values">The collection of <see cref="KVObject"/> instances to add. Cannot be null.</param>
2542
public void AddRange(IEnumerable<KVObject> values)
2643
{
2744
ArgumentNullException.ThrowIfNull(values);
2845
children.AddRange(values);
2946
}
3047

31-
public KVObject Get(string name)
48+
/// <summary>
49+
/// Retrieves the first child element with the specified name, using the given string comparison option.
50+
/// </summary>
51+
/// <param name="name">The name of the child element to locate. Cannot be null.</param>
52+
/// <param name="comparisonType">One of the enumeration values that determines how the name comparison is performed. The default is
53+
/// StringComparison.CurrentCulture.</param>
54+
/// <returns>A KVObject representing the first matching child element if found; otherwise, null.</returns>
55+
public KVObject? Get(string name, StringComparison comparisonType = StringComparison.CurrentCulture)
3256
{
3357
ArgumentNullException.ThrowIfNull(name);
34-
return children.FirstOrDefault(c => c.Name == name);
58+
return children.FirstOrDefault(c => string.Equals(c.Name, name, comparisonType));
3559
}
3660

61+
/// <summary>
62+
/// Sets the value associated with the specified name, replacing any existing entry with the same name.
63+
/// </summary>
64+
/// <remarks>If an entry with the specified name already exists, it is removed before adding the
65+
/// new value. This method ensures that only one entry with the given name exists after the operation.</remarks>
66+
/// <param name="name">The name of the key to set. Cannot be null.</param>
67+
/// <param name="value">The value to associate with the specified name. Cannot be null.</param>
3768
public void Set(string name, KVValue value)
3869
{
3970
ArgumentNullException.ThrowIfNull(name);
@@ -45,90 +76,111 @@ public void Set(string name, KVValue value)
4576

4677
#region IEnumerable<KVObject>
4778

79+
/// <summary>
80+
/// Returns an enumerator that iterates through the collection of child KVObject instances.
81+
/// </summary>
82+
/// <returns>An enumerator for the collection of child KVObject objects.</returns>
4883
public IEnumerator<KVObject> GetEnumerator() => children.GetEnumerator();
4984

5085
#endregion
5186

5287
#region IConvertible
5388

89+
/// <inheritdoc/>
5490
public override TypeCode GetTypeCode()
5591
{
5692
throw new NotSupportedException();
5793
}
5894

95+
/// <inheritdoc/>
5996
public override bool ToBoolean(IFormatProvider provider)
6097
{
6198
throw new NotSupportedException();
6299
}
63100

101+
/// <inheritdoc/>
64102
public override byte ToByte(IFormatProvider provider)
65103
{
66104
throw new NotSupportedException();
67105
}
68106

107+
/// <inheritdoc/>
69108
public override char ToChar(IFormatProvider provider)
70109
{
71110
throw new NotSupportedException();
72111
}
73112

113+
/// <inheritdoc/>
74114
public override DateTime ToDateTime(IFormatProvider provider)
75115
{
76116
throw new NotSupportedException();
77117
}
78118

119+
/// <inheritdoc/>
79120
public override decimal ToDecimal(IFormatProvider provider)
80121
{
81122
throw new NotSupportedException();
82123
}
83124

125+
/// <inheritdoc/>
84126
public override double ToDouble(IFormatProvider provider)
85127
{
86128
throw new NotSupportedException();
87129
}
88130

131+
/// <inheritdoc/>
89132
public override short ToInt16(IFormatProvider provider)
90133
{
91134
throw new NotSupportedException();
92135
}
93136

137+
/// <inheritdoc/>
94138
public override int ToInt32(IFormatProvider provider)
95139
{
96140
throw new NotSupportedException();
97141
}
98142

143+
/// <inheritdoc/>
99144
public override long ToInt64(IFormatProvider provider)
100145
{
101146
throw new NotSupportedException();
102147
}
103148

149+
/// <inheritdoc/>
104150
public override sbyte ToSByte(IFormatProvider provider)
105151
{
106152
throw new NotSupportedException();
107153
}
108154

155+
/// <inheritdoc/>
109156
public override float ToSingle(IFormatProvider provider)
110157
{
111158
throw new NotSupportedException();
112159
}
113160

161+
/// <inheritdoc/>
114162
public override string ToString(IFormatProvider provider)
115163
=> ToString();
116164

165+
/// <inheritdoc/>
117166
public override object ToType(Type conversionType, IFormatProvider provider)
118167
{
119168
throw new NotSupportedException();
120169
}
121170

171+
/// <inheritdoc/>
122172
public override ushort ToUInt16(IFormatProvider provider)
123173
{
124174
throw new NotSupportedException();
125175
}
126176

177+
/// <inheritdoc/>
127178
public override uint ToUInt32(IFormatProvider provider)
128179
{
129180
throw new NotSupportedException();
130181
}
131182

183+
/// <inheritdoc/>
132184
public override ulong ToUInt64(IFormatProvider provider)
133185
{
134186
throw new NotSupportedException();
@@ -142,6 +194,7 @@ public override ulong ToUInt64(IFormatProvider provider)
142194

143195
#endregion
144196

197+
/// <inheritdoc/>
145198
public override string ToString() => "[Collection]";
146199
}
147200
}

0 commit comments

Comments
 (0)