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
This commit is contained in:
parent
d52d60f652
commit
f45b872124
@ -2,5 +2,5 @@ namespace SessionZero.SzfLib.Objects;
|
||||
|
||||
public interface ISzfCharacterTemplate
|
||||
{
|
||||
|
||||
public List<ISzfDataset> LinkedDatasets { get; set; }
|
||||
}
|
@ -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);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
@ -6,6 +6,9 @@ namespace SessionZero.SzfLib.Objects;
|
||||
public class SzfCharacterTemplate : SzfObject, ISzfCharacterTemplate
|
||||
{
|
||||
public override string SzfType { get; set; } = "character_template";
|
||||
|
||||
public List<ISzfDataset> LinkedDatasets { get; set; } = new();
|
||||
|
||||
public override SzfError Validate()
|
||||
{
|
||||
var result = new SzfError();
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -13,6 +13,7 @@
|
||||
<RepositoryType>Git</RepositoryType>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<PackageIcon>logo_500x500.png</PackageIcon>
|
||||
<Version>0.1.0</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
Loading…
Reference in New Issue
Block a user