-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create system to register LiteralConverters
This will make it much easier for extensions to register their literal converters. Also, refactor `DefaultTypeSerializerEnricher` to implement `IResourceFileEnricher` executed by the `ResourceFileCompilationEnricher`.
- Loading branch information
1 parent
f18e744
commit 3ade6a2
Showing
7 changed files
with
86 additions
and
58 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
39 changes: 39 additions & 0 deletions
39
src/main/Yardarm/Enrichment/Compilation/DefaultLiteralConvertersEnricher.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,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Yardarm.Enrichment.Compilation; | ||
|
||
public class DefaultLiteralConvertersEnricher( | ||
IEnumerable<IDefaultLiteralConverterEnricher> createDefaultRegistryEnrichers) | ||
: IResourceFileEnricher | ||
{ | ||
public bool ShouldEnrich(string resourceName) => | ||
resourceName == "Yardarm.Client.Serialization.Literals.LiteralConverterRegistry.cs"; | ||
|
||
public CompilationUnitSyntax Enrich(CompilationUnitSyntax target, ResourceFileEnrichmentContext context) | ||
{ | ||
ClassDeclarationSyntax? classDeclaration = target | ||
.DescendantNodes() | ||
.OfType<ClassDeclarationSyntax>() | ||
.FirstOrDefault(p => p.Identifier.ValueText == "LiteralConverterRegistry"); | ||
|
||
MethodDeclarationSyntax? methodDeclaration = classDeclaration? | ||
.ChildNodes() | ||
.OfType<MethodDeclarationSyntax>() | ||
.FirstOrDefault(p => p.Identifier.ValueText == "CreateDefaultRegistry"); | ||
|
||
if (methodDeclaration?.ExpressionBody != null) | ||
{ | ||
MethodDeclarationSyntax newMethodDeclaration = methodDeclaration.WithExpressionBody( | ||
methodDeclaration.ExpressionBody.WithExpression( | ||
methodDeclaration.ExpressionBody.Expression.Enrich(createDefaultRegistryEnrichers))); | ||
|
||
target = target.ReplaceNode(methodDeclaration, newMethodDeclaration); | ||
} | ||
|
||
return target; | ||
} | ||
} |
74 changes: 25 additions & 49 deletions
74
src/main/Yardarm/Enrichment/Compilation/DefaultTypeSerializersEnricher.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 |
---|---|---|
@@ -1,62 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Yardarm.Enrichment.Compilation | ||
namespace Yardarm.Enrichment.Compilation; | ||
|
||
public class DefaultTypeSerializersEnricher( | ||
IEnumerable<ICreateDefaultRegistryEnricher> createDefaultRegistryEnrichers) : | ||
IResourceFileEnricher | ||
{ | ||
public class DefaultTypeSerializersEnricher : ICompilationEnricher | ||
{ | ||
private readonly IList<ICreateDefaultRegistryEnricher> _createDefaultRegistryEnrichers; | ||
public bool ShouldEnrich(string resourceName) => | ||
resourceName == "Yardarm.Client.Serialization.TypeSerializerRegistry.cs"; | ||
|
||
public Type[] ExecuteAfter { get; } = | ||
{ | ||
typeof(SyntaxTreeCompilationEnricher) | ||
}; | ||
public CompilationUnitSyntax Enrich(CompilationUnitSyntax target, ResourceFileEnrichmentContext context) | ||
{ | ||
ClassDeclarationSyntax? classDeclaration = target | ||
.DescendantNodes() | ||
.OfType<ClassDeclarationSyntax>() | ||
.FirstOrDefault(p => p.Identifier.ValueText == "TypeSerializerRegistry"); | ||
|
||
public DefaultTypeSerializersEnricher( | ||
IEnumerable<ICreateDefaultRegistryEnricher> createDefaultRegistryEnrichers) | ||
{ | ||
_createDefaultRegistryEnrichers = createDefaultRegistryEnrichers.ToArray(); | ||
} | ||
MethodDeclarationSyntax? methodDeclaration = classDeclaration? | ||
.ChildNodes() | ||
.OfType<MethodDeclarationSyntax>() | ||
.FirstOrDefault(p => p.Identifier.ValueText == "CreateDefaultRegistry"); | ||
|
||
public async ValueTask<CSharpCompilation> EnrichAsync(CSharpCompilation target, | ||
CancellationToken cancellationToken = default) | ||
if (methodDeclaration?.ExpressionBody != null) | ||
{ | ||
foreach (SyntaxTree syntaxTree in target.SyntaxTrees) | ||
{ | ||
SyntaxNode rootNode = await syntaxTree.GetRootAsync(cancellationToken) | ||
.ConfigureAwait(false); | ||
MethodDeclarationSyntax newMethodDeclaration = methodDeclaration.WithExpressionBody( | ||
methodDeclaration.ExpressionBody.WithExpression( | ||
methodDeclaration.ExpressionBody.Expression.Enrich(createDefaultRegistryEnrichers))); | ||
|
||
ClassDeclarationSyntax? classDeclaration = rootNode | ||
.DescendantNodes() | ||
.OfType<ClassDeclarationSyntax>() | ||
.FirstOrDefault(p => p.Identifier.ValueText == "TypeSerializerRegistry"); | ||
|
||
MethodDeclarationSyntax? methodDeclaration = classDeclaration? | ||
.ChildNodes() | ||
.OfType<MethodDeclarationSyntax>() | ||
.FirstOrDefault(p => p.Identifier.ValueText == "CreateDefaultRegistry"); | ||
|
||
if (methodDeclaration?.ExpressionBody != null) | ||
{ | ||
MethodDeclarationSyntax newMethodDeclaration = methodDeclaration.WithExpressionBody( | ||
methodDeclaration.ExpressionBody.WithExpression( | ||
methodDeclaration.ExpressionBody.Expression.Enrich(_createDefaultRegistryEnrichers))); | ||
|
||
rootNode = rootNode.ReplaceNode(methodDeclaration, newMethodDeclaration); | ||
|
||
// Break once we've found what we're looking for | ||
return target.ReplaceSyntaxTree(syntaxTree, | ||
syntaxTree.WithRootAndOptions(rootNode, syntaxTree.Options)); | ||
} | ||
} | ||
|
||
return target; | ||
target = target.ReplaceNode(methodDeclaration, newMethodDeclaration); | ||
} | ||
|
||
return target; | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/Yardarm/Enrichment/IDefaultLiteralConverterEnricher.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,10 @@ | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Yardarm.Enrichment; | ||
|
||
/// <summary> | ||
/// Enriches the body of the CreateDefaultRegistry method in the LiteralConverterRegistry class. | ||
/// </summary> | ||
public interface IDefaultLiteralConverterEnricher : IEnricher<ExpressionSyntax> | ||
{ | ||
} |