From bc31fd74f159bb67b712a0eca1416abfba1edec1 Mon Sep 17 00:00:00 2001 From: Stuart Turner Date: Fri, 5 Jan 2024 12:36:27 -0600 Subject: [PATCH] Implement `TransformRenderMode` --- .editorconfig | 1 + samples/Normal/Class1.cs | 50 +++++++++++++++++++ src/Immediate.Handlers.Utility/Behavior.cs | 10 +++- .../Generators/ImmediateHandlersGenerator.cs | 12 ++++- 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 samples/Normal/Class1.cs diff --git a/.editorconfig b/.editorconfig index 013544f4..647642f3 100644 --- a/.editorconfig +++ b/.editorconfig @@ -215,6 +215,7 @@ dotnet_diagnostic.CS1712.severity = none # CS1712: Type parameter # Async dotnet_diagnostic.CS1998.severity = error # CS1998: Async method lacks 'await' operators and will run synchronously dotnet_diagnostic.CS4014.severity = error # CS4014: Because this call is not awaited, execution of the current method continues before the call is completed +dotnet_diagnostic.CA2007.severity = none # CA2007: Consider calling ConfigureAwait on the awaited task # Dispose things need disposing dotnet_diagnostic.CA2000.severity = error # CA2000: Dispose objects before losing scope diff --git a/samples/Normal/Class1.cs b/samples/Normal/Class1.cs new file mode 100644 index 00000000..18b18c30 --- /dev/null +++ b/samples/Normal/Class1.cs @@ -0,0 +1,50 @@ +using Dummy; +using Immediate.Handlers.Utility; + +[assembly: RenderMode(renderMode: RenderMode.Normal)] + +[assembly: Behaviors( + typeof(LoggingBehavior<,>) +)] + +namespace Dummy; + +public class GetUsersEndpoint(GetUsersQuery.Handler handler) +{ + public async Task> GetUsers() => + handler.HandleAsync(new GetUsersQuery.Query()); +} + +[Handler] +public static class GetUsersQuery +{ + public record Query; + + private static Task> HandleAsync( + Query _, + UsersService usersService, + CancellationToken token) + { + return usersService.GetUsers(); + } +} + +public class LoggingBehavior(ILogger> logger) + : Behavior +{ + public override async Task HandleAsync(TRequest request, CancellationToken cancellationToken) + { + var response = await InnerHandler.HandleAsync(request, cancellationToken); + + return response; + } +} + +public class User { } +public class UsersService +{ + public Task> GetUsers() => + Task.FromResult(Enumerable.Empty()); +} + +public interface ILogger; diff --git a/src/Immediate.Handlers.Utility/Behavior.cs b/src/Immediate.Handlers.Utility/Behavior.cs index cbead812..da38eb74 100644 --- a/src/Immediate.Handlers.Utility/Behavior.cs +++ b/src/Immediate.Handlers.Utility/Behavior.cs @@ -5,10 +5,18 @@ /// /// /// -public class Behavior +public abstract class Behavior { /// /// TODO: Fill out /// public Behavior InnerHandler { get; set; } = default!; + + /// + /// TODO: Fill out + /// + /// + /// + /// + public abstract Task HandleAsync(TRequest request, CancellationToken cancellationToken); } diff --git a/src/Immediate.Handlers/Generators/ImmediateHandlersGenerator.cs b/src/Immediate.Handlers/Generators/ImmediateHandlersGenerator.cs index caa95216..ab9be8d7 100644 --- a/src/Immediate.Handlers/Generators/ImmediateHandlersGenerator.cs +++ b/src/Immediate.Handlers/Generators/ImmediateHandlersGenerator.cs @@ -72,7 +72,17 @@ public void Initialize(IncrementalGeneratorInitializationContext context) private RenderMode TransformRenderMode(GeneratorAttributeSyntaxContext context, CancellationToken token) { - return RenderMode.Normal; + var attr = context.Attributes[0]; + if (attr.ConstructorArguments.Length > 0) + { + var ca = attr.ConstructorArguments[0]; + return (RenderMode?)(int?)ca.Value ?? RenderMode.Normal; + } + else + { + var pa = attr.NamedArguments[0]; + return (RenderMode?)(int?)pa.Value.Value ?? RenderMode.Normal; + } } private static ImmutableArray TransformBehaviors(