From f45b872124046936baa7fa2f88943384fc7ea190 Mon Sep 17 00:00:00 2001 From: chrisbell Date: Tue, 9 Sep 2025 10:50:30 -0500 Subject: [PATCH] Added ability to set field values on an SzfObject & the SzfParser will generate and set a new GUID if one is not provided or is invalid --- .../Objects/ISzfCharacterTemplate.cs | 2 +- SessionZero.SzfLib/Objects/ISzfObject.cs | 5 +- .../Objects/SzfCharacterTemplate.cs | 3 + SessionZero.SzfLib/Objects/SzfObject.cs | 64 ++++++++++++++++++- SessionZero.SzfLib/Parser/SzfParser.cs | 12 +++- SessionZero.SzfLib/SessionZero.SzfLib.csproj | 1 + 6 files changed, 83 insertions(+), 4 deletions(-) diff --git a/SessionZero.SzfLib/Objects/ISzfCharacterTemplate.cs b/SessionZero.SzfLib/Objects/ISzfCharacterTemplate.cs index d48b69d..4be7154 100644 --- a/SessionZero.SzfLib/Objects/ISzfCharacterTemplate.cs +++ b/SessionZero.SzfLib/Objects/ISzfCharacterTemplate.cs @@ -2,5 +2,5 @@ namespace SessionZero.SzfLib.Objects; public interface ISzfCharacterTemplate { - + public List LinkedDatasets { get; set; } } \ No newline at end of file diff --git a/SessionZero.SzfLib/Objects/ISzfObject.cs b/SessionZero.SzfLib/Objects/ISzfObject.cs index 77cb7a5..1626209 100644 --- a/SessionZero.SzfLib/Objects/ISzfObject.cs +++ b/SessionZero.SzfLib/Objects/ISzfObject.cs @@ -12,7 +12,10 @@ public interface ISzfObject public string GetMetadataField(string fieldName); public string GetFieldValue(string sectionName, string fieldName); public string FindFieldValueInSection(SzfSection section, string[] path, int depth, string fieldName); - + public void SetMetadataField(string fieldName, string value); + public void SetFieldValueInSection(SzfSection section, string[] path, int depth, string fieldName, object value); + public void SetFieldValue(string sectionName, string fieldName, object value); + /// /// /// diff --git a/SessionZero.SzfLib/Objects/SzfCharacterTemplate.cs b/SessionZero.SzfLib/Objects/SzfCharacterTemplate.cs index 6dd5978..1baf550 100644 --- a/SessionZero.SzfLib/Objects/SzfCharacterTemplate.cs +++ b/SessionZero.SzfLib/Objects/SzfCharacterTemplate.cs @@ -6,6 +6,9 @@ namespace SessionZero.SzfLib.Objects; public class SzfCharacterTemplate : SzfObject, ISzfCharacterTemplate { public override string SzfType { get; set; } = "character_template"; + + public List LinkedDatasets { get; set; } = new(); + public override SzfError Validate() { var result = new SzfError(); diff --git a/SessionZero.SzfLib/Objects/SzfObject.cs b/SessionZero.SzfLib/Objects/SzfObject.cs index 99a3249..d6ae911 100644 --- a/SessionZero.SzfLib/Objects/SzfObject.cs +++ b/SessionZero.SzfLib/Objects/SzfObject.cs @@ -85,5 +85,67 @@ public abstract class SzfObject : ISzfObject return string.Empty; } - + + public void SetMetadataField(string fieldName, string value) + { + if (string.IsNullOrEmpty(fieldName)) + { + throw new ArgumentException("Field name cannot be null or empty.", nameof(fieldName)); + } + + SetFieldValue("Metadata", fieldName, value); + } + + public void SetFieldValueInSection(SzfSection section, string[] path, int depth, string fieldName, object value) + { + if (section.Name != path[depth]) return; + + if (depth == path.Length - 1) + { + if (section.Fields.TryGetValue(fieldName, out var field) && field is SzfField szfField) + { + szfField.Value = value?.ToString(); + } + else + { + throw new InvalidOperationException( + $"Field '{fieldName}' does not exist in section '{section.Name}'."); + } + return; + } + + if (section.Subsections.TryGetValue(path[depth + 1], out var subsection)) + { + SetFieldValueInSection(subsection, path, depth + 1, fieldName, value); + } + else + { + throw new InvalidOperationException( + $"Subsection '{path[depth + 1]}' does not exist in section '{section.Name}'."); + } + } + + public void SetFieldValue(string sectionName, string fieldName, object value) + { + if (string.IsNullOrEmpty(sectionName)) + { + throw new ArgumentException("Section name cannot be null or empty.", nameof(sectionName)); + } + + if (string.IsNullOrEmpty(fieldName)) + { + throw new ArgumentException("Field name cannot be null or empty.", nameof(fieldName)); + } + + var sectionPath = sectionName.Split('.'); + + var rootSection = Sections.FirstOrDefault(s => s.Name == sectionPath[0]); + if (rootSection == null) + { + throw new InvalidOperationException( + $"Section '{sectionPath[0]}' does not exist."); + } + + SetFieldValueInSection(rootSection, sectionPath, 0, fieldName, value); + } } \ No newline at end of file diff --git a/SessionZero.SzfLib/Parser/SzfParser.cs b/SessionZero.SzfLib/Parser/SzfParser.cs index ab109cf..7ca7581 100644 --- a/SessionZero.SzfLib/Parser/SzfParser.cs +++ b/SessionZero.SzfLib/Parser/SzfParser.cs @@ -78,7 +78,17 @@ public class SzfParser : ISzfParser result = szfObject.Validate(); - if (result.IsValid) return szfObject; + if (result.IsValid) + { + var guid = szfObject.GetMetadataField("Guid"); + + if (string.IsNullOrEmpty(guid) || !Guid.TryParse(guid, out var res)) + { + szfObject.SetMetadataField("Guid", Guid.NewGuid().ToString()); + } + + return szfObject; + } } throw result; diff --git a/SessionZero.SzfLib/SessionZero.SzfLib.csproj b/SessionZero.SzfLib/SessionZero.SzfLib.csproj index 8284a14..4cdb089 100644 --- a/SessionZero.SzfLib/SessionZero.SzfLib.csproj +++ b/SessionZero.SzfLib/SessionZero.SzfLib.csproj @@ -13,6 +13,7 @@ Git true logo_500x500.png + 0.1.0