Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 52 additions & 15 deletions src/PatternKit.Generators/Messaging/ClaimCheckGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -112,28 +113,64 @@ private static string GenerateSource(
sb.AppendLine();
}

var containingTypes = GetContainingTypes(type);
var indentLevel = 0;
foreach (var containingType in containingTypes)
{
AppendTypeDeclaration(sb, containingType, indentLevel);
sb.AppendLine();
sb.AppendLine(new string(' ', indentLevel * 4) + "{");
indentLevel++;
}

AppendTypeDeclaration(sb, type, indentLevel);
sb.AppendLine();
var indent = new string(' ', indentLevel * 4);
sb.AppendLine(indent + "{");
var memberIndent = indent + " ";
var bodyIndent = memberIndent + " ";
sb.Append(memberIndent).Append("public static global::PatternKit.Messaging.Transformation.ClaimCheck<")
.Append(payloadName).Append("> ").Append(factoryName).AppendLine("()");
sb.Append(bodyIndent).Append("=> global::PatternKit.Messaging.Transformation.ClaimCheck<")
.Append(payloadName).Append(">.Create(\"").Append(Escape(claimCheckName)).AppendLine("\")");
sb.Append(bodyIndent).Append(" .InStore(\"").Append(Escape(storeName)).AppendLine("\")");
sb.Append(bodyIndent).Append(" .UseStore(").Append(storeFactory).AppendLine("())");
sb.Append(bodyIndent).Append(" .UseClaimIds(static (message, _) => \"")
.Append(Escape(claimIdPrefix))
.Append(":\" + (message.Headers.MessageId ?? global::System.Guid.NewGuid().ToString(\"N\")))")
.AppendLine();
sb.Append(bodyIndent).AppendLine(" .Build();");
sb.AppendLine(indent + "}");
for (var i = containingTypes.Length - 1; i >= 0; i--)
{
sb.AppendLine(new string(' ', i * 4) + "}");
}

return sb.ToString();
}

private static INamedTypeSymbol[] GetContainingTypes(INamedTypeSymbol type)
{
var containingTypes = new Stack<INamedTypeSymbol>();
for (var current = type.ContainingType; current is not null; current = current.ContainingType)
{
containingTypes.Push(current);
}

return containingTypes.ToArray();
}

private static void AppendTypeDeclaration(StringBuilder sb, INamedTypeSymbol type, int indentLevel)
{
sb.Append(new string(' ', indentLevel * 4));
sb.Append(GetAccessibility(type.DeclaredAccessibility)).Append(' ');
if (type.IsStatic)
sb.Append("static ");
else if (type.IsAbstract && type.TypeKind == TypeKind.Class)
sb.Append("abstract ");
else if (type.IsSealed && type.TypeKind == TypeKind.Class)
sb.Append("sealed ");
sb.Append("partial ").Append(type.TypeKind == TypeKind.Struct ? "struct" : "class").Append(' ').Append(type.Name).AppendLine();
sb.AppendLine("{");
sb.Append(" public static global::PatternKit.Messaging.Transformation.ClaimCheck<")
.Append(payloadName).Append("> ").Append(factoryName).AppendLine("()");
sb.Append(" => global::PatternKit.Messaging.Transformation.ClaimCheck<")
.Append(payloadName).Append(">.Create(\"").Append(Escape(claimCheckName)).AppendLine("\")");
sb.Append(" .InStore(\"").Append(Escape(storeName)).AppendLine("\")");
sb.Append(" .UseStore(").Append(storeFactory).AppendLine("())");
sb.Append(" .UseClaimIds(static (message, _) => \"")
.Append(Escape(claimIdPrefix))
.Append(":\" + (message.Headers.MessageId ?? global::System.Guid.NewGuid().ToString(\"N\")))")
.AppendLine();
sb.AppendLine(" .Build();");
sb.AppendLine("}");
return sb.ToString();
sb.Append("partial ").Append(type.TypeKind == TypeKind.Struct ? "struct" : "class").Append(' ').Append(type.Name);
}

