Skip to content

Commit

Permalink
Async match for actions (#83)
Browse files Browse the repository at this point in the history
* Implement match async for action parameters
  • Loading branch information
domn1995 authored Nov 13, 2022
1 parent 8b66025 commit 26a4bff
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 8 deletions.
82 changes: 76 additions & 6 deletions src/GenerateUnionExtensions/UnionExtensionsSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,38 @@ public static string GenerateExtensions(UnionRecord union)
$"{union.Accessibility.ToKeyword()} static class {union.Name}MatchExtensions"
);
builder.AppendLine("{");
var taskMethod = GenerateMatchAsyncMethod(union, "System.Threading.Tasks.Task");
builder.AppendLine(taskMethod);
var valueTaskMethod = GenerateMatchAsyncMethod(union, "System.Threading.Tasks.ValueTask");
builder.Append(valueTaskMethod);
builder.AppendLine("}");

var taskMethodForFuncs = GenerateMatchAsyncMethodForFuncs(
union,
"System.Threading.Tasks.Task"
);
builder.AppendLine(taskMethodForFuncs);

var valueTaskMethodForFuncs = GenerateMatchAsyncMethodForFuncs(
union,
"System.Threading.Tasks.ValueTask"
);
builder.AppendLine(valueTaskMethodForFuncs);

var taskMethodForActions = GenerateMatchAsyncMethodForActions(
union,
"System.Threading.Tasks.Task"
);
builder.AppendLine(taskMethodForActions);

var valueTaskMethodForActions = GenerateMatchAsyncMethodForActions(
union,
"System.Threading.Tasks.ValueTask"
);
builder.Append(valueTaskMethodForActions);

builder.AppendLine("}");
builder.AppendLine("#pragma warning restore 1591");

return builder.ToString();
}

private static string GenerateMatchAsyncMethod(UnionRecord union, string taskType)
private static string GenerateMatchAsyncMethodForFuncs(UnionRecord union, string taskType)
{
var builder = new StringBuilder();

Expand Down Expand Up @@ -92,4 +112,54 @@ private static string GenerateMatchAsyncMethod(UnionRecord union, string taskTyp

return builder.ToString();
}

private static string GenerateMatchAsyncMethodForActions(UnionRecord union, string taskType)
{
var builder = new StringBuilder();

builder.Append($" public static async {taskType} MatchAsync");
builder.AppendTypeParams(union.TypeParameters);
builder.AppendLine("(");
builder.Append($" this {taskType}<");
builder.AppendFullUnionName(union);
builder.AppendTypeParams(union.TypeParameters);
builder.AppendLine("> unionTask,");

for (int i = 0; i < union.Members.Count; ++i)
{
var member = union.Members[i];
builder.Append($" System.Action<");
builder.AppendFullUnionName(union);
builder.AppendTypeParams(union.TypeParameters);
builder.Append($".{member.Name}");
builder.Append($"> {member.Name.ToMethodParameterCase()}");
if (i < union.Members.Count - 1)
{
builder.Append(",");
}
builder.AppendLine();
}

builder.AppendLine($" )");
foreach (var typeParamConstraint in union.TypeParameterConstraints)
{
builder.AppendLine($" {typeParamConstraint}");
}
builder.AppendLine($" => (await unionTask.ConfigureAwait(false)).Match(");

for (int i = 0; i < union.Members.Count; ++i)
{
var member = union.Members[i];
builder.Append($" {member.Name.ToMethodParameterCase()}");
if (i < union.Members.Count - 1)
{
builder.Append(",");
}
builder.AppendLine();
}

builder.AppendLine(" );");

return builder.ToString();
}
}
47 changes: 47 additions & 0 deletions test/GenerateUnionExtensions/GenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,53 @@ partial record Triangle(double Base, double Height);
triangle => triangle.Base * triangle.Height / 2
);
async static {taskType}<Shape> GetShapeAsync()
{{
await Task.Delay(0);
return new Shape.Rectangle(3, 4);
}}";
// Act.
var result = Compile.ToAssembly(shapeCs, programCs);

// Assert.
result.CompilationErrors.Should().BeEmpty();
result.GenerationErrors.Should().BeEmpty();
}

