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
Expand Up @@ -23,7 +23,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0')) And !$(MSBuildProjectDirectory.Contains('tests'))">
<IsAotCompatible>true</IsAotCompatible>
<EnableAotAnalyzer>true</EnableAotAnalyzer>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
Expand Down
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<PackageVersion Include="Autofac.Extensions.DependencyInjection" Version="10.0.0" />
<PackageVersion Include="AutoFixture.AutoNSubstitute" Version="4.18.1" />
<PackageVersion Include="AutoFixture.Xunit3" Version="4.19.0" />
<PackageVersion Include="Basic.Reference.Assemblies.Net100" Version="1.8.4" />
<PackageVersion Include="Basic.Reference.Assemblies.Net80" Version="1.8.4" />
<PackageVersion Include="BenchmarkDotNet" Version="0.15.8" />
<PackageVersion Include="LayeredCraft.SourceGeneratorTools.Generator" Version="0.1.0-beta.10" />
<PackageVersion Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.3.0" />
Expand Down
4 changes: 1 addition & 3 deletions benchmarks/MinimalLambda.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

var config = DefaultConfig.Instance;

var summary = BenchmarkRunner.Run<CreateBuilderBenchmarks>(config, args);

// var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
BenchmarkRunner.Run<CreateBuilderBenchmarks>(config, args);
2 changes: 1 addition & 1 deletion examples/MinimalLambda.Example.Events/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
(
[FromEvent] ApiGatewayRequestEnvelope<Request> request,
ILogger<Program> logger,
ILambdaInvocationContext context
ILambdaInvocationContext _
) =>
{
logger.LogInformation("In Handler. Payload: {Payload}", request.Body);
Expand Down
4 changes: 2 additions & 2 deletions examples/MinimalLambda.Example.Lifecycle/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async Task<bool> (ILogger<Program> logger, CancellationToken cancellationToken)
});

