Skip to content

Commit

Permalink
Improve VToken NRT in multiple locations
Browse files Browse the repository at this point in the history
  • Loading branch information
shravan2x committed May 29, 2020
1 parent 2348632 commit faba496
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
9 changes: 4 additions & 5 deletions Gameloop.Vdf/Linq/VObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public VObject(VObject other)

public int Count => _children.Count;

public override VToken this[object key]
public override VToken? this[object key]
{
get
{
Expand All @@ -37,6 +37,7 @@ public override VToken this[object key]

return this[propertyName];
}

set
{
ValidationUtils.ArgumentNotNull(key, nameof(key));
Expand Down Expand Up @@ -256,7 +257,7 @@ protected override DynamicMetaObject GetMetaObject(Expression parameter)

private class VObjectDynamicProxy : DynamicProxy<VObject>
{
public override bool TryGetMember(VObject instance, GetMemberBinder binder, out object result)
public override bool TryGetMember(VObject instance, GetMemberBinder binder, out object? result)
{
// result can be null
result = instance[binder.Name];
Expand All @@ -265,10 +266,8 @@ public override bool TryGetMember(VObject instance, GetMemberBinder binder, out

public override bool TrySetMember(VObject instance, SetMemberBinder binder, object value)
{
VToken v = value as VToken;

// this can throw an error if value isn't a valid for a JValue
if (v == null)
if (!(value is VToken v))
v = new VValue(value);

instance[binder.Name] = v;
Expand Down
16 changes: 8 additions & 8 deletions Gameloop.Vdf/Linq/VToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace Gameloop.Vdf.Linq
public abstract class VToken : IVEnumerable<VToken>, IDynamicMetaObjectProvider
{
// TODO: Implement these.
public VToken Parent { get; internal set; }
public VToken Previous { get; internal set; }
public VToken Next { get; internal set; }
public VToken? Parent { get; internal set; }
public VToken? Previous { get; internal set; }
public VToken? Next { get; internal set; }

public abstract void WriteTo(VdfWriter writer);

Expand All @@ -31,25 +31,25 @@ IEnumerator<VToken> IEnumerable<VToken>.GetEnumerator()
return Children().GetEnumerator();
}

IVEnumerable<VToken> IVEnumerable<VToken>.this[object key] => this[key];
IVEnumerable<VToken> IVEnumerable<VToken>.this[object key] => this[key]!;

public static bool DeepEquals(VToken t1, VToken t2)
public static bool DeepEquals(VToken? t1, VToken? t2)
{
return (t1 == t2 || (t1 != null && t2 != null && t1.DeepEquals(t2)));
}

public abstract VToken DeepClone();

public virtual VToken this[object key]
public virtual VToken? this[object key]
{
get => throw new InvalidOperationException($"Cannot access child value on {GetType()}.");
set => throw new InvalidOperationException($"Cannot set child value on {GetType()}.");
}

public virtual T Value<T>(object key)
{
VToken token = this[key];
return (token == null ? default(T) : Extensions.Convert<VToken, T>(token));
VToken? token = this[key];
return (token == null ? default : Extensions.Convert<VToken, T>(token));
}

public virtual IEnumerable<VToken> Children()
Expand Down
16 changes: 8 additions & 8 deletions Gameloop.Vdf/Utilities/DynamicProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ public virtual IEnumerable<string> GetDynamicMemberNames(T instance)
return CollectionUtils.ArrayEmpty<string>();
}

public virtual bool TryBinaryOperation(T instance, BinaryOperationBinder binder, object arg, out object result)
public virtual bool TryBinaryOperation(T instance, BinaryOperationBinder binder, object arg, out object? result)
{
result = null;
return false;
}

public virtual bool TryConvert(T instance, ConvertBinder binder, out object result)
public virtual bool TryConvert(T instance, ConvertBinder binder, out object? result)
{
result = null;
return false;
}

public virtual bool TryCreateInstance(T instance, CreateInstanceBinder binder, object[] args, out object result)
public virtual bool TryCreateInstance(T instance, CreateInstanceBinder binder, object[] args, out object? result)
{
result = null;
return false;
Expand All @@ -63,25 +63,25 @@ public virtual bool TryDeleteMember(T instance, DeleteMemberBinder binder)
return false;
}

public virtual bool TryGetIndex(T instance, GetIndexBinder binder, object[] indexes, out object result)
public virtual bool TryGetIndex(T instance, GetIndexBinder binder, object[] indexes, out object? result)
{
result = null;
return false;
}

public virtual bool TryGetMember(T instance, GetMemberBinder binder, out object result)
public virtual bool TryGetMember(T instance, GetMemberBinder binder, out object? result)
{
result = null;
return false;
}

public virtual bool TryInvoke(T instance, InvokeBinder binder, object[] args, out object result)
public virtual bool TryInvoke(T instance, InvokeBinder binder, object[] args, out object? result)
{
result = null;
return false;
}

public virtual bool TryInvokeMember(T instance, InvokeMemberBinder binder, object[] args, out object result)
public virtual bool TryInvokeMember(T instance, InvokeMemberBinder binder, object[] args, out object? result)
{
result = null;
return false;
Expand All @@ -97,7 +97,7 @@ public virtual bool TrySetMember(T instance, SetMemberBinder binder, object valu
return false;
}

public virtual bool TryUnaryOperation(T instance, UnaryOperationBinder binder, out object result)
public virtual bool TryUnaryOperation(T instance, UnaryOperationBinder binder, out object? result)
{
result = null;
return false;
Expand Down

0 comments on commit faba496

Please sign in to comment.