From faba4968ce5c4564af82d79072c20937fbbc639e Mon Sep 17 00:00:00 2001 From: Shravan Rajinikanth Date: Thu, 28 May 2020 23:15:03 -0700 Subject: [PATCH] Improve VToken NRT in multiple locations --- Gameloop.Vdf/Linq/VObject.cs | 9 ++++----- Gameloop.Vdf/Linq/VToken.cs | 16 ++++++++-------- Gameloop.Vdf/Utilities/DynamicProxy.cs | 16 ++++++++-------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Gameloop.Vdf/Linq/VObject.cs b/Gameloop.Vdf/Linq/VObject.cs index 1c24432..62a8442 100644 --- a/Gameloop.Vdf/Linq/VObject.cs +++ b/Gameloop.Vdf/Linq/VObject.cs @@ -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 { @@ -37,6 +37,7 @@ public override VToken this[object key] return this[propertyName]; } + set { ValidationUtils.ArgumentNotNull(key, nameof(key)); @@ -256,7 +257,7 @@ protected override DynamicMetaObject GetMetaObject(Expression parameter) private class VObjectDynamicProxy : DynamicProxy { - 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]; @@ -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; diff --git a/Gameloop.Vdf/Linq/VToken.cs b/Gameloop.Vdf/Linq/VToken.cs index 7716883..3edc6ff 100644 --- a/Gameloop.Vdf/Linq/VToken.cs +++ b/Gameloop.Vdf/Linq/VToken.cs @@ -13,9 +13,9 @@ namespace Gameloop.Vdf.Linq public abstract class VToken : IVEnumerable, 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); @@ -31,16 +31,16 @@ IEnumerator IEnumerable.GetEnumerator() return Children().GetEnumerator(); } - IVEnumerable IVEnumerable.this[object key] => this[key]; + IVEnumerable IVEnumerable.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()}."); @@ -48,8 +48,8 @@ public virtual VToken this[object key] public virtual T Value(object key) { - VToken token = this[key]; - return (token == null ? default(T) : Extensions.Convert(token)); + VToken? token = this[key]; + return (token == null ? default : Extensions.Convert(token)); } public virtual IEnumerable Children() diff --git a/Gameloop.Vdf/Utilities/DynamicProxy.cs b/Gameloop.Vdf/Utilities/DynamicProxy.cs index 8b6928a..33f6425 100644 --- a/Gameloop.Vdf/Utilities/DynamicProxy.cs +++ b/Gameloop.Vdf/Utilities/DynamicProxy.cs @@ -35,19 +35,19 @@ public virtual IEnumerable GetDynamicMemberNames(T instance) return CollectionUtils.ArrayEmpty(); } - 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; @@ -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; @@ -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;