lambda.OnShutdown(
(CancellationToken token) =>
(CancellationToken _) =>
{
Console.WriteLine("Shutting down...");
return Task.CompletedTask;
Expand All @@ -68,7 +68,7 @@ async Task<bool> (ILogger<Program> logger, CancellationToken cancellationToken)
lambda.OnShutdown(
Task (IService service) =>
{
Console.WriteLine(service?.GetMessage());
Console.WriteLine(service.GetMessage());
return Task.CompletedTask;
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ internal static class GenericHandlerSources
/// handler. The return type of the wrapper is a <c>Task</c> or <c>Task&lt;T&gt;</c> depending on
/// the return type of the actual handler.
/// </summary>
/// <param name="higherOrderMethodInfos">The method info from which the handler is generated</param>
/// <param name="methodName">
/// The name of the method that the handler is coming from and for which the
/// code is intercepting
/// </param>
/// <param name="wrapperReturnType">
/// The return type of the wrapper function, without Task. Ex.,
/// <c>bool</c> not <c>Task&lt;bool&gt;</c>. A <c>null</c> signifies no return value.
/// </param>
/// <param name="defaultWrapperReturnValue">
/// The default value the wrapped handler will return if the
/// handler does not return a value of the same type as wrapperReturnType. This is without Task.
/// Ex., <c>bool</c> not <c>Task&lt;bool&gt;</c>. A <c>null</c> signifies no default return value.
/// </param>
/// <returns></returns>
internal static string Generate(
EquatableArray<HigherOrderMethodInfo> higherOrderMethodInfos,
string methodName,
Expand Down Expand Up @@ -155,5 +140,6 @@ private static HandlerArg[] BuildHandlerParameterAssignment(this DelegateInfo de
return handlerArgs;
}

// ReSharper disable NotAccessedPositionalProperty.Local
private readonly record struct HandlerArg(string String, string Assignment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ internal static string GeneratedCodeAttribute
if (field is null)
{
var assembly = Assembly.GetExecutingAssembly();
var generatorName = assembly.GetName().FullName;
var generatorName = assembly.GetName().Name;
var generatorVersion = assembly.GetName().Version.ToString();

field = $"[GeneratedCode(\"{generatorName}\", \"{generatorVersion}\")]";
field =
$"""[global::System.CodeDom.Compiler.GeneratedCode("{generatorName}", "{generatorVersion}")]""";
}

return field;
Expand All @@ -45,7 +46,6 @@ internal static void Generate(SourceProductionContext context, CompilationInfo c
namespace MinimalLambda.Generated
{
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,6 @@ private static HandlerArg[] BuildHandlerParameterAssignment(this DelegateInfo de
})
.ToArray();

// ReSharper disable NotAccessedPositionalProperty.Local
private readonly record struct HandlerArg(string String, string Assignment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ CancellationToken cancellationToken

private static ExpressionSyntax? GetDelegateFromCast(
CastExpressionSyntax castExpression,
CancellationToken cancellationToken
CancellationToken _
)
{
// must have at least 2 children -> expression at index 1, cast at index 0
Expand Down
2 changes: 2 additions & 0 deletions src/MinimalLambda.Testing/HostFactoryResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using System.Diagnostics;
using System.Reflection;

// ReSharper disable All

namespace Microsoft.Extensions.Hosting;

internal sealed class HostFactoryResolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public IServiceCollection TryAddLambdaHostDefaultServices()
// We will only attempt to add ILambdaSerializer if we are not using AOT.
// This is needed for code AOT analysis.
if (RuntimeFeature.IsDynamicCodeSupported)
#pragma warning disable IL2026
services.TryAddSingleton<ILambdaSerializer, DefaultLambdaJsonSerializer>();
#pragma warning restore IL2026

services.TryAddSingleton<
ILambdaCancellationFactory,
Expand Down
4 changes: 1 addition & 3 deletions src/MinimalLambda/Builder/LambdaApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ internal LambdaApplication(IHost host)
/// <summary>Gets the application's logger.</summary>
public ILogger Logger =>
field ??=
_host
.Services.GetService<ILoggerFactory>()
?.CreateLogger(Environment.ApplicationName ?? nameof(LambdaApplication))
_host.Services.GetService<ILoggerFactory>()?.CreateLogger(Environment.ApplicationName)
?? NullLogger.Instance;

/// <inheritdoc />
Expand Down
1 change: 1 addition & 0 deletions src/MinimalLambda/Builder/LambdaOnInitBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public ILambdaOnInitBuilder OnInit(LambdaInitDelegate handler)
using var cts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
cts.CancelAfter(_options.InitTimeout);

// ReSharper disable once AccessToDisposedClosure
var tasks = _handlers.Select(h => RunInitHandler(h, cts.Token));

(Exception? Error, bool ShouldContinue)[] results;
Expand Down
12 changes: 6 additions & 6 deletions src/MinimalLambda/Runtime/LambdaHandlerComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,26 @@ async Task<Stream> CreateRequestHandler(Stream inputStream, ILambdaContext lambd

// Create a new lambda host context. This will also create a new service scope
// the first time that the service container is accessed.
var LambdaInvocationContext = _contextFactory.Create(
var lambdaInvocationContext = _contextFactory.Create(
lambdaContext,
builder.Properties,
linkedTokenSource.Token
);

await using (LambdaInvocationContext as IAsyncDisposable)
await using (lambdaInvocationContext as IAsyncDisposable)
{
using var invocationDataFeature = _invocationDataFeatureFactory.Create(inputStream);
LambdaInvocationContext.Features.Set(invocationDataFeature);
lambdaInvocationContext.Features.Set(invocationDataFeature);

// Invoke the handler wrapped in the middleware pipeline.
await handler.Invoke(LambdaInvocationContext).ConfigureAwait(false);
await handler.Invoke(lambdaInvocationContext).ConfigureAwait(false);

if (
LambdaInvocationContext.Features.TryGet<IResponseFeature>(
lambdaInvocationContext.Features.TryGet<IResponseFeature>(
out var responseFeature
)
)
responseFeature.SerializeToStream(LambdaInvocationContext);
responseFeature.SerializeToStream(lambdaInvocationContext);

// If no serializer is provided, return an empty stream.
return invocationDataFeature.ResponseStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void PackPayload_WithValidData_SerializesBodyContent()
options.JsonOptions
);
deserializedData.Should().NotBeNull();
deserializedData!.Message.Should().Be(responseData.Message);
deserializedData.Message.Should().Be(responseData.Message);
deserializedData.Code.Should().Be(responseData.Code);
}

Expand Down Expand Up @@ -82,7 +82,7 @@ public void BodyContent_HasJsonIgnoreAttribute()
{
// Arrange
var property = typeof(AlbResponseEnvelope<ResponsePayload>).GetProperty(
nameof(AlbResponseEnvelope<ResponsePayload>.BodyContent)
nameof(AlbResponseEnvelope<>.BodyContent)
);

// Act
Expand Down Expand Up @@ -118,7 +118,7 @@ public void PackPayload_WithComplexObject_PreservesAllProperties()
envelope.Body.Should().NotBeNull();
var deserializedData = JsonSerializer.Deserialize<ResponsePayload>(envelope.Body);
deserializedData.Should().NotBeNull();
deserializedData!.Message.Should().Be(responseData.Message);
deserializedData.Message.Should().Be(responseData.Message);
deserializedData.Code.Should().Be(responseData.Code);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/MinimalLambda.Envelopes.UnitTests/AlbResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void PackPayload_SerializesBodyContent()
result.Body.Should().NotBeNull();
var deserialized = JsonSerializer.Deserialize<TestPayload>(result.Body);
deserialized.Should().NotBeNull();
deserialized!.Name.Should().Be(payload.Name);
deserialized.Name.Should().Be(payload.Name);
deserialized.Value.Should().Be(payload.Value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void BodyContent_HasJsonIgnoreAttribute()
{
// Arrange
var property = typeof(ApiGatewayRequestEnvelope<TestPayload>).GetProperty(
nameof(ApiGatewayRequestEnvelope<TestPayload>.BodyContent)
nameof(ApiGatewayRequestEnvelope<>.BodyContent)
);

// Act
Expand All @@ -137,24 +137,6 @@ public void ApiGatewayRequestEnvelope_InheritsFromApiGatewayProxyRequest()
envelope.Should().BeAssignableTo<APIGatewayProxyRequest>();
}

[Fact]
public void ExtractPayload_WithMultipleProperties_PreservesAllValues()
{
// Arrange
var testData = _fixture.Create<TestPayload>();
var json = JsonSerializer.Serialize(testData);
var envelope = new ApiGatewayRequestEnvelope<TestPayload> { Body = json };
var options = new EnvelopeOptions();

// Act
envelope.ExtractPayload(options);

// Assert
envelope.BodyContent.Should().NotBeNull();
envelope.BodyContent!.Name.Should().Be(testData.Name);
envelope.BodyContent.Value.Should().Be(testData.Value);
}

[Fact]
public void ExtractPayload_WithMalformedJsonStructure_ThrowsJsonException()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void PackPayload_WithValidData_SerializesBodyContent()
options.JsonOptions
);
deserializedData.Should().NotBeNull();
deserializedData!.Message.Should().Be(responseData.Message);
deserializedData.Message.Should().Be(responseData.Message);
deserializedData.Code.Should().Be(responseData.Code);
}

Expand Down Expand Up @@ -101,7 +101,7 @@ public void PackPayload_WithComplexObject_PreservesAllProperties()
envelope.Body.Should().NotBeNull();
var deserializedData = JsonSerializer.Deserialize<ResponsePayload>(envelope.Body);
deserializedData.Should().NotBeNull();
deserializedData!.Message.Should().Be(responseData.Message);
deserializedData.Message.Should().Be(responseData.Message);
deserializedData.Code.Should().Be(responseData.Code);
}

Expand All @@ -110,7 +110,7 @@ public void BodyContent_HasJsonIgnoreAttribute()
{
// Arrange
var property = typeof(ApiGatewayResponseEnvelope<ResponsePayload>).GetProperty(
nameof(ApiGatewayResponseEnvelope<ResponsePayload>.BodyContent)
nameof(ApiGatewayResponseEnvelope<>.BodyContent)
);

// Act
Expand Down Expand Up @@ -202,7 +202,7 @@ public void PackPayload_WithCustomConverter_UsesCustomSerialization()
options.JsonOptions
);
deserializedData.Should().NotBeNull();
deserializedData!.Message.Should().Be(responseData.Message);
deserializedData.Message.Should().Be(responseData.Message);
}

private record ResponsePayload(string Message, int Code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void PackPayload_SerializesBodyContent()
result.Body.Should().NotBeNull();
var deserialized = JsonSerializer.Deserialize<TestPayload>(result.Body);
deserialized.Should().NotBeNull();
deserialized!.Name.Should().Be(payload.Name);
deserialized.Name.Should().Be(payload.Name);
deserialized.Value.Should().Be(payload.Value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void PackPayload_WithValidData_SerializesBodyContent()
options.JsonOptions
);
deserializedData.Should().NotBeNull();
deserializedData!.Message.Should().Be(responseData.Message);
deserializedData.Message.Should().Be(responseData.Message);
deserializedData.Code.Should().Be(responseData.Code);
}

Expand Down Expand Up @@ -101,7 +101,7 @@ public void PackPayload_WithComplexObject_PreservesAllProperties()
envelope.Body.Should().NotBeNull();
var deserializedData = JsonSerializer.Deserialize<ResponsePayload>(envelope.Body);
deserializedData.Should().NotBeNull();
deserializedData!.Message.Should().Be(responseData.Message);
deserializedData.Message.Should().Be(responseData.Message);
deserializedData.Code.Should().Be(responseData.Code);
}

Expand All @@ -110,7 +110,7 @@ public void BodyContent_HasJsonIgnoreAttribute()
{
// Arrange
var property = typeof(ApiGatewayV2ResponseEnvelope<ResponsePayload>).GetProperty(
nameof(ApiGatewayV2ResponseEnvelope<ResponsePayload>.BodyContent)
nameof(ApiGatewayV2ResponseEnvelope<>.BodyContent)
);

// Act
Expand Down Expand Up @@ -205,7 +205,7 @@ public void PackPayload_WithCustomConverter_UsesCustomSerialization()
options.JsonOptions
);
deserializedData.Should().NotBeNull();
deserializedData!.Message.Should().Be(responseData.Message);
deserializedData.Message.Should().Be(responseData.Message);
}

private record ResponsePayload(string Message, int Code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void PackPayload_SerializesBodyContent()
result.Body.Should().NotBeNull();
var deserialized = JsonSerializer.Deserialize<TestPayload>(result.Body);
deserialized.Should().NotBeNull();
deserialized!.Name.Should().Be(payload.Name);
deserialized.Name.Should().Be(payload.Name);
deserialized.Value.Should().Be(payload.Value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,5 +356,6 @@ public void InternalServerError_WithBodyContent_ReturnsStatus500WithJson()
result.Body.Should().Contain(payload.Name);
}

// ReSharper disable once NotAccessedPositionalProperty.Local
private record TestPayload(string Name, int Value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void PackPayload_WithValidData_SerializesDataContent()
options.JsonOptions
);
deserializedData.Should().NotBeNull();
deserializedData!.Name.Should().Be(payload.Name);
deserializedData.Name.Should().Be(payload.Name);
deserializedData.Value.Should().Be(payload.Value);
}

Expand Down
Loading
Loading