Skip to content

Commit afa020b

Browse files
committed
More progress
1 parent 29299e5 commit afa020b

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

src/Foundatio.Mediator.SourceGenerator/HandlerGenerator.cs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ private static void GenerateHandleMethod(IndentedStringBuilder source, HandlerIn
175175
parameters = BuildParameters(source, m.Method.Parameters);
176176

177177
source.AppendLine($"{result}{asyncModifier}{accessor}.{m.Method.MethodName}({parameters});");
178+
179+
if (m.Method.ReturnType.IsHandlerResult)
180+
{
181+
result = handler.HasReturnValue ? $" ({handler.ReturnType.UnwrappedFullName}){m.Middleware.Identifier.ToCamelCase()}Result.Value!" : "";
182+
if (handler.ReturnType.IsResult)
183+
{
184+
result = $" {m.Middleware.Identifier.ToCamelCase()}Result.Value is Foundatio.Mediator.Result result ? ({handler.ReturnType.UnwrappedFullName})result : ({handler.ReturnType.UnwrappedFullName}?){m.Middleware.Identifier.ToCamelCase()}Result.Value ?? default({handler.ReturnType.UnwrappedFullName})!";
185+
}
186+
source.AppendLine($"if ({m.Middleware.Identifier.ToCamelCase()}Result.IsShortCircuited)");
187+
source.AppendLine("{");
188+
source.AppendLine($" return{result};");
189+
source.AppendLine("}");
190+
}
178191
}
179192
source.AppendLineIf(beforeMiddleware.Any());
180193

@@ -367,16 +380,16 @@ private static string BuildParameters(IndentedStringBuilder source, EquatableArr
367380
{
368381
var parameterValues = new List<string>();
369382

370-
const bool debugVariables = true;
383+
const bool outputDebugInfo = false;
371384

372385
foreach (var kvp in variables ?? [])
373386
{
374-
source.AppendLineIf($"// Variable: {kvp.Key} = {kvp.Value}", debugVariables);
387+
source.AppendLineIf($"// Variable: {kvp.Key} = {kvp.Value}", outputDebugInfo);
375388
}
376389

377390
foreach (var param in parameters)
378391
{
379-
source.AppendLineIf($"// Param: Name='{param.Name}', Type.FullName='{param.Type.FullName}', Type.UnwrappedFullName='{param.Type.UnwrappedFullName}', IsMessageParameter={param.IsMessageParameter}, Type.IsObject={param.Type.IsObject}, Type.IsCancellationToken={param.Type.IsCancellationToken}", debugVariables);
392+
source.AppendLineIf($"// Param: Name='{param.Name}', Type.FullName='{param.Type.FullName}', Type.UnwrappedFullName='{param.Type.UnwrappedFullName}', IsMessageParameter={param.IsMessageParameter}, Type.IsObject={param.Type.IsObject}, Type.IsCancellationToken={param.Type.IsCancellationToken}", outputDebugInfo);
380393

381394
if (param.IsMessageParameter)
382395
{
@@ -472,33 +485,6 @@ private static void GenerateGetOrCreateHandler(IndentedStringBuilder source, Han
472485
""");
473486
}
474487

475-
private static string GenerateShortCircuitCheck(MiddlewareInfo middleware, string resultVariableName, string hrVariableName)
476-
{
477-
if (middleware.BeforeMethod == null)
478-
return $"if ({resultVariableName} is Foundatio.Mediator.HandlerResult {hrVariableName} && {hrVariableName}.IsShortCircuited)";
479-
480-
var methodInfo = middleware.BeforeMethod.Value;
481-
482-
// Use the return type for the check
483-
string returnType = methodInfo.ReturnType.FullName;
484-
485-
// If the return type is exactly HandlerResult (not nullable), we can directly check it
486-
if (returnType == "HandlerResult" || returnType == "Foundatio.Mediator.HandlerResult")
487-
{
488-
return $"if ({resultVariableName}.IsShortCircuited)";
489-
}
490-
// If it's nullable HandlerResult, check for null first
491-
else if (returnType == "HandlerResult?" || returnType == "Foundatio.Mediator.HandlerResult?")
492-
{
493-
return $"if ({resultVariableName}?.IsShortCircuited == true)";
494-
}
495-
// Otherwise, fall back to pattern matching for object/object? return types
496-
else
497-
{
498-
return $"if ({resultVariableName} is Foundatio.Mediator.HandlerResult {hrVariableName} && {hrVariableName}.IsShortCircuited)";
499-
}
500-
}
501-
502488
private static void GenerateOptimizedTupleHandling(IndentedStringBuilder source, HandlerInfo handler, TypeSymbolInfo responseType)
503489
{
504490
var tupleFields = handler.ReturnType.TupleItems.ToList();

0 commit comments

Comments
 (0)