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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.0.0-beta.9</Version>
<Version>2.0.0-beta.10</Version>
<!-- SPDX license identifier for MIT -->
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<!-- Other useful metadata -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,12 @@ namespace MinimalLambda.SourceGenerators.Extensions;

internal static class DelegateInfoExtensions
{
internal static string BuildHandlerSignature(this DelegateInfo delegateInfo)
{
// build handler function signature
var signatureBuilder = new StringBuilder();
signatureBuilder.Append(delegateInfo.DelegateType);

// angle brackets needed for any parameters or a response type
if (
delegateInfo.Parameters.Count > 0
|| delegateInfo.ReturnTypeInfo.FullyQualifiedType != TypeConstants.Void
)
{
signatureBuilder.Append("<");

// join parameters with comma
if (delegateInfo.Parameters.Count > 0)
signatureBuilder.Append(
string.Join(
", ",
delegateInfo.Parameters.Select(p => p.TypeInfo.FullyQualifiedType)
)
);

if (delegateInfo.ReturnTypeInfo.FullyQualifiedType != TypeConstants.Void)
{
// add comma if there are parameters, i.e. this is the last in the list
if (delegateInfo.Parameters.Count > 0)
signatureBuilder.Append(", ");
signatureBuilder.Append(delegateInfo.ReturnTypeInfo.FullyQualifiedType);
}

signatureBuilder.Append(">");
}

var handlerSignature = signatureBuilder.ToString();

return handlerSignature;
}

extension(DelegateInfo delegateInfo)
{
internal string BuildHandlerCastCall()
{
var signatureBuilder = new StringBuilder();
signatureBuilder.Append("Cast(handler, ");
signatureBuilder.Append("Utilities.Cast(handler, ");

signatureBuilder.Append(delegateInfo.ReturnTypeInfo.FullyQualifiedType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,23 @@ string generatorVersion
var generatedCodeAttribute =
$"[GeneratedCode(\"{generatorName}\", \"{generatorVersion}\")]";

List<string?> outputs = [CommonSources.Generate(generatedCodeAttribute)];
List<string?> outputs =
[
CommonSources.Generate(generatedCodeAttribute),
"""
namespace MinimalLambda.Generated
{
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using MinimalLambda;
using MinimalLambda.Builder;

""",
];

// if MapHandler calls found, generate the source code.
if (compilationInfo.MapHandlerInvocationInfos.Count >= 1)
Expand Down Expand Up @@ -67,6 +83,16 @@ string generatorVersion
)
);

outputs.Add(
"""
file static class Utilities
{
internal static T Cast<T>(Delegate d, T _) where T : Delegate => (T)d;
}
}
"""
);

// join all the source code together and add it to the compilation context.
var outCode = string.Join("\n", outputs.Where(s => s != null));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ string generatedCodeAttribute
var delegateInfo = mapHandlerInvocationInfo.DelegateInfo;

// build handler function signature
var handlerSignature = delegateInfo.BuildHandlerSignature();
var handlerSignature = delegateInfo.BuildHandlerCastCall();

// build out assignment statements for each handler parameter
var handlerArgs = delegateInfo.BuildHandlerParameterAssignment();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
namespace MinimalLambda.Generated
{
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using MinimalLambda.Builder;

{{ generated_code_attribute }}
file static class GeneratedLambda{{ name }}BuilderExtensions
{
Expand Down Expand Up @@ -50,7 +40,4 @@ namespace MinimalLambda.Generated

{{~ end ~}}
{{~ end ~}}

private static T Cast<T>(Delegate d, T _) where T : Delegate => (T)d;
}
}
}
13 changes: 1 addition & 12 deletions src/MinimalLambda.SourceGenerators/Templates/MapHandler.scriban
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
namespace MinimalLambda.Generated
{
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using MinimalLambda.Builder;
using MinimalLambda;
using Microsoft.Extensions.DependencyInjection;

{{ generated_code_attribute }}
file static class GeneratedLambdaInvocationBuilderExtensions
{
Expand All @@ -21,7 +11,7 @@ namespace MinimalLambda.Generated
Delegate handler
)
{
var castHandler = ({{ call.handler_signature }})handler;
var castHandler = {{ call.handler_signature }};

application.Handle(InvocationDelegate);
{{~ if call.is_event_feature_required ~}}
Expand Down Expand Up @@ -72,4 +62,3 @@ namespace MinimalLambda.Generated
}
{{~ end ~}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ namespace MinimalLambda.Generated
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using MinimalLambda.Builder;
using MinimalLambda;
using Microsoft.Extensions.DependencyInjection;
using MinimalLambda;
using MinimalLambda.Builder;

[GeneratedCode("MinimalLambda.SourceGenerators", "0.0.0")]
file static class GeneratedLambdaInvocationBuilderExtensions
Expand All @@ -48,7 +49,7 @@ internal static ILambdaInvocationBuilder MapHandlerInterceptor0(
Delegate handler
)
{
var castHandler = (global::System.Action<string, global::Amazon.Lambda.Core.ILambdaContext, global::System.Threading.CancellationToken, global::IService, global::IService?, global::IService, global::IService?>)handler;
var castHandler = Utilities.Cast(handler, void (string arg0, global::Amazon.Lambda.Core.ILambdaContext arg1, global::System.Threading.CancellationToken arg2, global::IService arg3, global::IService? arg4, global::IService arg5, global::IService? arg6) => throw null!);

application.Handle(InvocationDelegate);

Expand Down Expand Up @@ -84,4 +85,9 @@ Task InvocationDelegate(ILambdaInvocationContext context)
}
}
}
}

