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
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 @@ -172,31 +173,67 @@ 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.EnterpriseIntegration.EventNotification.EventNotification<")
.Append(eventTypeName).Append(", ").Append(keyTypeName).Append("> ").Append(factoryMethodName).AppendLine("()");
sb.Append(memberIndent).AppendLine("{");
sb.Append(bodyIndent).Append("return global::PatternKit.EnterpriseIntegration.EventNotification.EventNotification<")
.Append(eventTypeName).Append(", ").Append(keyTypeName).Append(">.Create(\"").Append(Escape(notificationName)).AppendLine("\")");
if (!string.IsNullOrWhiteSpace(ruleName))
sb.Append(bodyIndent).Append(" .When(").Append(ruleName).AppendLine(")");
sb.Append(bodyIndent).Append(" .WithKey(").Append(keySelectorName).AppendLine(")");
if (!string.IsNullOrWhiteSpace(correlationSelectorName))
sb.Append(bodyIndent).Append(" .WithCorrelation(").Append(correlationSelectorName).AppendLine(")");
foreach (var item in metadata)
sb.Append(bodyIndent).Append(" .WithMetadata(\"").Append(Escape(item.Name)).Append("\", ").Append(item.Method.Name).AppendLine(")");
sb.Append(bodyIndent).AppendLine(" .Build();");
sb.AppendLine(memberIndent + "}");
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.EnterpriseIntegration.EventNotification.EventNotification<")
.Append(eventTypeName).Append(", ").Append(keyTypeName).Append("> ").Append(factoryMethodName).AppendLine("()");
sb.AppendLine(" {");
sb.Append(" return global::PatternKit.EnterpriseIntegration.EventNotification.EventNotification<")
.Append(eventTypeName).Append(", ").Append(keyTypeName).Append(">.Create(\"").Append(Escape(notificationName)).AppendLine("\")");
if (!string.IsNullOrWhiteSpace(ruleName))
sb.Append(" .When(").Append(ruleName).AppendLine(")");
sb.Append(" .WithKey(").Append(keySelectorName).AppendLine(")");
if (!string.IsNullOrWhiteSpace(correlationSelectorName))
sb.Append(" .WithCorrelation(").Append(correlationSelectorName).AppendLine(")");
foreach (var item in metadata)
sb.Append(" .WithMetadata(\"").Append(Escape(item.Name)).Append("\", ").Append(item.Method.Name).AppendLine(")");
sb.AppendLine(" .Build();");
sb.AppendLine(" }");
sb.AppendLine("}");
return sb.ToString();
sb.Append("partial ").Append(type.TypeKind == TypeKind.Struct ? "struct" : "class").Append(' ').Append(type.Name);
}

private static string? GetNamedString(AttributeData attribute, string name)
Expand Down
82 changes: 67 additions & 15 deletions src/PatternKit.Generators/Messaging/MessageEnvelopeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,31 +178,61 @@ private static string GenerateSource(
sb.AppendLine();
}

sb.Append("partial ").Append(GetKind(type)).Append(' ').Append(type.Name).AppendLine();
sb.AppendLine("{");
sb.Append(" public static global::PatternKit.Messaging.Message<").Append(payload).Append("> ").Append(config.FactoryName).Append('(').Append(payload).Append(" payload");
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.Message<").Append(payload).Append("> ").Append(config.FactoryName).Append('(').Append(payload).Append(" payload");
foreach (var header in headers)
sb.Append(", ").Append(header.ValueType).Append(' ').Append(header.ParameterName);
sb.AppendLine(")");
sb.Append(" => global::PatternKit.Messaging.Message<").Append(payload).AppendLine(">.Create(payload)");
sb.Append(bodyIndent).Append("=> global::PatternKit.Messaging.Message<").Append(payload).AppendLine(">.Create(payload)");
foreach (var header in headers)
sb.Append(" .WithHeader(\"").Append(Escape(header.Name)).Append("\", ").Append(header.ParameterName).AppendLine(")");
sb.AppendLine(" ;");
sb.Append(bodyIndent).Append(" .WithHeader(\"").Append(Escape(header.Name)).Append("\", ").Append(header.ParameterName).AppendLine(")");
sb.Append(bodyIndent).AppendLine(" ;");
sb.AppendLine();

sb.Append(" public static global::PatternKit.Messaging.MessageContext ").Append(config.ContextFactoryName)
sb.Append(memberIndent).Append("public static global::PatternKit.Messaging.MessageContext ").Append(config.ContextFactoryName)
.Append("(global::PatternKit.Messaging.Message<").Append(payload).Append("> message, global::System.Threading.CancellationToken cancellationToken = default)");
sb.AppendLine();
sb.AppendLine(" {");
sb.AppendLine(" if (message is null)");
sb.AppendLine(" throw new global::System.ArgumentNullException(nameof(message));");
sb.Append(memberIndent).AppendLine("{");
sb.Append(bodyIndent).AppendLine("if (message is null)");
sb.Append(bodyIndent).AppendLine(" throw new global::System.ArgumentNullException(nameof(message));");
sb.AppendLine();
sb.AppendLine(" return global::PatternKit.Messaging.MessageContext.From(message, cancellationToken);");
sb.AppendLine(" }");
sb.AppendLine("}");
sb.Append(bodyIndent).AppendLine("return global::PatternKit.Messaging.MessageContext.From(message, cancellationToken);");
sb.AppendLine(memberIndent + "}");
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 string ToParameterName(string headerName)
{
var sb = new StringBuilder();
Expand Down Expand Up @@ -231,8 +261,30 @@ private static bool IsValidIdentifier(string value)
&& SyntaxFacts.IsValidIdentifier(value)
&& SyntaxFacts.GetKeywordKind(value) == SyntaxKind.None;

private static string GetKind(INamedTypeSymbol type)
=> type.TypeKind == TypeKind.Struct ? "struct" : "class";
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);
}

private static string GetAccessibility(Accessibility accessibility)
=> accessibility switch
{
Accessibility.Public => "public",
Accessibility.Internal => "internal",
Accessibility.Private => "private",
Accessibility.Protected => "protected",
Accessibility.ProtectedAndInternal => "private protected",
Accessibility.ProtectedOrInternal => "protected internal",
_ => "internal"
};

private static string Escape(string value) => value.Replace("\\", "\\\\").Replace("\"", "\\\"");

Expand Down
Loading
Loading