added source generator for Combine methods
All checks were successful
.NET Test / test (push) Successful in 1m6s

This commit is contained in:
2023-11-28 18:16:50 +04:00
parent db2fa49ff5
commit ac18863426
7 changed files with 256 additions and 1057 deletions

View File

@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Railway", "Railway\Railway.
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raliway.Tests", "Raliway.Tests\Raliway.Tests.csproj", "{607F91E4-83A2-48C4-BAC2-2205BEE81D93}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raliway.Tests", "Raliway.Tests\Raliway.Tests.csproj", "{607F91E4-83A2-48C4-BAC2-2205BEE81D93}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Railway.SourceGenerator", "Railway.SourceGenerator\Railway.SourceGenerator.csproj", "{1A3B8F0A-7A30-4AA8-BC15-47FA2D75B6BF}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -24,5 +26,9 @@ Global
{607F91E4-83A2-48C4-BAC2-2205BEE81D93}.Debug|Any CPU.Build.0 = Debug|Any CPU {607F91E4-83A2-48C4-BAC2-2205BEE81D93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{607F91E4-83A2-48C4-BAC2-2205BEE81D93}.Release|Any CPU.ActiveCfg = Release|Any CPU {607F91E4-83A2-48C4-BAC2-2205BEE81D93}.Release|Any CPU.ActiveCfg = Release|Any CPU
{607F91E4-83A2-48C4-BAC2-2205BEE81D93}.Release|Any CPU.Build.0 = Release|Any CPU {607F91E4-83A2-48C4-BAC2-2205BEE81D93}.Release|Any CPU.Build.0 = Release|Any CPU
{1A3B8F0A-7A30-4AA8-BC15-47FA2D75B6BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A3B8F0A-7A30-4AA8-BC15-47FA2D75B6BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A3B8F0A-7A30-4AA8-BC15-47FA2D75B6BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A3B8F0A-7A30-4AA8-BC15-47FA2D75B6BF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@@ -0,0 +1,7 @@
namespace Just.Railway.SourceGen;
internal static class Constants
{
public const int MaxResultTupleSize = 4;
}

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
<RootNamespace>Just.Railway.SourceGen</RootNamespace>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,214 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
namespace Just.Railway.SourceGen;
[Generator]
public class ResultCombineGenerator : IIncrementalGenerator
{
public ResultCombineGenerator()
{
}
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterSourceOutput(context.CompilationProvider, Execute);
}
private void Execute(SourceProductionContext context, Compilation source)
{
var methods = GenerateCombineMethods();
var code = $$"""
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.CodeDom.Compiler;
namespace Just.Railway;
public readonly partial struct Result
{
{{methods}}
}
""";
context.AddSource("Result.Combine.g.cs", code);
}
private string GenerateCombineMethods()
{
var sb = new StringBuilder();
for (int i = 2; i <= Constants.MaxResultTupleSize; i++)
{
GenerateCombineMethodsForArgCount(sb, argCount: i);
}
return sb.ToString();
}
private void GenerateCombineMethodsForArgCount(StringBuilder sb, int argCount)
{
sb.AppendLine($"#region Combine {argCount} Results");
GenerateGetBottomMethod(sb, argCount);
var argsResultTupleSizes = new List<ImmutableArray<int>>();
Span<int> templateCounts = stackalloc int[argCount];
Permute(templateCounts, argsResultTupleSizes);
foreach (var argResultTupleSizes in argsResultTupleSizes)
{
sb.AppendLine(GenerateCombineMethodBody(argResultTupleSizes));
}
sb.AppendLine("#endregion");
static void Permute(Span<int> templateCounts, ICollection<ImmutableArray<int>> argsResultTupleSizes, int lvl = 0)
{
int sum = 0;
for (int i = 0; i < lvl; i++)
{
sum += templateCounts[i];
}
for (templateCounts[lvl] = 0; templateCounts[lvl] <= Constants.MaxResultTupleSize - sum; templateCounts[lvl]++)
{
if (lvl == templateCounts.Length - 1)
{
argsResultTupleSizes.Add(templateCounts.ToImmutableArray());
continue;
}
Permute(templateCounts, argsResultTupleSizes, lvl + 1);
}
}
}
private static void GenerateGetBottomMethod(StringBuilder sb, int argCount)
{
var args = Enumerable.Range(1, argCount)
.Select(i => $"result{i}")
.ToImmutableArray();
var argsDecl = string.Join(", ", args.Select(x => $"ResultState {x}"));
sb.AppendLine($"[GeneratedCodeAttribute(\"{nameof(ResultCombineGenerator)}\", \"1.0.0.0\")]");
sb.AppendLine($"private static IEnumerable<string> GetBottom({argsDecl})");
sb.AppendLine("{");
foreach (var arg in args)
{
sb.AppendLine($" if ({arg} == ResultState.Bottom) yield return \"{arg}\";");
}
sb.AppendLine("}");
}
private string GenerateCombineMethodBody(ImmutableArray<int> argResultTupleSizes)
{
var resultTupleSize = argResultTupleSizes.Sum();
var paramNames = Enumerable.Range(1, argResultTupleSizes.Length)
.Select(i => $"result{i}")
.ToImmutableArray();
var templateArgNames = Enumerable.Range(1, resultTupleSize)
.Select(i => $"T{i}")
.ToImmutableArray();
string templateDecl = templateArgNames.IsEmpty
? string.Empty
: $"<{string.Join(", ", templateArgNames)}>";
string resultTypeDecl = GetResultTypeDecl(templateArgNames);
string paramDecl;
{
var paramDeclBuilder = new StringBuilder();
int currentTemplateArg = 0;
for (int i = 0; i < argResultTupleSizes.Length; i++)
{
var argResultTupleSize = argResultTupleSizes[i];
string currentParamType = GetResultTypeDecl(templateArgNames.Slice(currentTemplateArg, argResultTupleSize));
currentTemplateArg += argResultTupleSize;
paramDeclBuilder.Append($"in {currentParamType} {paramNames[i]}, ");
}
paramDeclBuilder.Remove(paramDeclBuilder.Length-2, 2);
paramDecl = paramDeclBuilder.ToString();
}
var paramNameStates = paramNames.Select(x => $"{x}.State")
.ToImmutableArray();
string bottomStateCheck = string.Join(" & ", paramNameStates);
string statesSeparatedList = string.Join(", ", paramNameStates);
string failureChecks;
{
var failureChecksBuilder = new StringBuilder();
foreach (var paramName in paramNames)
{
failureChecksBuilder.AppendLine($" if ({paramName}.IsFailure) error += {paramName}.Error;");
}
failureChecks = failureChecksBuilder.ToString();
}
string resultExpansion;
switch (resultTupleSize)
{
case 0:
resultExpansion = "null";
break;
case 1:
resultExpansion = $"{paramNames[argResultTupleSizes.IndexOf(1)]}.Value";
break;
default:
var resultExpansionBuilder = new StringBuilder();
resultExpansionBuilder.Append("(");
for (int i = 0; i < argResultTupleSizes.Length; i++)
{
if (argResultTupleSizes[i] == 0) continue;
if (argResultTupleSizes[i] == 1)
{
resultExpansionBuilder.Append($"{paramNames[i]}.Value, ");
continue;
}
for (int valueIndex = 1; valueIndex <= argResultTupleSizes[i]; valueIndex++)
{
resultExpansionBuilder.Append($"{paramNames[i]}.Value.Item{valueIndex}, ");
}
}
resultExpansionBuilder.Remove(resultExpansionBuilder.Length - 2, 2);
resultExpansionBuilder.Append(")");
resultExpansion = resultExpansionBuilder.ToString();
break;
}
string returnExpr = $"return error is null ? new({resultExpansion}) : new(error);";
var method = $$"""
[GeneratedCodeAttribute("{{nameof(ResultCombineGenerator)}}", "1.0.0.0")]
[PureAttribute]
public static {{resultTypeDecl}} Combine{{templateDecl}}({{paramDecl}})
{
if (({{bottomStateCheck}}) == ResultState.Bottom)
{
throw new ResultNotInitializedException(string.Join(';', GetBottom({{statesSeparatedList}})));
}
Error? error = null;
{{failureChecks}}
{{returnExpr}}
}
""";
return method;
static string GetResultTypeDecl(IReadOnlyList<string> templateArgNames)
{
return templateArgNames.Count switch
{
0 => "Result",
1 => $"Result<{templateArgNames[0]}>",
_ => $"Result<({string.Join(", ", templateArgNames)})>"
};
}
}
}

View File

@@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles>
<AssemblyName>Just.Railway</AssemblyName> <AssemblyName>Just.Railway</AssemblyName>
<RootNamespace>Just.Railway</RootNamespace> <RootNamespace>Just.Railway</RootNamespace>
@@ -19,4 +20,10 @@
<InternalsVisibleTo Include="$(AssemblyName).Tests" /> <InternalsVisibleTo Include="$(AssemblyName).Tests" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Railway.SourceGenerator\Railway.SourceGenerator.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> </Project>

File diff suppressed because it is too large Load Diff