Adding per object validation
This commit is contained in:
parent
69df933609
commit
f00bde7e24
@ -1,6 +1,7 @@
|
||||
using Spectre.Console;
|
||||
using SessionZero.SzfLib.Parser;
|
||||
using System.IO;
|
||||
using SessionZero.SzfLib.Helpers;
|
||||
|
||||
void PrintSection(dynamic section, int indent = 0)
|
||||
{
|
||||
@ -55,19 +56,27 @@ if (!File.Exists(filePath))
|
||||
|
||||
string szfContent = File.ReadAllText(filePath);
|
||||
var parser = new SzfParser();
|
||||
var szfObject = parser.Parse(szfContent);
|
||||
|
||||
if (szfObject == null)
|
||||
try
|
||||
{
|
||||
var szfObject = parser.Parse(szfContent);
|
||||
|
||||
if (szfObject == null)
|
||||
{
|
||||
AnsiConsole.MarkupLine("[red]Failed to parse SZF file.[/]");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
AnsiConsole.MarkupLine($"[bold]SZF Type:[/] {szfObject.SzfType}");
|
||||
AnsiConsole.MarkupLine($"[bold]SZF Version:[/] {szfObject.SzfVersion}");
|
||||
AnsiConsole.MarkupLine($"[bold]Number of Sections:[/] {szfObject.Sections.Count}");
|
||||
AnsiConsole.MarkupLine($"[bold]SZF Type:[/] {szfObject.SzfType}");
|
||||
AnsiConsole.MarkupLine($"[bold]SZF Version:[/] {szfObject.SzfVersion}");
|
||||
AnsiConsole.MarkupLine($"[bold]Number of Sections:[/] {szfObject.Sections.Count}");
|
||||
|
||||
foreach (var section in szfObject.Sections)
|
||||
{
|
||||
foreach (var section in szfObject.Sections)
|
||||
{
|
||||
PrintSection(section);
|
||||
}
|
||||
}
|
||||
catch (SzfError ex)
|
||||
{
|
||||
Console.WriteLine($"Szf Validation Error: {ex.Message}");
|
||||
}
|
||||
|
||||
|
18
SessionZero.SzfLib/Helpers/SzfError.cs
Normal file
18
SessionZero.SzfLib/Helpers/SzfError.cs
Normal file
@ -0,0 +1,18 @@
|
||||
namespace SessionZero.SzfLib.Helpers;
|
||||
|
||||
public class SzfError : Exception
|
||||
{
|
||||
public bool IsValid { get; set; } = true;
|
||||
public string Message { get; set; } = string.Empty;
|
||||
|
||||
public void AddError(string message)
|
||||
{
|
||||
Message += message + "\r\n";
|
||||
IsValid = false;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Valid: {IsValid}, Message: {Message}";
|
||||
}
|
||||
}
|
@ -16,4 +16,16 @@ public static class SzfHelper
|
||||
|
||||
return obj.IsAbstract || obj.IsInterface ? null : obj;
|
||||
}
|
||||
|
||||
public static SzfError BasicSzfValidation(ISzfObject obj)
|
||||
{
|
||||
SzfError result = new SzfError();
|
||||
|
||||
var metadataSection = obj.Sections.FirstOrDefault(s => s.Name == "Metadata");
|
||||
if (metadataSection is null) result.AddError("No metadata section found");
|
||||
|
||||
if (obj.GetMetadataField("Name") == string.Empty) result.AddError("No metadata field name found");
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using SessionZero.SzfLib.Helpers;
|
||||
using SessionZero.SzfLib.Parser;
|
||||
|
||||
namespace SessionZero.SzfLib.Objects;
|
||||
@ -12,5 +13,9 @@ public interface ISzfObject
|
||||
public string GetFieldValue(string sectionName, string fieldName);
|
||||
public string FindFieldValueInSection(SzfSection section, string[] path, int depth, string fieldName);
|
||||
|
||||
public ISzfObject? Parse();
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public SzfError Validate();
|
||||
}
|
@ -1,7 +1,13 @@
|
||||
using SessionZero.SzfLib.Helpers;
|
||||
|
||||
namespace SessionZero.SzfLib.Objects;
|
||||
|
||||
[SzfObject("character")]
|
||||
public class SzfCharacter : SzfObject, ISzfCharacter
|
||||
{
|
||||
public override string SzfType { get; set; } = "character";
|
||||
public override SzfError Validate()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
@ -1,7 +1,13 @@
|
||||
using SessionZero.SzfLib.Helpers;
|
||||
|
||||
namespace SessionZero.SzfLib.Objects;
|
||||
|
||||
[SzfObject("character_template")]
|
||||
public class SzfCharacterTemplate : SzfObject, ISzfCharacterTemplate
|
||||
{
|
||||
public override string SzfType { get; set; } = "character_template";
|
||||
public override SzfError Validate()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
@ -1,7 +1,25 @@
|
||||
using SessionZero.SzfLib.Helpers;
|
||||
using SessionZero.SzfLib.Parser;
|
||||
|
||||
namespace SessionZero.SzfLib.Objects;
|
||||
|
||||
[SzfObject("dataset")]
|
||||
public class SzfDataset : SzfObject, ISzfDataset
|
||||
{
|
||||
public override string SzfType { get; set; } = "dataset";
|
||||
|
||||
|
||||
public override SzfError Validate()
|
||||
{
|
||||
var result = SzfHelper.BasicSzfValidation(this);
|
||||
|
||||
if (GetMetadataField("Type") == string.Empty) result.AddError("Metadata field `Type` is missing or empty");
|
||||
|
||||
foreach (var entry in Sections.Where(entry => GetFieldValue(entry.Name, "Name") == string.Empty))
|
||||
{
|
||||
result.AddError($"Entry {entry.Name}'s `Name` field is missing or empty");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using SessionZero.SzfLib.Helpers;
|
||||
using SessionZero.SzfLib.Parser;
|
||||
|
||||
namespace SessionZero.SzfLib.Objects;
|
||||
@ -13,9 +14,10 @@ public abstract class SzfObject : ISzfObject
|
||||
/// Handles specific parsing logic for the SzfObject that overrides this method.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual ISzfObject? Parse()
|
||||
public virtual SzfError Validate()
|
||||
{
|
||||
return this;
|
||||
var result = SzfHelper.BasicSzfValidation(this);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
13
SessionZero.SzfLib/Parser/SzfField.cs
Normal file
13
SessionZero.SzfLib/Parser/SzfField.cs
Normal file
@ -0,0 +1,13 @@
|
||||
namespace SessionZero.SzfLib.Parser;
|
||||
|
||||
public class SzfField(string name, SzfFieldType type)
|
||||
{
|
||||
public string Name { get; set; } = name;
|
||||
public SzfFieldType SzfType { get; set; } = type;
|
||||
public string? Value { get; set; }
|
||||
|
||||
public bool Validate()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
13
SessionZero.SzfLib/Parser/SzfFieldType.cs
Normal file
13
SessionZero.SzfLib/Parser/SzfFieldType.cs
Normal file
@ -0,0 +1,13 @@
|
||||
namespace SessionZero.SzfLib.Parser;
|
||||
|
||||
public enum SzfFieldType
|
||||
{
|
||||
Text,
|
||||
TextField,
|
||||
Number,
|
||||
Bool,
|
||||
Calculated,
|
||||
System,
|
||||
EntryReference,
|
||||
EntryReferenceList,
|
||||
}
|
@ -7,12 +7,15 @@ namespace SessionZero.SzfLib.Parser;
|
||||
public class SzfParser : ISzfParser
|
||||
{
|
||||
/// <summary>
|
||||
/// Parses the given SZF content and returns the corresponding ISzfObject.
|
||||
/// Tries to parse an ISzfObject
|
||||
/// </summary>
|
||||
/// <param name="szfContent"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="SzfError"></exception>
|
||||
public ISzfObject? Parse(string szfContent)
|
||||
{
|
||||
SzfError result = new();
|
||||
|
||||
var lines = szfContent.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
string szfObjectTypeName = string.Empty;
|
||||
@ -73,10 +76,14 @@ public class SzfParser : ISzfParser
|
||||
szfObject.Sections.Add(section);
|
||||
}
|
||||
|
||||
return szfObject.Parse() ?? szfObject;
|
||||
result = szfObject.Validate();
|
||||
|
||||
if (result.IsValid) return szfObject;
|
||||
|
||||
Console.WriteLine(result.Message);
|
||||
}
|
||||
|
||||
return null;
|
||||
throw result;
|
||||
}
|
||||
|
||||
public ISzfObject? Parse(ISzfFile file)
|
||||
@ -144,30 +151,3 @@ public class SzfParser : ISzfParser
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class SzfSection(string name)
|
||||
{
|
||||
public string Name { get; set; } = name;
|
||||
public Dictionary<string, SzfField> Fields { get; set; } = new();
|
||||
public Dictionary<string, SzfSection> Subsections { get; set; } = new();
|
||||
}
|
||||
|
||||
|
||||
public class SzfField(string name, SzfFieldType type)
|
||||
{
|
||||
public string Name { get; set; } = name;
|
||||
public SzfFieldType SzfType { get; set; } = type;
|
||||
public string? Value { get; set; }
|
||||
}
|
||||
|
||||
public enum SzfFieldType
|
||||
{
|
||||
Text,
|
||||
TextField,
|
||||
Number,
|
||||
Bool,
|
||||
Calculated,
|
||||
System,
|
||||
EntryReference,
|
||||
EntryReferenceList,
|
||||
}
|
8
SessionZero.SzfLib/Parser/SzfSection.cs
Normal file
8
SessionZero.SzfLib/Parser/SzfSection.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace SessionZero.SzfLib.Parser;
|
||||
|
||||
public class SzfSection(string name)
|
||||
{
|
||||
public string Name { get; set; } = name;
|
||||
public Dictionary<string, SzfField> Fields { get; set; } = new();
|
||||
public Dictionary<string, SzfSection> Subsections { get; set; } = new();
|
||||
}
|
Loading…
Reference in New Issue
Block a user