Skip to content

Commit ecd7777

Browse files
committed
Added IDictionary<string, VToken> as a superclass of VObject
- Also fixed namespaces from the previous commit.
1 parent 771fb2f commit ecd7777

File tree

8 files changed

+81
-19
lines changed

8 files changed

+81
-19
lines changed

Gameloop.Vdf/Linq/VObject.cs

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
using System.Linq;
66
using System.Linq.Expressions;
77

8-
namespace Gameloop.Vdf
8+
namespace Gameloop.Vdf.Linq
99
{
10-
public class VObject : VToken
10+
public class VObject : VToken, IDictionary<string, VToken>
1111
{
1212
private readonly List<VProperty> _children;
1313

@@ -44,21 +44,26 @@ public VToken this[string key]
4444
{
4545
get
4646
{
47-
if (!TryGetValue(key, out VProperty result))
47+
if (!TryGetValue(key, out VToken result))
4848
throw new KeyNotFoundException("The given key was not present.");
4949

50-
return result.Value;
50+
return result;
5151
}
5252

5353
set
5454
{
55-
if (TryGetValue(key, out VProperty result))
56-
result.Value = value;
55+
VProperty prop = _children.FirstOrDefault(x => x.Key == key);
56+
if (prop != null)
57+
prop.Value = value;
5758
else
5859
Add(key, value);
5960
}
6061
}
6162

63+
ICollection<string> IDictionary<string, VToken>.Keys => _children.Select(x => x.Key).ToList();
64+
65+
ICollection<VToken> IDictionary<string, VToken>.Values => throw new NotImplementedException();
66+
6267
public override IEnumerable<VProperty> Children()
6368
{
6469
return _children;
@@ -102,10 +107,10 @@ public bool Remove(string key)
102107
return _children.RemoveAll(x => x.Key == key) != 0;
103108
}
104109

105-
public bool TryGetValue(string key, out VProperty value)
110+
public bool TryGetValue(string key, out VToken value)
106111
{
107-
value = _children.FirstOrDefault(x => x.Key == key);
108-
return value != null;
112+
value = _children.FirstOrDefault(x => x.Key == key)?.Value;
113+
return (value != null);
109114
}
110115

111116
public void RemoveAt(string key)
@@ -123,6 +128,61 @@ public override void WriteTo(VdfWriter writer)
123128
writer.WriteObjectEnd();
124129
}
125130

131+
#region ICollection<KeyValuePair<string,JToken>> Members
132+
133+
public IEnumerator<KeyValuePair<string, VToken>> GetEnumerator()
134+
{
135+
foreach (VProperty property in _children)
136+
yield return new KeyValuePair<string, VToken>(property.Key, property.Value);
137+
}
138+
139+
void ICollection<KeyValuePair<string, VToken>>.Add(KeyValuePair<string, VToken> item)
140+
{
141+
Add(new VProperty(item.Key, item.Value));
142+
}
143+
144+
void ICollection<KeyValuePair<string, VToken>>.Clear()
145+
{
146+
_children.Clear();
147+
}
148+
149+
bool ICollection<KeyValuePair<string, VToken>>.Contains(KeyValuePair<string, VToken> item)
150+
{
151+
VProperty property = _children.FirstOrDefault(x => x.Key == item.Key);
152+
if (property == null)
153+
return false;
154+
155+
return (property.Value == item.Value);
156+
}
157+
158+
void ICollection<KeyValuePair<string, VToken>>.CopyTo(KeyValuePair<string, VToken>[] array, int arrayIndex)
159+
{
160+
if (array == null)
161+
throw new ArgumentNullException(nameof(array));
162+
if (arrayIndex < 0)
163+
throw new ArgumentOutOfRangeException(nameof(arrayIndex), "arrayIndex is less than 0.");
164+
if (arrayIndex >= array.Length && arrayIndex != 0)
165+
throw new ArgumentException("arrayIndex is equal to or greater than the length of array.");
166+
if (Count > array.Length - arrayIndex)
167+
throw new ArgumentException("The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.");
168+
169+
for (int index = 0; index < _children.Count; index++)
170+
array[arrayIndex + index] = new KeyValuePair<string, VToken>(_children[index].Key, _children[index].Value);
171+
}
172+
173+
bool ICollection<KeyValuePair<string, VToken>>.IsReadOnly => false;
174+
175+
bool ICollection<KeyValuePair<string, VToken>>.Remove(KeyValuePair<string, VToken> item)
176+
{
177+
if (!((ICollection<KeyValuePair<string, VToken>>) this).Contains(item))
178+
return false;
179+
180+
((IDictionary<string, VToken>) this).Remove(item.Key);
181+
return true;
182+
}
183+
184+
#endregion
185+
126186
protected override DynamicMetaObject GetMetaObject(Expression parameter)
127187
{
128188
return new DynamicProxyMetaObject<VObject>(parameter, this, new VObjectDynamicProxy());

Gameloop.Vdf/Linq/VProperty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace Gameloop.Vdf
3+
namespace Gameloop.Vdf.Linq
44
{
55
public class VProperty : VToken
66
{

Gameloop.Vdf/Linq/VToken.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Gameloop.Vdf.Linq;
2-
using Gameloop.Vdf.Utilities;
1+
using Gameloop.Vdf.Utilities;
32
using System;
43
using System.Collections;
54
using System.Collections.Generic;
@@ -9,7 +8,7 @@
98
using System.Linq;
109
using System.Linq.Expressions;
1110

12-
namespace Gameloop.Vdf
11+
namespace Gameloop.Vdf.Linq
1312
{
1413
public abstract class VToken : IVEnumerable<VToken>, IDynamicMetaObjectProvider
1514
{

Gameloop.Vdf/Linq/VValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace Gameloop.Vdf
3+
namespace Gameloop.Vdf.Linq
44
{
55
public class VValue : VToken
66
{

Gameloop.Vdf/VdfConvert.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Gameloop.Vdf.Linq;
2+
using System;
23
using System.Globalization;
34
using System.IO;
45
using System.Text;

Gameloop.Vdf/VdfSerializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using Gameloop.Vdf.Linq;
2+
using System.IO;
23

34
namespace Gameloop.Vdf
45
{

Gameloop.Vdf/VdfTextWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
1+
using Gameloop.Vdf.Linq;
2+
using System;
23
using System.IO;
3-
using System.Linq;
44

55
namespace Gameloop.Vdf
66
{

Gameloop.Vdf/VdfWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Gameloop.Vdf.Linq;
2+
using System;
23

34
namespace Gameloop.Vdf
45
{

0 commit comments

Comments
 (0)