[Theory]
[InlineData("Task")]
[InlineData("ValueTask")]
public void CanUseMatchAsyncWithActionsOnAsyncMethodsThatReturnUnions(string taskType)
{
// Arrange.
const string shapeCs =
@"
using Dunet;
namespace Shapes;
[Union]
partial record Shape
{
partial record Circle(double Radius);
partial record Rectangle(double Length, double Width);
partial record Triangle(double Base, double Height);
}";

var programCs =
@$"
using System.Threading.Tasks;
using Shapes;
await GetShapeAsync()
.MatchAsync(
circle => DoNothing(),
rectangle => DoNothing(),
triangle => DoNothing()
);
void DoNothing() {{ }}
async static {taskType}<Shape> GetShapeAsync()
{{
await Task.Delay(0);
Expand Down
52 changes: 51 additions & 1 deletion test/GenerateUnionExtensions/GenericGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class GenericGenerationTests : UnionRecordTests
[InlineData("ValueTask", "new Option<int>.Some(1)", 1)]
[InlineData("Task", "new Option<int>.None()", 0)]
[InlineData("ValueTask", "new Option<int>.None()", 0)]
public async Task SupportsUnionsWithSingleTypeParameter(
public async Task SupportsAsyncMatchFunctionsForUnionsWithSingleTypeParameter(
string taskType,
string optionDeclaration,
int expectedValue
Expand All @@ -36,6 +36,56 @@ partial record None();
async static Task<int> GetValueAsync() =>
await GetOptionAsync().MatchAsync(some => some.Value, none => 0);
async static {taskType}<Option<int>> GetOptionAsync() =>
await Task.FromResult({optionDeclaration});
";

// Act.
var result = Compile.ToAssembly(optionCs, programCs);
var value = await result.Assembly!.ExecuteStaticAsyncMethod<int>("GetValueAsync");

// Assert.
result.CompilationErrors.Should().BeEmpty();
result.GenerationDiagnostics.Should().BeEmpty();
value.Should().Be(expectedValue);
}

[Theory]
[InlineData("Task", "new Option<int>.Some(1)", 1)]
[InlineData("ValueTask", "new Option<int>.Some(1)", 1)]
[InlineData("Task", "new Option<int>.None()", 0)]
[InlineData("ValueTask", "new Option<int>.None()", 0)]
public async Task SupportsAsyncMatchActionsForUnionsWithSingleTypeParameter(
string taskType,
string optionDeclaration,
int expectedValue
)
{
// Arrange.
var optionCs =
@"
using Dunet;
namespace GenericsTest;
[Union]
partial record Option<T>
{
partial record Some(T Value);
partial record None();
}";
var programCs =
@$"
using System.Threading.Tasks;
using GenericsTest;
async static Task<int> GetValueAsync()
{{
var value = 0;
await GetOptionAsync().MatchAsync(some => value = some.Value, none => value = 0);
return value;
}}
async static {taskType}<Option<int>> GetOptionAsync() =>
await Task.FromResult({optionDeclaration});
";
Expand Down
108 changes: 108 additions & 0 deletions test/GenerateUnionExtensions/GenericMatchAsyncMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,59 @@ await GetOptionAsync()
value.Should().Be(expectedValue);
}

[Theory]
[InlineData("Task", "new Option<int>.Some(1)", 1)]
[InlineData("ValueTask", "new Option<int>.Some(2)", 2)]
[InlineData("Task", "new Option<int>.None()", 0)]
[InlineData("ValueTask", "new Option<int>.None()", 0)]
public async Task GenericMatchAsyncCallsCorrectActionArgument(
string taskType,
string optionDeclaration,
int expectedValue
)
{
// Arrange.
const string optionCs =
@"
using Dunet;
namespace GenericsTest;
[Union]
partial record Option<T>
{
partial record Some(T Value);
partial record None();
}";

var programCs =
@$"
using System.Threading.Tasks;
using GenericsTest;
async static {taskType}<Option<int>> GetOptionAsync()
{{
await Task.Delay(0);
return {optionDeclaration};
}}
async static Task<int> GetValueAsync()
{{
var value = 0;
await GetOptionAsync()
.MatchAsync(some => value = some.Value, none => value = 0);
return value;
}}";
// Act.
var result = Compile.ToAssembly(optionCs, programCs);
var value = await result.Assembly!.ExecuteStaticAsyncMethod<int>("GetValueAsync");

// Assert.
result.CompilationErrors.Should().BeEmpty();
result.GenerationErrors.Should().BeEmpty();
value.Should().Be(expectedValue);
}

[Theory]
[InlineData("Task", "new Result<string, double>.Success(1d)", "1")]
[InlineData("ValueTask", "new Result<string, double>.Success(1d)", "1")]
Expand Down Expand Up @@ -106,4 +159,59 @@ await GetResultAsync()
result.GenerationErrors.Should().BeEmpty();
value.Should().Be(expectedValue);
}

[Theory]
[InlineData("Task", "new Result<string, double>.Success(1d)", "1")]
[InlineData("ValueTask", "new Result<string, double>.Success(1d)", "1")]
[InlineData("Task", "new Result<string, double>.Failure(\"Error!\")", "Error!")]
[InlineData("ValueTask", "new Result<string, double>.Failure(\"Error!\")", "Error!")]
public async Task MultiGenericMatchAsyncCallsCorrectActionArgument(
string taskType,
string resultDeclaration,
string expectedValue
)
{
// Arrange.
const string resultCs =
@"
using Dunet;
namespace GenericsTest;
[Union]
partial record Result<TFailure, TSuccess>
{
partial record Success(TSuccess Value);
partial record Failure(TFailure Error);
}";

var programCs =
@$"
using System;
using System.Threading.Tasks;
using GenericsTest;
async static {taskType}<Result<string, double>> GetResultAsync()
{{
await Task.Delay(0);
return {resultDeclaration};
}}
async static Task<string> GetValueAsync()
{{
var value = """";
await GetResultAsync()
.MatchAsync(success => {{ value = success.Value.ToString(); }}, failure => {{ value = failure.Error; }});
return value;
}}";

// Act.
var result = Compile.ToAssembly(resultCs, programCs);
var value = await result.Assembly!.ExecuteStaticAsyncMethod<string>("GetValueAsync");

// Assert.
result.CompilationErrors.Should().BeEmpty();
result.GenerationErrors.Should().BeEmpty();
value.Should().Be(expectedValue);
}
}
Loading

0 comments on commit 26a4bff

Please sign in to comment.