SessionZeroWasm/SessionZero/Data/SzfGenerator.cs

121 lines
3.3 KiB
C#

using System.Reflection;
using System.Text;
namespace SessionZero.Data;
public class SzfGenerator
{
private readonly SzfGeneratorOptions _options;
public SzfGenerator(SzfGeneratorOptions? options = null)
{
_options = options ?? new SzfGeneratorOptions();
}
/// <summary>
/// Generate SZF content from any SzfObject
/// </summary>
public string Generate(SzfObject obj)
{
var builder = new StringBuilder();
var writer = new SzfFieldWriter(builder, _options);
// Generate header
GenerateHeader(writer, obj);
// Generate metadata section
GenerateMetadataSection(writer, obj);
// Generate type-specific content
GenerateTypeSpecificContent(writer, obj);
return builder.ToString();
}
/// <summary>
/// Generate the SZF header (!type: and !schema:)
/// </summary>
private void GenerateHeader(SzfFieldWriter writer, SzfObject obj)
{
var type = obj.GetType();
var attribute = type.GetCustomAttribute<SzfObjectAttribute>();
if (attribute == null)
{
throw new SzfGenerateException(
$"Type {type.Name} does not have SzfObjectAttribute, cannot determine type string.");
}
writer.Builder.AppendLine($"!type: {attribute.TypeString}");
writer.Builder.AppendLine($"!schema: {obj.SchemaVersion}");
writer.Builder.AppendLine();
}
/// <summary>
/// Generate the common metadata section
/// </summary>
private void GenerateMetadataSection(SzfFieldWriter writer, SzfObject obj)
{
writer.AppendSectionHeader("Metadata");
// Standard metadata fields
writer.WriteField("Name", "text", obj.Name);
writer.WriteField("Version", "text", obj.Version);
writer.WriteField("GUID", "text", obj.Id.ToString());
writer.WriteField("Description", "text-field", obj.Description);
writer.WriteField("Author", "text", obj.Author);
// Type-specific metadata delegated to the object itself
obj.GenerateMetadata(writer);
writer.AppendLine();
}
/// <summary>
/// Generate type-specific content sections delegated to the object itself
/// </summary>
private void GenerateTypeSpecificContent(SzfFieldWriter writer, SzfObject obj)
{
obj.GenerateContent(writer);
}
}
/// <summary>
/// Configuration options for SZF generation
/// </summary>
public class SzfGeneratorOptions
{
/// <summary>
/// Whether to indent field lines within sections
/// </summary>
public bool IndentFields { get; set; } = false;
/// <summary>
/// Indent string to use for fields (if IndentFields is true)
/// </summary>
public string Indent { get; set; } = " ";
/// <summary>
/// Whether to include empty sections
/// </summary>
public bool IncludeEmptySections { get; set; } = false;
/// <summary>
/// Whether to include fields with null/empty values
/// </summary>
public bool IncludeEmptyFields { get; set; } = true;
}
/// <summary>
/// Exception thrown during SZF generation
/// </summary>
public class SzfGenerateException : Exception
{
public SzfGenerateException(string message) : base(message)
{
}
public SzfGenerateException(string message, Exception innerException) : base(message, innerException)
{
}
}