private static bool IsStoreFactory(IMethodSymbol method, ITypeSymbol payloadType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -119,25 +120,61 @@ private static string GenerateSource(
sb.AppendLine();
}

var containingTypes = GetContainingTypes(type);
var indentLevel = 0;
foreach (var containingType in containingTypes)
{
AppendTypeDeclaration(sb, containingType, indentLevel);
sb.AppendLine();
sb.AppendLine(new string(' ', indentLevel * 4) + "{");
indentLevel++;
}

AppendTypeDeclaration(sb, type, indentLevel);
sb.AppendLine();
var indent = new string(' ', indentLevel * 4);
sb.AppendLine(indent + "{");
var memberIndent = indent + " ";
var bodyIndent = memberIndent + " ";
sb.Append(memberIndent).Append("public static global::PatternKit.Application.TransactionScript.TransactionScript<")
.Append(requestName).Append(", ").Append(responseName).Append("> ").Append(factoryName).AppendLine("()");
sb.Append(bodyIndent).Append("=> global::PatternKit.Application.TransactionScript.TransactionScript<")
.Append(requestName).Append(", ").Append(responseName).Append(">.Create(\"").Append(Escape(scriptName)).Append("\")");
if (validatorName is not null)
sb.AppendLine().Append(bodyIndent).Append(" .Validate(").Append(validatorName).Append(')');
sb.AppendLine().Append(bodyIndent).Append(" .Execute(").Append(handlerName).AppendLine(")");
sb.Append(bodyIndent).AppendLine(" .Build();");
sb.AppendLine(indent + "}");
for (var i = containingTypes.Length - 1; i >= 0; i--)
{
sb.AppendLine(new string(' ', i * 4) + "}");
}

return sb.ToString();
}

private static INamedTypeSymbol[] GetContainingTypes(INamedTypeSymbol type)
{
var containingTypes = new Stack<INamedTypeSymbol>();
for (var current = type.ContainingType; current is not null; current = current.ContainingType)
{
containingTypes.Push(current);
}

return containingTypes.ToArray();
}

private static void AppendTypeDeclaration(StringBuilder sb, INamedTypeSymbol type, int indentLevel)
{
sb.Append(new string(' ', indentLevel * 4));
sb.Append(GetAccessibility(type.DeclaredAccessibility)).Append(' ');
if (type.IsStatic)
sb.Append("static ");
else if (type.IsAbstract && type.TypeKind == TypeKind.Class)
sb.Append("abstract ");
else if (type.IsSealed && type.TypeKind == TypeKind.Class)
sb.Append("sealed ");
sb.Append("partial ").Append(type.TypeKind == TypeKind.Struct ? "struct" : "class").Append(' ').Append(type.Name).AppendLine();
sb.AppendLine("{");
sb.Append(" public static global::PatternKit.Application.TransactionScript.TransactionScript<")
.Append(requestName).Append(", ").Append(responseName).Append("> ").Append(factoryName).AppendLine("()");
sb.Append(" => global::PatternKit.Application.TransactionScript.TransactionScript<")
.Append(requestName).Append(", ").Append(responseName).Append(">.Create(\"").Append(Escape(scriptName)).Append("\")");
if (validatorName is not null)
sb.AppendLine().Append(" .Validate(").Append(validatorName).Append(')');
sb.AppendLine().Append(" .Execute(").Append(handlerName).AppendLine(")");
sb.AppendLine(" .Build();");
sb.AppendLine("}");
return sb.ToString();
sb.Append("partial ").Append(type.TypeKind == TypeKind.Struct ? "struct" : "class").Append(' ').Append(type.Name);
}

private static bool IsHandler(IMethodSymbol method, INamedTypeSymbol requestType, INamedTypeSymbol responseType)
Expand Down
Loading
Loading