Преглед изворни кода

Added proper logging to Serialisation

Kenric Nugteren пре 1 година
родитељ
комит
f37600537b

+ 2 - 2
InABox.Core/DatabaseSchema/CustomProperty.cs

@@ -15,7 +15,7 @@ namespace InABox.Core
 
         private string _name = "";
 
-        private Action<object, object> _setter;
+        private Action<object, object?> _setter;
 
         private string _type;
 
@@ -248,7 +248,7 @@ namespace InABox.Core
             return _getter;
         }
 
-        public Action<object, object> Setter()
+        public Action<object, object?> Setter()
         {
             return _setter;
         }

+ 1 - 1
InABox.Core/DatabaseSchema/IProperty.cs

@@ -59,7 +59,7 @@ namespace InABox.Core
 
         Func<object, object> Getter();
 
-        Action<object, object> Setter();
+        Action<object, object?> Setter();
 
         TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute;
     }

+ 2 - 2
InABox.Core/DatabaseSchema/StandardProperty.cs

@@ -15,7 +15,7 @@ namespace InABox.Core
 
         private bool? _calculated;
 
-        private Action<object, object> _setter;
+        private Action<object, object?> _setter;
 
         public string _type = "";
         public Type _propertyType = typeof(object);
@@ -140,7 +140,7 @@ namespace InABox.Core
             return _getter;
         }
 
-        public Action<object, object> Setter()
+        public Action<object, object?> Setter()
         {
             return _setter;
         }

+ 10 - 10
InABox.Core/Expressions.cs

@@ -183,9 +183,9 @@ namespace InABox.Core
             return compiled;
         }
 
-        public static Action<object, object> Setter(Type objectType, string propname, Type? type = null)
+        public static Action<object, object?> Setter(Type objectType, string propname, Type? type = null)
         {
-            Action<object, object>? compiled = null;
+            Action<object, object?>? compiled = null;
             try
             {
                 var objectParameter = Expression.Parameter(typeof(object), "o");
@@ -196,7 +196,7 @@ namespace InABox.Core
                     body = Expression.PropertyOrField(body, member);
 
                 var valueParameter = Expression.Parameter(typeof(object), "v");
-                UnaryExpression value = null;
+                UnaryExpression value;
                 if (type != null)
                 {
                     var val1 = Expression.Convert(valueParameter, type);
@@ -224,7 +224,7 @@ namespace InABox.Core
                     expression = Expression.Empty();
                 }
 
-                var lambda = Expression.Lambda<Action<object, object>>(expression, objectParameter, valueParameter);
+                var lambda = Expression.Lambda<Action<object, object?>>(expression, objectParameter, valueParameter);
                 compiled = lambda.Compile();
             }
             catch (Exception e)
@@ -235,9 +235,9 @@ namespace InABox.Core
             return compiled;
         }
 
-        public static Action<T, object> Setter<T>(string propname, string key)
+        public static Action<T, object?> Setter<T>(string propname, string key)
         {
-            Action<T, object> result = null;
+            Action<T, object?> result = null;
             try
             {
                 var param = Expression.Parameter(typeof(T), "o");
@@ -263,7 +263,7 @@ namespace InABox.Core
 
                 var assign = Expression.Assign(indexExpr, valueExpr);
 
-                var lambdaSetter = Expression.Lambda<Action<T, object>>(assign, param, valueExpr);
+                var lambdaSetter = Expression.Lambda<Action<T, object?>>(assign, param, valueExpr);
                 result = lambdaSetter.Compile();
             }
             catch (Exception e)
@@ -274,9 +274,9 @@ namespace InABox.Core
             return result;
         }
 
-        public static Action<object, object> Setter(Type objectType, string propname, string key)
+        public static Action<object, object?> Setter(Type objectType, string propname, string key)
         {
-            Action<object, object> result = null;
+            Action<object, object?> result = null;
             try
             {
                 var objectParameter = Expression.Parameter(typeof(object), "o");
@@ -303,7 +303,7 @@ namespace InABox.Core
 
                 var assign = Expression.Assign(indexExpr, valueExpr);
 
-                var lambdaSetter = Expression.Lambda<Action<object, object>>(assign, objectParameter, valueExpr);
+                var lambdaSetter = Expression.Lambda<Action<object, object?>>(assign, objectParameter, valueExpr);
                 result = lambdaSetter.Compile();
             }
             catch (Exception e)

+ 25 - 14
InABox.Core/Serialization.cs

@@ -14,6 +14,11 @@ using Newtonsoft.Json.Linq;
 
 namespace InABox.Core
 {
+    public class SerialisationException : Exception
+    {
+        public SerialisationException(string message): base(message) { }
+    }
+
     public interface ISerializeBinary
     {
         public void SerializeBinary(CoreBinaryWriter writer);
@@ -494,7 +499,7 @@ namespace InABox.Core
                 WriteBinaryValue(writer, userprop.Type, userprop.Value);
             
             else
-                throw new Exception($"Invalid type; Target DataType is {type} and value DataType is {value?.GetType().ToString() ?? "null"}");
+                throw new SerialisationException($"Invalid type; Target DataType is {type} and value DataType is {value?.GetType().ToString() ?? "null"}");
             
         }
 
@@ -630,7 +635,7 @@ namespace InABox.Core
             }
             else
             {
-                throw new Exception($"Invalid type; Target DataType is {type}");
+                throw new SerialisationException($"Invalid type; Target DataType is {type}");
             }
         }
 
