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
68 changes: 48 additions & 20 deletions src/PatternKit.Generators/Messaging/MessageTranslatorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private static void Generate(SourceProductionContext context, INamedTypeSymbol t

var inputType = attribute.ConstructorArguments.Length >= 1 ? attribute.ConstructorArguments[0].Value as INamedTypeSymbol : null;
var outputType = attribute.ConstructorArguments.Length >= 2 ? attribute.ConstructorArguments[1].Value as INamedTypeSymbol : null;
if (inputType is null || outputType is null)
if (inputType is null || outputType is null || inputType.TypeKind == TypeKind.Error || outputType.TypeKind == TypeKind.Error)
return;

var handlers = type.GetMembers().OfType<IMethodSymbol>()
Expand Down Expand Up @@ -117,31 +117,39 @@ private static string GenerateSource(
sb.AppendLine();
}

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.MessageTranslator<")
var indent = string.Empty;
foreach (var containingType in GetContainingTypes(type))
{
AppendTypeDeclaration(sb, containingType, indent);
sb.Append(indent).AppendLine("{");
indent += " ";
}

AppendTypeDeclaration(sb, type, indent);
sb.Append(indent).AppendLine("{");
var memberIndent = indent + " ";
var bodyIndent = memberIndent + " ";
sb.Append(memberIndent).Append("public static global::PatternKit.Messaging.Transformation.MessageTranslator<")
.Append(inputName).Append(", ").Append(outputName).Append("> ").Append(factoryName).AppendLine("()");
sb.AppendLine(" {");
sb.Append(" var builder = global::PatternKit.Messaging.Transformation.MessageTranslator<")
sb.Append(memberIndent).AppendLine("{");
sb.Append(bodyIndent).Append("var builder = global::PatternKit.Messaging.Transformation.MessageTranslator<")
.Append(inputName).Append(", ").Append(outputName).Append(">.Create(\"").Append(Escape(translatorName)).AppendLine("\")");
sb.Append(" .PreserveHeaders(").Append(preserveHeaders ? "true" : "false").AppendLine(")");
sb.Append(" .TranslateWith(static (message, context) => ").Append(handlerName).AppendLine("(message, context));");
sb.Append(bodyIndent).Append(" .PreserveHeaders(").Append(preserveHeaders ? "true" : "false").AppendLine(")");
sb.Append(bodyIndent).Append(" .TranslateWith(static (message, context) => ").Append(handlerName).AppendLine("(message, context));");

foreach (var drop in drops)
sb.Append(" builder.DropHeader(\"").Append(Escape(drop)).AppendLine("\");");
sb.Append(bodyIndent).Append("builder.DropHeader(\"").Append(Escape(drop)).AppendLine("\");");
foreach (var set in sets)
sb.Append(" builder.SetHeader(\"").Append(Escape(set.Name)).Append("\", \"").Append(Escape(set.Value)).AppendLine("\");");
sb.Append(bodyIndent).Append("builder.SetHeader(\"").Append(Escape(set.Name)).Append("\", \"").Append(Escape(set.Value)).AppendLine("\");");

sb.AppendLine(" return builder.Build();");
sb.AppendLine(" }");
sb.AppendLine("}");
sb.Append(bodyIndent).AppendLine("return builder.Build();");
sb.Append(memberIndent).AppendLine("}");
sb.Append(indent).AppendLine("}");
while (indent.Length > 0)
{
indent = indent.Substring(0, indent.Length - 4);
sb.Append(indent).AppendLine("}");
}
return sb.ToString();
}

