Added data evaluation, made parser more robust, switched to NewtonSoft json
This commit is contained in:
2
SessionZero.sln.DotSettings.user
Normal file
2
SessionZero.sln.DotSettings.user
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||||
|
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADictionary_00602_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003Fhome_003Fchris_003F_002Econfig_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa7e0e0f67f744397a03b2e746d7e5534b2e200_003F39_003F247644fd_003FDictionary_00602_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>
|
||||||
@@ -16,7 +16,10 @@ public class Program
|
|||||||
|
|
||||||
Test.CreateTestData();
|
Test.CreateTestData();
|
||||||
|
|
||||||
|
var dataset = SZ.SzDataHandler.LoadDataset("test-dataset");
|
||||||
var dataTemplate = SZ.SzDataHandler.LoadTemplate<SzDataObjectTemplate>("szcore-item-basic");
|
var dataTemplate = SZ.SzDataHandler.LoadTemplate<SzDataObjectTemplate>("szcore-item-basic");
|
||||||
_logger.Log($"Test - {dataTemplate?.DataObjectType ?? "Template was null"}");
|
|
||||||
|
var evalResult = SZ.SzEvaluator.EvaluateDataObject(dataset.DataObjects["test"], dataTemplate);
|
||||||
|
_logger.Log($"Evaluation test -- Pass?: {evalResult.Pass} {(evalResult.Errors == "" ? "" : (" - Errors: " + evalResult.Errors))}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,7 +48,7 @@ public static class Test
|
|||||||
|
|
||||||
var testField = new SzField()
|
var testField = new SzField()
|
||||||
{
|
{
|
||||||
Id = "value",
|
Id = "cost",
|
||||||
FieldType = SzFieldType.Number,
|
FieldType = SzFieldType.Number,
|
||||||
Value = "200"
|
Value = "200"
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ namespace SzCore.DataObjects;
|
|||||||
|
|
||||||
public enum SzFieldType
|
public enum SzFieldType
|
||||||
{
|
{
|
||||||
Text, Number, Bool, Formula, Ref
|
Text, Number, Bool, Formula, Reference
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@ public static class SZ
|
|||||||
|
|
||||||
public static readonly SzParser SzParser = new();
|
public static readonly SzParser SzParser = new();
|
||||||
public static readonly SzDataHandler SzDataHandler = new();
|
public static readonly SzDataHandler SzDataHandler = new();
|
||||||
|
public static readonly SzEvaluator SzEvaluator = new();
|
||||||
|
|
||||||
public static ISzFileManager LocalFileManager
|
public static ISzFileManager LocalFileManager
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,7 +6,9 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
102
SzCore/SzEvaluator.cs
Normal file
102
SzCore/SzEvaluator.cs
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
using System.Data;
|
||||||
|
using System.Text;
|
||||||
|
using SzCore.DataObjects;
|
||||||
|
|
||||||
|
namespace SzCore;
|
||||||
|
|
||||||
|
public class SzEvaluator
|
||||||
|
{
|
||||||
|
public SzEvaluationResult EvaluateDataObject(SzDataObject dataObject, SzDataObjectTemplate template)
|
||||||
|
{
|
||||||
|
if (dataObject is null || template is null) return new SzEvaluationResult(false, "dataObject or template is null");
|
||||||
|
|
||||||
|
var errors = new StringBuilder();
|
||||||
|
|
||||||
|
foreach (var (key, templateField) in template.TemplateFields)
|
||||||
|
{
|
||||||
|
if (!dataObject.Fields.TryGetValue(key, out var matchingField))
|
||||||
|
{
|
||||||
|
errors.AppendLine($"Missing required field: '{key}'");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var templateMatchResult = DoesFieldMatchTemplateField(matchingField, templateField);
|
||||||
|
if (!templateMatchResult.Pass) errors.Append(templateMatchResult.Errors);
|
||||||
|
|
||||||
|
var evalResult = EvaluateFieldValue(matchingField);
|
||||||
|
if (!evalResult.Pass) errors.Append(evalResult.Errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var objectFieldKey in dataObject.Fields.Keys)
|
||||||
|
{
|
||||||
|
if (!template.TemplateFields.ContainsKey(objectFieldKey))
|
||||||
|
{
|
||||||
|
errors.AppendLine($"DataObject contains extra field '{objectFieldKey}' not found in template");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var errString = errors.ToString().Trim();
|
||||||
|
return new SzEvaluationResult(string.IsNullOrEmpty(errString), string.IsNullOrEmpty(errString) ? "" : errString);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SzEvaluationResult DoesFieldMatchTemplateField(SzField field, SzTemplateField templateField)
|
||||||
|
{
|
||||||
|
var errors = new StringBuilder();
|
||||||
|
|
||||||
|
if (field.Id != templateField.Id) errors.AppendLine($"Field ID mismatch for '{field.Id}': Expected {templateField.Id}");
|
||||||
|
if (field.FieldType != templateField.FieldType) errors.AppendLine($"Field '{field.Id}' type mismatch: Expected {templateField.FieldType}, got {field.FieldType}");
|
||||||
|
if (field.IsList != templateField.IsList) errors.AppendLine($"Field '{field.Id}' IsList mismatch: Expected {templateField.IsList}");
|
||||||
|
|
||||||
|
var errString = errors.ToString().Trim();
|
||||||
|
return new SzEvaluationResult(string.IsNullOrEmpty(errString), errString);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SzEvaluationResult EvaluateFieldValue(SzField field)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(field.Value))
|
||||||
|
return new SzEvaluationResult(true, "");
|
||||||
|
|
||||||
|
var errors = new StringBuilder();
|
||||||
|
|
||||||
|
switch (field.FieldType)
|
||||||
|
{
|
||||||
|
case SzFieldType.Text:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SzFieldType.Number:
|
||||||
|
if (!float.TryParse(field.Value, out _))
|
||||||
|
{
|
||||||
|
errors.AppendLine($"Value '{field.Value}' in field '{field.Id}' is not a valid Number");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SzFieldType.Bool:
|
||||||
|
if (!bool.TryParse(field.Value, out _))
|
||||||
|
{
|
||||||
|
errors.AppendLine($"Value '{field.Value}' in field '{field.Id}' is not a valid Boolean");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SzFieldType.Formula:
|
||||||
|
errors.AppendLine($"Formula field type is not implemented yet. Field: {field.Id}");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SzFieldType.Reference:
|
||||||
|
errors.AppendLine($"Reference field type is not implemented yet. Field: {field.Id}");
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
errors.AppendLine($"Unsupported FieldType '{field.FieldType}' for field '{field.Id}'");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var errString = errors.ToString().Trim();
|
||||||
|
return new SzEvaluationResult(string.IsNullOrEmpty(errString), errString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SzEvaluationResult(bool pass, string errors = "None")
|
||||||
|
{
|
||||||
|
public bool Pass = pass;
|
||||||
|
public string Errors = errors;
|
||||||
|
}
|
||||||
@@ -1,19 +1,22 @@
|
|||||||
using System.Text.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
using SzCore.DataObjects;
|
using SzCore.DataObjects;
|
||||||
|
|
||||||
namespace SzCore;
|
namespace SzCore;
|
||||||
|
|
||||||
public class SzParser
|
public class SzParser
|
||||||
{
|
{
|
||||||
|
private readonly JsonSerializerSettings _jsonSettings = new()
|
||||||
private readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
|
{
|
||||||
|
Formatting = Formatting.Indented,
|
||||||
|
Converters = { new StringEnumConverter() }
|
||||||
|
};
|
||||||
|
|
||||||
public string SerializeDatasetToJson(SzDataset dataset)
|
public string SerializeDatasetToJson(SzDataset dataset)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var jsonString = JsonSerializer.Serialize(dataset, _jsonOptions);
|
return JsonConvert.SerializeObject(dataset, _jsonSettings);
|
||||||
return jsonString;
|
|
||||||
}
|
}
|
||||||
catch (JsonException e)
|
catch (JsonException e)
|
||||||
{
|
{
|
||||||
@@ -25,8 +28,7 @@ public class SzParser
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var result = JsonSerializer.Deserialize<SzDataset>(jsonString, _jsonOptions);
|
return JsonConvert.DeserializeObject<SzDataset>(jsonString, _jsonSettings);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@@ -39,9 +41,7 @@ public class SzParser
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var templateType = template.GetType();
|
return JsonConvert.SerializeObject(template, _jsonSettings);
|
||||||
var jsonString = JsonSerializer.Serialize(template, templateType, _jsonOptions);
|
|
||||||
return jsonString;
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@@ -53,8 +53,7 @@ public class SzParser
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var result = JsonSerializer.Deserialize<T>(jsonString, _jsonOptions);
|
return JsonConvert.DeserializeObject<T>(jsonString, _jsonSettings);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@@ -63,4 +62,3 @@ public class SzParser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user