Skip to content

Commit

Permalink
Add support for Handler() without Token (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin authored May 29, 2024
1 parent 69419f2 commit 22b5dd3
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 34 deletions.
15 changes: 15 additions & 0 deletions src/Immediate.Handlers.Generators/ITypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,19 @@ typeSymbol is INamedTypeSymbol
ContainingNamespace.IsGlobalNamespace: true
},
};

public static bool IsCancellationToken(this ITypeSymbol typeSymbol) =>
typeSymbol is INamedTypeSymbol
{
Name: "CancellationToken",
ContainingNamespace:
{
Name: "Threading",
ContainingNamespace:
{
Name: "System",
ContainingNamespace.IsGlobalNamespace: true
}
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Template template

handler.MethodName,
HandlerParameters = handler.Parameters,
handler.UseToken,

RequestType = handler.RequestType.Name,
ResponseType = responseType.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ CancellationToken cancellationToken
}

// must have request type and cancellation token
if (handleMethod.Parameters.Length < 2)
if (handleMethod.Parameters is [])
return null;

if (handleMethod.ReturnsVoid)
return null;

cancellationToken.ThrowIfCancellationRequested();

var requestType = BuildGenericType(handleMethod.Parameters[0].Type);
var useToken = handleMethod.Parameters[^1]
.Type.IsCancellationToken();

cancellationToken.ThrowIfCancellationRequested();
var responseTypeSymbol = handleMethod.GetTaskReturnType();
Expand All @@ -55,7 +58,8 @@ CancellationToken cancellationToken

cancellationToken.ThrowIfCancellationRequested();
var parameters = handleMethod.Parameters
.Skip(1).Take(handleMethod.Parameters.Length - 2)
.Skip(1)
.Take(handleMethod.Parameters.Length - (useToken ? 2 : 1))
.Select(p => new Parameter
{
Type = p.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
Expand All @@ -77,12 +81,12 @@ CancellationToken cancellationToken
DisplayName = displayName,

MethodName = handleMethod.Name,
Parameters = parameters,
UseToken = useToken,

RequestType = requestType,
ResponseType = responseType,

Parameters = parameters,

OverrideRenderMode = renderMode,
OverrideBehaviors = behaviors,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private sealed record Handler

public required string MethodName { get; init; }
public required EquatableReadOnlyList<Parameter> Parameters { get; init; }
public required bool UseToken { get; init; }

public required GenericType RequestType { get; init; }
public required GenericType? ResponseType { get; init; }
Expand Down
16 changes: 10 additions & 6 deletions src/Immediate.Handlers.Generators/Templates/Handler.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,25 @@ partial class {{ class_name }}
{{~ if !is_implicit_value_tuple ~}}
return await {{ class_fully_qualified_name }}
.{{ method_name }}(
request,
request
{{~ for parameter in handler_parameters ~}}
_{{ parameter.name }},
, _{{ parameter.name }}
{{~ end ~}}
{{~ if use_token ~}}
, cancellationToken
{{~ end ~}}
cancellationToken
)
.ConfigureAwait(false);
{{~ else ~}}
await {{ class_fully_qualified_name }}
.{{ method_name }}(
request,
request
{{~ for parameter in handler_parameters ~}}
_{{ parameter.name }},
, _{{ parameter.name }}
{{~ end ~}}
{{~ if use_token ~}}
, cancellationToken
{{~ end ~}}
cancellationToken
)
.ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ private static ValueTask<int> HandleAsync(
}
}

[Handler]
public static partial class NoBehaviorNoTokenOneAdder
{
public sealed record Query(int Input);

private static ValueTask<int> HandleAsync(
Query query
)
{
return ValueTask.FromResult(query.Input + 1);
}
}

public class ParameterlessTests
{
[Fact]
Expand All @@ -31,4 +44,18 @@ public async Task NoBehaviorShouldReturnExpectedResponse()

Assert.Equal(Input + 1, result);
}

[Fact]
public async Task NoTokenShouldReturnExpectedResponse()
{
const int Input = 1;

var handler = HandlerResolver.Resolve<NoBehaviorNoTokenOneAdder.Handler>();

var query = new NoBehaviorNoTokenOneAdder.Query(Input);

var result = await handler.HandleAsync(query);

Assert.Equal(Input + 1, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public HandleBehavior(
{
await global::ConstraintHandler
.HandleAsync(
request,
cancellationToken
request
, cancellationToken
)
.ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public HandleBehavior(
{
await global::ConstraintHandler
.HandleAsync(
request,
cancellationToken
request
, cancellationToken
)
.ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public async Task CrtpBehavior(DriverReferenceAssemblies assemblies)
{
var driver = GeneratorTestHelper.GetDriver(
"""
using System.Threading;
using System.Threading.Tasks;
using Immediate.Handlers.Shared;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public HandleBehavior(
{
return await global::Dummy.GetUsersQuery
.HandleAsync(
request,
_usersService,
cancellationToken
request
, _usersService
, cancellationToken
)
.ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public HandleBehavior(
{
return await global::Dummy.GetUsersQuery
.HandleAsync(
request,
_usersService,
cancellationToken
request
, _usersService
, cancellationToken
)
.ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Immediate.Handlers.Tests.GeneratorTests.Behaviors;
public class MultipleBehaviorTest
{
private const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public HandleBehavior(
{
return await global::Dummy.GetUsersQuery
.HandleAsync(
request,
_usersService,
cancellationToken
request
, _usersService
, cancellationToken
)
.ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public HandleBehavior(
{
return await global::Dummy.GetUsersQuery
.HandleAsync(
request,
_usersService,
cancellationToken
request
, _usersService
, cancellationToken
)
.ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Immediate.Handlers.Tests.GeneratorTests.Behaviors;
public class SingleBehaviorTest
{
private const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class InvalidBehaviorsTest
public async Task NonBehaviorShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down Expand Up @@ -73,6 +74,7 @@ public interface ILogger<T>;
public async Task BoundGenericShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down Expand Up @@ -136,6 +138,7 @@ public interface ILogger<T>;
public async Task NonGenericBehaviorShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down Expand Up @@ -199,6 +202,7 @@ public interface ILogger<T>;
public async Task AbstractBehaviorShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down Expand Up @@ -254,6 +258,7 @@ public interface ILogger<T>;
public async Task BehaviorHasTooManyTRequestConstraintsShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down Expand Up @@ -317,6 +322,7 @@ public interface ILogger<T>;
public async Task BehaviorHasTooManyTResponseConstraintsShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down Expand Up @@ -380,6 +386,7 @@ public interface ILogger<T>;
public async Task BehaviorHasTooManyTypeParametersShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class InvalidHandlerTest
public async Task HandlerWithoutHandlerMethodShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down Expand Up @@ -52,6 +53,7 @@ public interface ILogger<T>;
public async Task HandlerWithTwoHandlersMethodShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down Expand Up @@ -106,9 +108,10 @@ public interface ILogger<T>;
[Theory]
[InlineData(DriverReferenceAssemblies.Normal)]
[InlineData(DriverReferenceAssemblies.Msdi)]
public async Task HandlerWithOneParameterShouldProduceNothing(DriverReferenceAssemblies assemblies)
public async Task HandlerWithNoParametersShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand All @@ -126,8 +129,7 @@ public static class GetUsersQuery
{
public record Query;
private static ValueTask<IEnumerable<User>> HandleAsync(
Query _)
private static ValueTask<IEnumerable<User>> HandleAsync()
{
return usersService.GetUsers();
}
Expand Down Expand Up @@ -156,6 +158,7 @@ public interface ILogger<T>;
public async Task HandlerWithVoidResponseShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down Expand Up @@ -204,6 +207,7 @@ public interface ILogger<T>;
public async Task HandlerWithTaskResponseShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down Expand Up @@ -253,6 +257,7 @@ public interface ILogger<T>;
public async Task NestedHandlerShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class InvalidRenderModeTest
public async Task InvalidRenderModeOnAssemblyShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Dummy;
using Immediate.Handlers.Shared;
Expand Down Expand Up @@ -54,6 +55,7 @@ public interface ILogger<T>;
public async Task InvalidRenderModeOnHandlerShouldProduceNothing(DriverReferenceAssemblies assemblies)
{
const string Input = """
using System.Threading;
using System.Threading.Tasks;
using Immediate.Handlers.Shared;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public HandleBehavior(
{
return await global::Dummy.GetUsersQuery
.HandleAsync(
request,
cancellationToken
request
, cancellationToken
)
.ConfigureAwait(false);
}
Expand Down
Loading

0 comments on commit 22b5dd3

Please sign in to comment.