Skip to content

Commit

Permalink
Implment Python Serializer and add Comment and Identation to AppSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanGreve committed Mar 30, 2024
1 parent 9192f6b commit 096d728
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions JackTheEnumRipper/Models/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
public record AppSettings
{
public required string Encoding { get; set; }

public string? Comment { get; set; }

public string? Indentation { get; set; }
}
}
22 changes: 21 additions & 1 deletion JackTheEnumRipper/Serializer/PythonSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using JackTheEnumRipper.Interfaces;
Expand All @@ -19,8 +20,27 @@ public class PythonSerializer(IOptions<AppSettings> appSettings) : ISerializer
public void Serialize(IEnumerable<AbstractEnum> enums, string path)
{
var builder = new StringBuilder();
string identation = this._appSettings.Indentation ?? "\t";

// TODO
if (!string.IsNullOrEmpty(this._appSettings.Comment))
{
builder.AppendLine($"# {this._appSettings.Comment}");
}

builder.AppendLine($"from enum import Enum, unique");

foreach (AbstractEnum @enum in enums)
{
builder.AppendLine(Environment.NewLine);
builder.AppendLine($"# namespace={@enum.Namespace},scope={(@enum.IsPublic ? "public" : "internal")},type={@enum.Type}");
builder.AppendLine($"@unique");
builder.AppendLine($"class {@enum.Name}(Enum):");

foreach (AbstractField field in @enum.Fields)
{
builder.AppendLine($"{identation}{field.Name.ToUpper()} = {field.Value}");
}
}

var encoding = Encoding.GetEncoding(this._appSettings.Encoding);
string content = builder.ToString();
Expand Down
4 changes: 3 additions & 1 deletion JackTheEnumRipper/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"AppSettings": {
"Encoding": "utf-8"
"Encoding": "utf-8",
"Comment": "This code was generated by JackTheEnumRipper. Changes to this file will be lost if the code is regenerated.",
"Indentation": "\t"
},
"NLog": {
"extensions": [
Expand Down

0 comments on commit 096d728

Please sign in to comment.