@@ -720,7 +725,7 @@ namespace InABox.Core
             where TObject : BaseObject
         {
             if (!typeof(TObject).IsAssignableFrom(type))
-                throw new Exception($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
+                throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
 
             var properties = SerializableProperties(type).ToList();
 
@@ -749,7 +754,7 @@ namespace InABox.Core
             where TObject : BaseObject
         {
             if (!typeof(TObject).IsAssignableFrom(type))
-                throw new Exception($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
+                throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
 
             var obj = (Activator.CreateInstance(type) as TObject)!;
             obj.SetObserving(false);
@@ -758,8 +763,9 @@ namespace InABox.Core
             for (int i = 0; i < nProps; ++i)
             {
                 var propName = reader.ReadString();
-                var property = DatabaseSchema.Property(type, propName);
-                property?.Setter()(obj, reader.ReadBinaryValue(property.PropertyType));
+                var property = DatabaseSchema.Property(type, propName)
+                    ?? throw new SerialisationException($"Property {propName} does not exist on {type.EntityName()}");
+                property.Setter()(obj, reader.ReadBinaryValue(property.PropertyType));
             }
             reader.ReadOriginalValues(obj);
 
@@ -794,7 +800,7 @@ namespace InABox.Core
             where TObject : BaseObject
         {
             if (!typeof(TObject).IsAssignableFrom(type))
-                throw new Exception($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
+                throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
 
             var nObjs = objects?.Count ?? 0;
             writer.Write(nObjs);
@@ -811,13 +817,16 @@ namespace InABox.Core
                 writer.Write(property.Name);
             }
 
-            foreach (var obj in objects)
+            if(objects != null)
             {
-                foreach (var property in properties)
+                foreach (var obj in objects)
                 {
-                    writer.WriteBinaryValue(property.PropertyType, property.Getter()(obj));
+                    foreach (var property in properties)
+                    {
+                        writer.WriteBinaryValue(property.PropertyType, property.Getter()(obj));
+                    }
+                    writer.WriteOriginalValues(obj);
                 }
-                writer.WriteOriginalValues(obj);
             }
         }
 
@@ -836,7 +845,7 @@ namespace InABox.Core
         public static List<TObject> ReadObjects<TObject>(this CoreBinaryReader reader, Type type) where TObject : BaseObject
         {
             if (!typeof(TObject).IsAssignableFrom(type))
-                throw new Exception($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
+                throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
             
             var objs = new List<TObject>();
             var properties = new List<IProperty>();
@@ -850,8 +859,10 @@ namespace InABox.Core
             var nProps = reader.ReadInt32();
             for (int i = 0; i < nProps; ++i)
             {
-                var property = reader.ReadString();
-                properties.Add(DatabaseSchema.Property(type, property));
+                var propertyName = reader.ReadString();
+                var property = DatabaseSchema.Property(type, propertyName)
+                    ?? throw new SerialisationException($"Property {propertyName} does not exist on {type.EntityName()}"); 
+                properties.Add(property);
             }
 
             for (int i = 0; i < nObjs; ++i)

+ 1 - 1
InABox.Server/RPC/Handlers/RPCCommandHandler.cs

@@ -14,7 +14,7 @@ namespace InABox.Rpc
     {
         public TSender Sender { get; }
         
-        private static bool IsLogCommand = typeof(TCommand).IsAssignableTo(typeof(IRpcLogCommand));
+        private static readonly bool IsLogCommand = typeof(TCommand).IsAssignableTo(typeof(IRpcLogCommand));
 
         public RpcCommandHandler(TSender sender)
         {

+ 1 - 1
inabox.wpf/DynamicGrid/Editors/DynamicEnclosedEditorControl.cs

@@ -36,7 +36,7 @@ namespace InABox.DynamicGrid
                 Updating = true;
                 try
                 {
-                    var values = new Dictionary<string, object>();
+                    var values = new Dictionary<string, object?>();
                     var sColumn = string.IsNullOrEmpty(ColumnName) ? "" : ColumnName;
                     foreach(var (k, v) in GetValues())
                     {

+ 2 - 0
inabox.wpf/Forms/MessageWindow.xaml.cs

@@ -208,6 +208,8 @@ public partial class MessageWindow : Window, INotifyPropertyChanged
 
     public static readonly BitmapImage _warning = InABox.Wpf.Resources.warning.AsBitmapImage();
 
+    public static BitmapImage WarningImage => _warning;
+
     public static MessageWindow New()
     {
         return new MessageWindow();