Skip to content

Commit

Permalink
Implement TransformRenderMode
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin committed Jan 5, 2024
1 parent fe1c487 commit bc31fd7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
50 changes: 50 additions & 0 deletions samples/Normal/Class1.cs
Original file line number Diff line number Diff line change
@@ -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<IEnumerable<User>> GetUsers() =>
handler.HandleAsync(new GetUsersQuery.Query());
}

[Handler]
public static class GetUsersQuery
{
public record Query;

private static Task<IEnumerable<User>> HandleAsync(
Query _,
UsersService usersService,
CancellationToken token)
{
return usersService.GetUsers();
}
}

public class LoggingBehavior<TRequest, TResponse>(ILogger<LoggingBehavior<TRequest, TResponse>> logger)
: Behavior<TRequest, TResponse>
{
public override async Task<TResponse> HandleAsync(TRequest request, CancellationToken cancellationToken)
{
var response = await InnerHandler.HandleAsync(request, cancellationToken);

return response;
}
}

public class User { }
public class UsersService
{
public Task<IEnumerable<User>> GetUsers() =>
Task.FromResult(Enumerable.Empty<User>());
}

public interface ILogger<T>;
10 changes: 9 additions & 1 deletion src/Immediate.Handlers.Utility/Behavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
/// </summary>
/// <typeparam name="TRequest"></typeparam>
/// <typeparam name="TResponse"></typeparam>
public class Behavior<TRequest, TResponse>
public abstract class Behavior<TRequest, TResponse>
{
/// <summary>
/// TODO: Fill out
/// </summary>
public Behavior<TRequest, TResponse> InnerHandler { get; set; } = default!;

/// <summary>
/// TODO: Fill out
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public abstract Task<TResponse> HandleAsync(TRequest request, CancellationToken cancellationToken);
}
12 changes: 11 additions & 1 deletion src/Immediate.Handlers/Generators/ImmediateHandlersGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Behavior> TransformBehaviors(
Expand Down

0 comments on commit bc31fd7

Please sign in to comment.