Skip to content

Commit

Permalink
Added IDictionary<string, VToken> as a superclass of VObject
Browse files Browse the repository at this point in the history
- Also fixed namespaces from the previous commit.
  • Loading branch information
shravan2x committed Jul 31, 2018
1 parent 771fb2f commit ecd7777
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 19 deletions.
78 changes: 69 additions & 9 deletions Gameloop.Vdf/Linq/VObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using System.Linq;
using System.Linq.Expressions;

namespace Gameloop.Vdf
namespace Gameloop.Vdf.Linq
{
public class VObject : VToken
public class VObject : VToken, IDictionary<string, VToken>
{
private readonly List<VProperty> _children;

Expand Down Expand Up @@ -44,21 +44,26 @@ public VToken this[string key]
{
get
{
if (!TryGetValue(key, out VProperty result))
if (!TryGetValue(key, out VToken result))
throw new KeyNotFoundException("The given key was not present.");

return result.Value;
return result;
}

set
{
if (TryGetValue(key, out VProperty result))
result.Value = value;
VProperty prop = _children.FirstOrDefault(x => x.Key == key);
if (prop != null)
prop.Value = value;
else
Add(key, value);
}
}

ICollection<string> IDictionary<string, VToken>.Keys => _children.Select(x => x.Key).ToList();

ICollection<VToken> IDictionary<string, VToken>.Values => throw new NotImplementedException();

public override IEnumerable<VProperty> Children()
{
return _children;
Expand Down Expand Up @@ -102,10 +107,10 @@ public bool Remove(string key)
return _children.RemoveAll(x => x.Key == key) != 0;
}

public bool TryGetValue(string key, out VProperty value)
public bool TryGetValue(string key, out VToken value)
{
value = _children.FirstOrDefault(x => x.Key == key);
return value != null;
value = _children.FirstOrDefault(x => x.Key == key)?.Value;
return (value != null);
}

public void RemoveAt(string key)
Expand All @@ -123,6 +128,61 @@ public override void WriteTo(VdfWriter writer)
writer.WriteObjectEnd();
}

#region ICollection<KeyValuePair<string,JToken>> Members

public IEnumerator<KeyValuePair<string, VToken>> GetEnumerator()
{
foreach (VProperty property in _children)
yield return new KeyValuePair<string, VToken>(property.Key, property.Value);
}

void ICollection<KeyValuePair<string, VToken>>.Add(KeyValuePair<string, VToken> item)
{
Add(new VProperty(item.Key, item.Value));
}

void ICollection<KeyValuePair<string, VToken>>.Clear()
{
_children.Clear();
}

bool ICollection<KeyValuePair<string, VToken>>.Contains(KeyValuePair<string, VToken> item)
{
VProperty property = _children.FirstOrDefault(x => x.Key == item.Key);
if (property == null)
return false;

return (property.Value == item.Value);
}

void ICollection<KeyValuePair<string, VToken>>.CopyTo(KeyValuePair<string, VToken>[] array, int arrayIndex)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0)
throw new ArgumentOutOfRangeException(nameof(arrayIndex), "arrayIndex is less than 0.");
if (arrayIndex >= array.Length && arrayIndex != 0)
throw new ArgumentException("arrayIndex is equal to or greater than the length of array.");
if (Count > array.Length - arrayIndex)
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.");

for (int index = 0; index < _children.Count; index++)
array[arrayIndex + index] = new KeyValuePair<string, VToken>(_children[index].Key, _children[index].Value);
}

bool ICollection<KeyValuePair<string, VToken>>.IsReadOnly => false;

bool ICollection<KeyValuePair<string, VToken>>.Remove(KeyValuePair<string, VToken> item)
{
if (!((ICollection<KeyValuePair<string, VToken>>) this).Contains(item))
return false;

((IDictionary<string, VToken>) this).Remove(item.Key);
return true;
}

#endregion

protected override DynamicMetaObject GetMetaObject(Expression parameter)
{
return new DynamicProxyMetaObject<VObject>(parameter, this, new VObjectDynamicProxy());
Expand Down
2 changes: 1 addition & 1 deletion Gameloop.Vdf/Linq/VProperty.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Gameloop.Vdf
namespace Gameloop.Vdf.Linq
{
public class VProperty : VToken
{
Expand Down
5 changes: 2 additions & 3 deletions Gameloop.Vdf/Linq/VToken.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Gameloop.Vdf.Linq;
using Gameloop.Vdf.Utilities;
using Gameloop.Vdf.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -9,7 +8,7 @@
using System.Linq;
using System.Linq.Expressions;

namespace Gameloop.Vdf
namespace Gameloop.Vdf.Linq
{
public abstract class VToken : IVEnumerable<VToken>, IDynamicMetaObjectProvider
{
Expand Down
2 changes: 1 addition & 1 deletion Gameloop.Vdf/Linq/VValue.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Gameloop.Vdf
namespace Gameloop.Vdf.Linq
{
public class VValue : VToken
{
Expand Down
3 changes: 2 additions & 1 deletion Gameloop.Vdf/VdfConvert.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Gameloop.Vdf.Linq;
using System;
using System.Globalization;
using System.IO;
using System.Text;
Expand Down
3 changes: 2 additions & 1 deletion Gameloop.Vdf/VdfSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using Gameloop.Vdf.Linq;
using System.IO;

namespace Gameloop.Vdf
{
Expand Down
4 changes: 2 additions & 2 deletions Gameloop.Vdf/VdfTextWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Gameloop.Vdf.Linq;
using System;
using System.IO;
using System.Linq;

namespace Gameloop.Vdf
{
Expand Down
3 changes: 2 additions & 1 deletion Gameloop.Vdf/VdfWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Gameloop.Vdf.Linq;
using System;

namespace Gameloop.Vdf
{
Expand Down

0 comments on commit ecd7777

Please sign in to comment.