file static class Utilities
{
internal static T Cast<T>(Delegate d, T _) where T : Delegate => (T)d;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ namespace MinimalLambda.Generated
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using MinimalLambda.Builder;
using MinimalLambda;
using Microsoft.Extensions.DependencyInjection;
using MinimalLambda;
using MinimalLambda.Builder;

[GeneratedCode("MinimalLambda.SourceGenerators", "0.0.0")]
file static class GeneratedLambdaInvocationBuilderExtensions
Expand All @@ -48,7 +49,7 @@ internal static ILambdaInvocationBuilder MapHandlerInterceptor0(
Delegate handler
)
{
var castHandler = (global::System.Func<global::System.Threading.Tasks.Task<string>>)handler;
var castHandler = Utilities.Cast(handler, global::System.Threading.Tasks.Task<string> () => throw null!);

application.Handle(InvocationDelegate);

Expand All @@ -70,4 +71,9 @@ async Task InvocationDelegate(ILambdaInvocationContext context)
}
}
}
}

file static class Utilities
{
internal static T Cast<T>(Delegate d, T _) where T : Delegate => (T)d;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ namespace MinimalLambda.Generated
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using MinimalLambda.Builder;
using MinimalLambda;
using Microsoft.Extensions.DependencyInjection;
using MinimalLambda;
using MinimalLambda.Builder;

[GeneratedCode("MinimalLambda.SourceGenerators", "0.0.0")]
file static class GeneratedLambdaInvocationBuilderExtensions
Expand All @@ -48,7 +49,7 @@ internal static ILambdaInvocationBuilder MapHandlerInterceptor0(
Delegate handler
)
{
var castHandler = (global::System.Action<string, global::IService>)handler;
var castHandler = Utilities.Cast(handler, void (string arg0, global::IService arg1) => throw null!);

application.Handle(InvocationDelegate);

Expand All @@ -70,4 +71,9 @@ Task InvocationDelegate(ILambdaInvocationContext context)
}
}
}
}

file static class Utilities
{
internal static T Cast<T>(Delegate d, T _) where T : Delegate => (T)d;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ namespace MinimalLambda.Generated
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using MinimalLambda.Builder;
using MinimalLambda;
using Microsoft.Extensions.DependencyInjection;
using MinimalLambda;
using MinimalLambda.Builder;

[GeneratedCode("MinimalLambda.SourceGenerators", "0.0.0")]
file static class GeneratedLambdaInvocationBuilderExtensions
Expand All @@ -48,7 +49,7 @@ internal static ILambdaInvocationBuilder MapHandlerInterceptor0(
Delegate handler
)
{
var castHandler = (global::System.Func<global::IService, string, string>)handler;
var castHandler = Utilities.Cast(handler, string (global::IService arg0, string arg1) => throw null!);

application.Handle(InvocationDelegate);

Expand All @@ -75,4 +76,9 @@ Task InvocationDelegate(ILambdaInvocationContext context)
}
}
}
}

file static class Utilities
{
internal static T Cast<T>(Delegate d, T _) where T : Delegate => (T)d;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ namespace MinimalLambda.Generated
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using MinimalLambda.Builder;
using MinimalLambda;
using Microsoft.Extensions.DependencyInjection;
using MinimalLambda;
using MinimalLambda.Builder;

[GeneratedCode("MinimalLambda.SourceGenerators", "0.0.0")]
file static class GeneratedLambdaInvocationBuilderExtensions
Expand All @@ -48,7 +49,7 @@ internal static ILambdaInvocationBuilder MapHandlerInterceptor0(
Delegate handler
)
{
var castHandler = (global::System.Func<string, global::IService, global::IService>)handler;
var castHandler = Utilities.Cast(handler, global::IService (string arg0, global::IService arg1) => throw null!);

application.Handle(InvocationDelegate);

Expand Down Expand Up @@ -80,4 +81,9 @@ Task InvocationDelegate(ILambdaInvocationContext context)
}
}
}
}

file static class Utilities
{
internal static T Cast<T>(Delegate d, T _) where T : Delegate => (T)d;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ namespace MinimalLambda.Generated
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using MinimalLambda.Builder;
using MinimalLambda;
using Microsoft.Extensions.DependencyInjection;
using MinimalLambda;
using MinimalLambda.Builder;

[GeneratedCode("MinimalLambda.SourceGenerators", "0.0.0")]
file static class GeneratedLambdaInvocationBuilderExtensions
Expand All @@ -48,7 +49,7 @@ internal static ILambdaInvocationBuilder MapHandlerInterceptor0(
Delegate handler
)
{
var castHandler = (global::System.Func<string, global::IService, string?>)handler;
var castHandler = Utilities.Cast(handler, string? (string arg0, global::IService arg1) => throw null!);

application.Handle(InvocationDelegate);

Expand Down Expand Up @@ -80,4 +81,9 @@ Task InvocationDelegate(ILambdaInvocationContext context)
}
}
}
}

file static class Utilities
{
internal static T Cast<T>(Delegate d, T _) where T : Delegate => (T)d;
}
}
Loading
Loading