Expand Down Expand Up @@ -175,6 +183,26 @@ private static IReadOnlyList<HeaderAssignment> GetSetHeaders(INamedTypeSymbol ty
.Where(static header => !string.IsNullOrWhiteSpace(header.Name))
.ToArray();

private static IReadOnlyList<INamedTypeSymbol> GetContainingTypes(INamedTypeSymbol type)
{
var stack = new Stack<INamedTypeSymbol>();
for (var current = type.ContainingType; current is not null; current = current.ContainingType)
stack.Push(current);
return stack.ToArray();
}

private static void AppendTypeDeclaration(StringBuilder sb, INamedTypeSymbol type, string indent)
{
sb.Append(indent).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();
}

private static string? GetNamedString(AttributeData attribute, string name)
=> attribute.NamedArguments.FirstOrDefault(kv => kv.Key == name).Value.Value as string;

Expand Down
57 changes: 43 additions & 14 deletions src/PatternKit.Generators/PriorityQueue/PriorityQueueGenerator.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 @@ -58,7 +59,7 @@ private static void Generate(SourceProductionContext context, INamedTypeSymbol t

var itemType = attribute.ConstructorArguments.Length >= 1 ? attribute.ConstructorArguments[0].Value as INamedTypeSymbol : null;
var priorityType = attribute.ConstructorArguments.Length >= 2 ? attribute.ConstructorArguments[1].Value as INamedTypeSymbol : null;
if (itemType is null || priorityType is null)
if (itemType is null || priorityType is null || itemType.TypeKind == TypeKind.Error || priorityType.TypeKind == TypeKind.Error)
return;

var selectors = type.GetMembers().OfType<IMethodSymbol>().Where(static method =>
Expand Down Expand Up @@ -113,26 +114,54 @@ private static string GenerateSource(
sb.AppendLine();
}

sb.Append(GetAccessibility(type.DeclaredAccessibility)).Append(' ');
var indent = string.Empty;
foreach (var containingType in GetContainingTypes(type))
{
AppendTypeDeclaration(sb, containingType, indent);
sb.Append(indent).AppendLine("{");
indent += " ";
}

AppendTypeDeclaration(sb, type, indent);
sb.Append(indent).AppendLine("{");
var memberIndent = indent + " ";
var bodyIndent = memberIndent + " ";
sb.Append(memberIndent).Append("public static global::PatternKit.Cloud.PriorityQueue.PriorityQueuePolicy<").Append(itemTypeName).Append(", ").Append(priorityTypeName).Append("> ").Append(factoryMethodName).AppendLine("()");
sb.Append(memberIndent).AppendLine("{");
sb.Append(bodyIndent).Append("return global::PatternKit.Cloud.PriorityQueue.PriorityQueuePolicy<").Append(itemTypeName).Append(", ").Append(priorityTypeName).Append(">.Create(\"").Append(Escape(queueName)).AppendLine("\")");
sb.Append(bodyIndent).Append(" .WithPrioritySelector(").Append(selectorName).AppendLine(")");
sb.Append(bodyIndent).AppendLine(dequeueHighestPriorityFirst
? " .DequeueHighestPriorityFirst()"
: " .DequeueLowestPriorityFirst()");
sb.Append(bodyIndent).AppendLine(" .Build();");
sb.Append(memberIndent).AppendLine("}");
sb.Append(indent).AppendLine("}");
while (indent.Length > 0)
{
indent = indent.Substring(0, indent.Length - 4);
sb.Append(indent).AppendLine("}");
}
return sb.ToString();
}

private static IReadOnlyList<INamedTypeSymbol> GetContainingTypes(INamedTypeSymbol type)
{
var stack = new Stack<INamedTypeSymbol>();
for (var current = type.ContainingType; current is not null; current = current.ContainingType)
stack.Push(current);
return stack.ToArray();
}

private static void AppendTypeDeclaration(StringBuilder sb, INamedTypeSymbol type, string indent)
{
sb.Append(indent).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.Cloud.PriorityQueue.PriorityQueuePolicy<").Append(itemTypeName).Append(", ").Append(priorityTypeName).Append("> ").Append(factoryMethodName).AppendLine("()");
sb.AppendLine(" {");
sb.Append(" return global::PatternKit.Cloud.PriorityQueue.PriorityQueuePolicy<").Append(itemTypeName).Append(", ").Append(priorityTypeName).Append(">.Create(\"").Append(Escape(queueName)).AppendLine("\")");
sb.Append(" .WithPrioritySelector(").Append(selectorName).AppendLine(")");
sb.AppendLine(dequeueHighestPriorityFirst
? " .DequeueHighestPriorityFirst()"
: " .DequeueLowestPriorityFirst()");
sb.AppendLine(" .Build();");
sb.AppendLine(" }");
sb.AppendLine("}");
return sb.ToString();
}

private static string Escape(string value) => value.Replace("\\", "\\\\").Replace("\"", "\\\"");
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 @@ -51,7 +52,7 @@ private static void Generate(SourceProductionContext context, INamedTypeSymbol t
}

var resultType = attribute.ConstructorArguments.Length >= 1 ? attribute.ConstructorArguments[0].Value as INamedTypeSymbol : null;
if (resultType is null)
if (resultType is null || resultType.TypeKind == TypeKind.Error)
return;

var maxConcurrentWorkers = GetNamedInt(attribute, "MaxConcurrentWorkers") ?? 1;
Expand Down Expand Up @@ -96,25 +97,53 @@ private static string GenerateSource(
sb.AppendLine();
}

sb.Append(GetAccessibility(type.DeclaredAccessibility)).Append(' ');
var indent = string.Empty;
foreach (var containingType in GetContainingTypes(type))
{
AppendTypeDeclaration(sb, containingType, indent);
sb.Append(indent).AppendLine("{");
indent += " ";
}

AppendTypeDeclaration(sb, type, indent);
sb.Append(indent).AppendLine("{");
var memberIndent = indent + " ";
var bodyIndent = memberIndent + " ";
sb.Append(memberIndent).Append("public static global::PatternKit.Cloud.QueueLoadLeveling.QueueLoadLevelingPolicy<").Append(resultTypeName).Append("> ").Append(factoryMethodName).AppendLine("()");
sb.Append(memberIndent).AppendLine("{");
sb.Append(bodyIndent).Append("return global::PatternKit.Cloud.QueueLoadLeveling.QueueLoadLevelingPolicy<").Append(resultTypeName).Append(">.Create(\"").Append(Escape(policyName)).AppendLine("\")");
sb.Append(bodyIndent).Append(" .WithMaxConcurrentWorkers(").Append(maxConcurrentWorkers).AppendLine(")");
sb.Append(bodyIndent).Append(" .WithMaxQueueLength(").Append(maxQueueLength).AppendLine(")");
sb.Append(bodyIndent).Append(" .WithQueueTimeout(global::System.TimeSpan.FromMilliseconds(").Append(queueTimeoutMilliseconds).AppendLine("))");
sb.Append(bodyIndent).AppendLine(" .Build();");
sb.Append(memberIndent).AppendLine("}");
sb.Append(indent).AppendLine("}");
while (indent.Length > 0)
{
indent = indent.Substring(0, indent.Length - 4);
sb.Append(indent).AppendLine("}");
}
return sb.ToString();
}

private static IReadOnlyList<INamedTypeSymbol> GetContainingTypes(INamedTypeSymbol type)
{
var stack = new Stack<INamedTypeSymbol>();
for (var current = type.ContainingType; current is not null; current = current.ContainingType)
stack.Push(current);
return stack.ToArray();
}

private static void AppendTypeDeclaration(StringBuilder sb, INamedTypeSymbol type, string indent)
{
sb.Append(indent).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.Cloud.QueueLoadLeveling.QueueLoadLevelingPolicy<").Append(resultTypeName).Append("> ").Append(factoryMethodName).AppendLine("()");
sb.AppendLine(" {");
sb.Append(" return global::PatternKit.Cloud.QueueLoadLeveling.QueueLoadLevelingPolicy<").Append(resultTypeName).Append(">.Create(\"").Append(Escape(policyName)).AppendLine("\")");
sb.Append(" .WithMaxConcurrentWorkers(").Append(maxConcurrentWorkers).AppendLine(")");
sb.Append(" .WithMaxQueueLength(").Append(maxQueueLength).AppendLine(")");
sb.Append(" .WithQueueTimeout(global::System.TimeSpan.FromMilliseconds(").Append(queueTimeoutMilliseconds).AppendLine("))");
sb.AppendLine(" .Build();");
sb.AppendLine(" }");
sb.AppendLine("}");
return sb.ToString();
}

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