Skip to content

Commit 90494be

Browse files
authored
LAMBJ-83 Runner Method Configuration (#92)
1 parent be6d673 commit 90494be

File tree

14 files changed

+176
-16
lines changed

14 files changed

+176
-16
lines changed

.github/releases/v0.6.0-beta2.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
This release introduces the following:
22

3-
- You can now shrink package sizes by utilizing the new Lambdajection Layer! For the time being, this is only available for those also using the Lambdajection Custom Runtime. Support for aws-hosted dotnetcore3.1 may be added in a future release.
3+
- You can now shrink package sizes by utilizing the new Lambdajection Layer! For the time being, this is only available for those also using the Lambdajection Custom Runtime. Support for aws-hosted dotnetcore3.1 may be added in a future release.
4+
- The "Run" method name can now be customized using the RunnerMethod property in the Lambda Attribute.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ So, going off the example above, the handler scheme would look like this:
290290
Your.Assembly.Name::Your.Namespace.YourLambda::Run
291291
```
292292

293+
You can customize the name of the "Run" method via the
294+
RunnerMethod property of `LambdaAttribute`.
295+
293296
### 4.9. Custom Runtimes
294297

295298
Lambdajection can be used with custom runtimes starting in v0.5.0-beta2, that way you can use a newer version of .NET as soon as it comes out, even if it is not an LTS release.

examples/CustomRuntime/CustomRuntime.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<PackageReference Include="Lambdajection.Runtime" Version="$(LambdajectionVersion)" />
1212
<PackageReference Include="Lambdajection.Layer" Version="$(LambdajectionVersion)" />
1313

14-
<!-- Ready to run compiler looks for AWSSDK.Core since Lambdajection.Core has references to it -->
15-
<PackageReference Include="AWSSDK.Core" Version="3.5.1.2" />
14+
<!-- If you don't use Lambdajection.Layer, the ready to run compiler will look for AWSSDK.Core since Lambdajection.Core has references to it -->
15+
<!-- <PackageReference Include="AWSSDK.Core" Version="3.5.1.2" /> -->
1616
</ItemGroup>
1717

1818
</Project>

src/Attributes/LambdaAttribute.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,15 @@ public LambdaAttribute(Type startup)
3131
/// <summary>Gets or sets the Config Factory to use for the Lambda.</summary>
3232
/// <value>The type of Config Factory to use for the Lambda.</value>
3333
public Type ConfigFactory { get; set; } = null!;
34+
35+
/// <summary>
36+
/// Gets or sets the runner method name.
37+
/// At compile-time, lambdajection generates a runner method, which is what gets invoked
38+
/// by the runtime instead of your lambda's handle method. The runner method is what sets
39+
/// up dependency injection, configuration etc. You can customize the runner method's name
40+
/// with this property.
41+
/// </summary>
42+
/// <value>The name of the lambda's runner method.</value>
43+
public string RunnerMethod { get; set; } = "Run";
3444
}
3545
}

src/Generator/GenerationContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class GenerationContext
3030

3131
public string StartupTypeDisplayName { get; set; }
3232

33+
public string RunnerMethodName { get; set; }
34+
3335
public HashSet<string> Usings { get; } = new HashSet<string>();
3436

3537
public GenerationSettings Settings { get; init; }

src/Generator/LambdaGenerator.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ClassDeclarationSyntax Generate()
4444
.WithBaseList(BaseList(Token(ColonToken), SeparatedList(typeConstraints)))
4545
.AddModifiers(Token(PartialKeyword))
4646
.AddMembers(
47-
GenerateRunMethod(),
47+
GenerateRunnerMethod(),
4848
configurator);
4949

5050
if (context.Settings.GenerateEntrypoint)
@@ -69,7 +69,7 @@ public ClassDeclarationSyntax Generate()
6969
return result;
7070
}
7171

72-
public MemberDeclarationSyntax GenerateRunMethod()
72+
public MemberDeclarationSyntax GenerateRunnerMethod()
7373
{
7474
var modifiers = new SyntaxToken[]
7575
{
@@ -91,10 +91,11 @@ public MemberDeclarationSyntax GenerateRunMethod()
9191
});
9292

9393
var parameterList = ParameterList(parameters);
94-
var method = MethodDeclaration(ParseTypeName($"Task<{returnType}>"), "Run")
94+
var body = Block(GenerateRunnerMethodBody());
95+
var method = MethodDeclaration(ParseTypeName($"Task<{returnType}>"), context.RunnerMethodName)
9596
.WithModifiers(TokenList(modifiers))
9697
.WithParameterList(parameterList)
97-
.WithBody(Block(GenerateRunMethodBody()));
98+
.WithBody(body);
9899

99100
var serializerName = GetSerializerName();
100101
var serializerNamespace = GetSerializerNamespace();
@@ -117,14 +118,15 @@ public MemberDeclarationSyntax GenerateRunMethod()
117118
return method;
118119
}
119120

120-
public IEnumerable<StatementSyntax> GenerateRunMethodBody()
121+
public IEnumerable<StatementSyntax> GenerateRunnerMethodBody()
121122
{
122123
var configFactory = context.ConfigFactoryType?.Name ?? "LambdaConfigFactory";
123124
var configFactoryNamespace = context.ConfigFactoryType?.ContainingNamespace?.ToString() ?? "Lambdajection.Core";
124125
context.Usings.Add(configFactoryNamespace);
125126

127+
var inputParameterName = inputParameter!.Identifier.ValueText;
126128
yield return ParseStatement($"await using var host = new LambdaHost<{className}, {inputType}, {returnType}, {context.StartupTypeName}, LambdajectionConfigurator, {configFactory}>();");
127-
yield return ParseStatement($"return await host.Run({inputParameter!.Identifier.ValueText}, context);");
129+
yield return ParseStatement($"return await host.Run({inputParameterName}, context);");
128130
}
129131

130132
public MemberDeclarationSyntax GenerateMainMethod()
@@ -134,7 +136,8 @@ public MemberDeclarationSyntax GenerateMainMethod()
134136

135137
IEnumerable<StatementSyntax> GenerateBody()
136138
{
137-
yield return ParseStatement($"using var wrapper = HandlerWrapper.GetHandlerWrapper((Func<{inputType}, ILambdaContext, Task<{returnType}>>)Run, new DefaultLambdaJsonSerializer());");
139+
var runnerMethodName = context.RunnerMethodName;
140+
yield return ParseStatement($"using var wrapper = HandlerWrapper.GetHandlerWrapper((Func<{inputType}, ILambdaContext, Task<{returnType}>>){runnerMethodName}, new DefaultLambdaJsonSerializer());");
138141
yield return ParseStatement($"using var bootstrap = new LambdaBootstrap(wrapper);");
139142
yield return ParseStatement($"await bootstrap.RunAsync();");
140143
}

src/Generator/UnitGenerator.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ from node in tree.GetRoot().DescendantNodesAndSelf().OfType<ClassDeclarationSynt
4141
from attr in semanticModel.GetDeclaredSymbol(node)?.GetAttributes() ?? ImmutableArray.Create<AttributeData>()
4242
where attr.AttributeClass?.Name == nameof(LambdaAttribute)
4343

44-
let startupType = GetAttributeArgument(attr, "Startup")!
44+
let startupType = GetAttributeArgument<INamedTypeSymbol>(attr, "Startup")!
4545
let generationContext = new GenerationContext
4646
{
4747
SourceGeneratorContext = context,
@@ -51,8 +51,9 @@ from attr in semanticModel.GetDeclaredSymbol(node)?.GetAttributes() ?? Immutable
5151
AttributeData = attr,
5252
CancellationToken = context.CancellationToken,
5353
StartupType = startupType,
54-
SerializerType = GetAttributeArgument(attr, "Serializer"),
55-
ConfigFactoryType = GetAttributeArgument(attr, "ConfigFactory"),
54+
SerializerType = GetAttributeArgument<INamedTypeSymbol>(attr, "Serializer"),
55+
ConfigFactoryType = GetAttributeArgument<INamedTypeSymbol>(attr, "ConfigFactory"),
56+
RunnerMethodName = GetAttributeArgument<string>(attr, "RunnerMethod") ?? "Run",
5657
StartupTypeName = startupType.Name,
5758
StartupTypeDisplayName = startupType.ToDisplayString(),
5859
Settings = settings,
@@ -88,16 +89,16 @@ from attr in semanticModel.GetDeclaredSymbol(node)?.GetAttributes() ?? Immutable
8889
}
8990
}
9091

91-
public static INamedTypeSymbol? GetAttributeArgument(AttributeData attributeData, string argName)
92+
public static T? GetAttributeArgument<T>(AttributeData attributeData, string argName)
9293
{
9394
if (argName == "Startup")
9495
{
95-
return (INamedTypeSymbol?)attributeData.ConstructorArguments[0].Value;
96+
return (T?)attributeData.ConstructorArguments[0].Value;
9697
}
9798

9899
var query = from arg in attributeData.NamedArguments
99100
where arg.Key == argName
100-
select (INamedTypeSymbol?)arg.Value.Value;
101+
select (T?)arg.Value.Value;
101102

102103
return query.FirstOrDefault();
103104
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net5.0</TargetFramework>
4+
<RuntimeIdentifiers>linux-x64</RuntimeIdentifiers>
5+
<SelfContained>false</SelfContained>
6+
<IsPackable>false</IsPackable>
7+
<NoWarn>CS8019</NoWarn>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Lambdajection" Version="$(LambdajectionVersion)" />
12+
13+
<!-- This is part of the test: does using a custom runtime work when the runner method name is customized? -->
14+
<PackageReference Include="Lambdajection.Runtime" Version="$(LambdajectionVersion)" />
15+
</ItemGroup>
16+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Threading.Tasks;
2+
3+
using Amazon.Lambda.Core;
4+
5+
using Lambdajection.Attributes;
6+
using Lambdajection.Core;
7+
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
10+
11+
namespace Lambdajection.CompilationTests.CustomRunnerMethod
12+
{
13+
[Lambda(typeof(Startup), RunnerMethod = "Process")]
14+
public partial class Handler
15+
{
16+
public Task<string> Handle(string request)
17+
{
18+
return Task.FromResult("Success");
19+
}
20+
}
21+
}
22+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Threading.Tasks;
2+
3+
using Amazon.Lambda.Core;
4+
5+
using Lambdajection.Attributes;
6+
using Lambdajection.Core;
7+
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
10+
11+
namespace Lambdajection.CompilationTests.CustomRunnerMethod
12+
{
13+
public class Startup : ILambdaStartup
14+
{
15+
public void ConfigureServices(IServiceCollection services)
16+
{
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)