SessionZeroWasm/SessionZero/Data/SzfGenerator.cs

87 lines
2.5 KiB
C#

using System.Reflection;
using System.Text;
using System; // Required for Exception
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 type-specific metadata and content
// These methods delegate to the SzfObject itself,
// which now correctly accesses its own Metadata.
obj.GenerateMetadata(writer); // This now handles writing the standard metadata section
obj.GenerateContent(writer); // This handles writing object-specific content sections
return builder.ToString();
}
/// <summary>
/// Generate the SZF header (!type: and !schema:)
/// </summary>
private void GenerateHeader(SzfFieldWriter writer, SzfObject obj)
{
// TypeIdentifier is now directly accessible from the SzfObject base class
writer.Builder.AppendLine($"!type: {obj.TypeIdentifier}");
// Schema version is a fixed format version, not object-specific metadata
writer.Builder.AppendLine($"!schema: 1.0.0"); // Assuming current schema version is 1.0.0
writer.Builder.AppendLine();
}
}
/// <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)
{
}
}