-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Should improve compile time refinement performance * TODO need to add many more tests around that compile time refinement
- Loading branch information
Showing
8 changed files
with
193 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
Tuxedo.SourceGenerator/Generators/RefinementSourceGenerator.RefinementServiceSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Collections.Immutable; | ||
using Tuxedo.SourceGenerator.Extensions; | ||
|
||
namespace Tuxedo.SourceGenerator; | ||
|
||
public sealed partial class RefinementSourceGenerator | ||
{ | ||
private static string RenderRefinementService( | ||
ImmutableArray<RefinedTypeDetails> refinedTypeDetails | ||
) | ||
{ | ||
return $$""" | ||
// <auto-generated/> | ||
#nullable enable | ||
namespace Tuxedo; | ||
/// <summary> | ||
/// Provides compile time support for running refinement predicates against compile time known values | ||
/// </summary> | ||
/// <remarks> | ||
/// This is for compile time use and should not be used in application code | ||
/// </remarks> | ||
internal static class RefinementService | ||
{ | ||
{{string.Join("\n\n", refinedTypeDetails.Select(RenderTestMethod))}} | ||
} | ||
"""; | ||
} | ||
|
||
private static string RenderTestMethod(RefinedTypeDetails model) | ||
{ | ||
return $$""" | ||
private static string? TestAgainst{{model.RefinedType}}{{model.Generics}}(object value){{model.GenericConstraints.PrependIfNotNull( | ||
"\n\t\t" | ||
)}} | ||
{ | ||
return {{(model.IsTuple | ||
? $"!{model.Namespace}.{model.RefinedType}{model.Generics}.TryParse(({model.RawType})value, out _, out var errorMessage) ? errorMessage : null" | ||
: $"value is {model.RawType} rt && !{model.Namespace}.{model.RefinedType}{model.Generics}.TryParse(rt, out _, out var errorMessage) ? errorMessage : null")}}; | ||
} | ||
"""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Tuxedo.Tests/SharedTests.Case1#RefinementService.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//HintName: RefinementService.g.cs | ||
// <auto-generated/> | ||
#nullable enable | ||
|
||
namespace Tuxedo; | ||
|
||
/// <summary> | ||
/// Provides compile time support for running refinement predicates against compile time known values | ||
/// </summary> | ||
/// <remarks> | ||
/// This is for compile time use and should not be used in application code | ||
/// </remarks> | ||
internal static class RefinementService | ||
{ | ||
private static string? TestAgainstTest1(object value) | ||
{ | ||
return value is bool rt && !<global namespace>.Test1.TryParse(rt, out _, out var errorMessage) ? errorMessage : null; | ||
} | ||
|
||
private static string? TestAgainstTest2(object value) | ||
{ | ||
return !<global namespace>.Test2.TryParse(((int a, int b))value, out _, out var errorMessage) ? errorMessage : null; | ||
} | ||
|
||
private static string? TestAgainstTest3<T>(object value) | ||
{ | ||
return value is List<T> rt && !<global namespace>.Test3<T>.TryParse(rt, out _, out var errorMessage) ? errorMessage : null; | ||
} | ||
|
||
private static string? TestAgainstTest4<T>(object value) | ||
where T: struct | ||
{ | ||
return value is List<T> rt && !<global namespace>.Test4<T>.TryParse(rt, out _, out var errorMessage) ? errorMessage : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters