-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add source generator with basic attribute generation #1
Open
erictuvesson
wants to merge
1
commit into
main
Choose a base branch
from
feature/attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Source Generator issues | ||
## Summary | ||
[Source Generators Cookbook](https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md) | ||
|
||
When working with the source generators I hit a lot of issues. | ||
Here is a collection of the issues I hit which forced me to make | ||
other design decisions which I wouldn't otherwise. | ||
|
||
## Issues | ||
* Can only read the currently referenced project, would be nice to get a better overview of type usage. | ||
* Unable to get more information from external attributes, than the name and argument list. | ||
* Would be nice to be able to visit the syntax nodes in external libraries too. | ||
* Only option I see here is to use reflection, but that creates other issues. |
29 changes: 29 additions & 0 deletions
29
src/Mallos.Searchable.CodeAnalyzer/Mallos.Searchable.CodeAnalyzer.csproj
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,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<LangVersion>8.0</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Mallos.Searchable\Mallos.Searchable.csproj" GeneratePathProperty="true" PrivateAssets="all" AdditionalProperties="TargetFramework=netstandard2.0" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn> | ||
</PropertyGroup> | ||
|
||
<Target Name="GetDependencyTargetPaths"> | ||
<ItemGroup> | ||
<TargetPathWithTargetPlatformMoniker Include="..\..\src\Mallos.Searchable\bin\Debug\netstandard2.0\Mallos.Searchable.dll" IncludeRuntimeDependency="false" /> | ||
</ItemGroup> | ||
</Target> | ||
</Project> |
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,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Mallos.Searchable.CodeAnalyzer | ||
{ | ||
static class ReflectionHelper | ||
{ | ||
public static IEnumerable<Type> AllOfType<T>() | ||
{ | ||
return Assembly.GetAssembly(typeof(T)) | ||
.GetTypes() | ||
.Where(x => x.IsSubclassOf(typeof(T))); | ||
} | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
src/Mallos.Searchable.CodeAnalyzer/SearchableSourceGenerator.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,114 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Text; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Mallos.Searchable.CodeAnalyzer | ||
{ | ||
[Generator] | ||
public class SearchableSourceGenerator : ISourceGenerator | ||
{ | ||
private readonly List<string> filterNames = new List<string>(); | ||
|
||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
context.RegisterForSyntaxNotifications(() => new SearchableSyntaxReceiver()); | ||
} | ||
|
||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
filterNames.Clear(); | ||
|
||
SearchableSyntaxReceiver syntaxReceiver = (SearchableSyntaxReceiver)context.SyntaxReceiver; | ||
Generate(context, syntaxReceiver); | ||
} | ||
|
||
private void Generate(GeneratorExecutionContext context, SearchableSyntaxReceiver syntaxReceiver) | ||
{ | ||
var sb = new StringBuilder(); | ||
sb.AppendLine("using Mallos.Searchable;"); | ||
sb.AppendLine("using Mallos.Searchable.Attributes;"); | ||
sb.AppendLine("using System;"); | ||
sb.AppendLine("using System.Collections.Generic;"); | ||
|
||
foreach (var ns in syntaxReceiver.Generators.Select(x => x.ClassNamespace).Distinct()) | ||
{ | ||
sb.AppendLine($"using {ns};"); | ||
} | ||
|
||
foreach (var node in syntaxReceiver.Generators) | ||
{ | ||
foreach (var member in node.Members) | ||
{ | ||
if (member.Member is PropertyDeclarationSyntax property) | ||
{ | ||
GenerateClass(sb, node.TargetClassName, node.ClassName, property.Identifier.ValueText, member); | ||
} | ||
|
||
if (member.Member is FieldDeclarationSyntax field) | ||
{ | ||
GenerateClass(sb, node.TargetClassName, node.ClassName, field.Declaration.Type.ToString(), member); | ||
} | ||
} | ||
|
||
sb.AppendLine($@" | ||
public class {node.TargetClassName} : Searchable<{node.ClassName}> | ||
{{ | ||
public {node.TargetClassName}() | ||
{{ | ||
{string.Join("\n", filterNames.Select(x => $" Filters.Add(new {x}());"))} | ||
}} | ||
|
||
protected override IEnumerable<{node.ClassName}> FreeTextFilter( | ||
IEnumerable<{node.ClassName}> values, bool negative, string text) | ||
{{ | ||
return base.FreeTextFilter(values, negative, text); | ||
}} | ||
}}"); | ||
} | ||
|
||
string finalSource = sb.ToString(); | ||
|
||
SourceText sourceText = SourceText.From(finalSource, Encoding.UTF8); | ||
|
||
context.AddSource("GeneratedSearchables.cs", sourceText); | ||
} | ||
|
||
private void GenerateClass( | ||
StringBuilder sb, | ||
string generatorName, | ||
string targetName, | ||
string identifierName, | ||
GeneratorNodeAttributeGroup member) | ||
{ | ||
foreach (var attr in member.Attributes) | ||
{ | ||
var keyName = attr.GetKey() ?? identifierName; | ||
var filterClassName = $"{generatorName}Filter_{attr.Rule.Name}_{keyName}"; | ||
filterNames.Add(filterClassName); | ||
|
||
var arguments = string.Join(",\n ", attr.Rule.ArgumentList.Arguments); | ||
var argumentsText = arguments.Length > 0 | ||
? $",\n {arguments}" | ||
: string.Empty; | ||
|
||
sb.AppendLine($@" | ||
public class {filterClassName} : SimpleFilter<{targetName}> | ||
{{ | ||
public override string Key => ""{keyName}""; | ||
|
||
protected override bool Check({targetName} item, string value) | ||
{{ | ||
return {attr.RuleType}.Execute( | ||
item.{identifierName}, | ||
value{argumentsText} | ||
); | ||
}} | ||
}}"); | ||
} | ||
} | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
src/Mallos.Searchable.CodeAnalyzer/SearchableSyntaxReceiver.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,149 @@ | ||
namespace Mallos.Searchable.CodeAnalyzer | ||
{ | ||
using Mallos.Searchable.Attributes; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
class GeneratorNodeAttribute | ||
{ | ||
public AttributeSyntax Key { get; } | ||
|
||
public AttributeSyntax Rule { get; } | ||
|
||
public Type RuleType { get; } | ||
|
||
public GeneratorNodeAttribute(AttributeSyntax key, AttributeSyntax rule, Type ruleType) | ||
{ | ||
this.Key = key; | ||
this.Rule = rule; | ||
this.RuleType = ruleType; | ||
} | ||
|
||
public string GetKey() | ||
{ | ||
if (this.Rule == null) | ||
return null; | ||
|
||
return this.Key.ArgumentList.Arguments[0].ToString().Replace("\"", ""); | ||
} | ||
} | ||
|
||
class GeneratorNodeAttributeGroup | ||
{ | ||
public MemberDeclarationSyntax Member { get; } | ||
|
||
public List<GeneratorNodeAttribute> Attributes { get; } | ||
|
||
public GeneratorNodeAttributeGroup(MemberDeclarationSyntax member, List<GeneratorNodeAttribute> attributes) | ||
{ | ||
this.Member = member; | ||
this.Attributes = attributes; | ||
} | ||
} | ||
|
||
class GeneratorNode | ||
{ | ||
public AttributeSyntax FilterGenerator { get; } | ||
|
||
public TypeDeclarationSyntax ObjectSyntax { get; } | ||
|
||
public GeneratorNodeAttributeGroup[] Members { get; } | ||
|
||
public string ClassNamespace => "Mallos.Searchable.Test"; | ||
|
||
public string ClassName => this.ObjectSyntax.Identifier.ValueText; | ||
|
||
public string TargetClassName { get; } | ||
|
||
public GeneratorNode( | ||
AttributeSyntax filterGenerator, | ||
TypeDeclarationSyntax objectSyntax, | ||
GeneratorNodeAttributeGroup[] members) | ||
{ | ||
this.FilterGenerator = filterGenerator; | ||
this.ObjectSyntax = objectSyntax; | ||
this.Members = members; | ||
|
||
// Process Attribute | ||
var filterGeneratorAttribute = this.FilterGenerator.ArgumentList.Arguments[0]; | ||
this.TargetClassName = filterGeneratorAttribute.ToString().Replace("\"", ""); | ||
} | ||
} | ||
|
||
class SearchableSyntaxReceiver : ISyntaxReceiver | ||
{ | ||
public List<GeneratorNode> Generators { get; private set; } = new List<GeneratorNode>(); | ||
|
||
private readonly List<Type> filterAttributes; | ||
|
||
public SearchableSyntaxReceiver() | ||
{ | ||
this.filterAttributes = ReflectionHelper.AllOfType<FilterBaseAttribute>().ToList(); | ||
} | ||
|
||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode) | ||
{ | ||
if (syntaxNode is ClassDeclarationSyntax | ||
|| syntaxNode is StructDeclarationSyntax | ||
|| syntaxNode is RecordDeclarationSyntax) | ||
{ | ||
var syntax = (TypeDeclarationSyntax)syntaxNode; | ||
|
||
var attr = GetAttribute(syntax, "FilterGenerator"); | ||
if (attr != null) | ||
{ | ||
var members = syntax.Members | ||
.Where(x => x.IsKind(SyntaxKind.PropertyDeclaration) || x.IsKind(SyntaxKind.FieldDeclaration)) | ||
.Select(x => new GeneratorNodeAttributeGroup(x, GetFilterAttributes(x))) | ||
.ToArray(); | ||
|
||
Generators.Add(new GeneratorNode(attr, syntax, members)); | ||
} | ||
} | ||
} | ||
|
||
private List<GeneratorNodeAttribute> GetFilterAttributes(MemberDeclarationSyntax syntax) | ||
{ | ||
var attrs = new List<GeneratorNodeAttribute>(); | ||
if (syntax.AttributeLists != null) | ||
{ | ||
foreach (var list in syntax.AttributeLists) | ||
{ | ||
AttributeSyntax keyAttr = null; | ||
AttributeSyntax filterAttr = null; | ||
Type filterAttrType = null; | ||
foreach (var attr in list.Attributes) | ||
{ | ||
if (attr.Name.ToString().Contains("FilterKey")) | ||
keyAttr = attr; | ||
|
||
var foundFilterType = filterAttributes.Find(x => x.Name.Contains(attr.Name.ToString())); | ||
if (foundFilterType != null) | ||
{ | ||
filterAttr = attr; | ||
filterAttrType = foundFilterType; | ||
} | ||
} | ||
attrs.Add(new GeneratorNodeAttribute(keyAttr, filterAttr, filterAttrType)); | ||
} | ||
} | ||
return attrs; | ||
} | ||
|
||
private static AttributeSyntax GetAttribute(TypeDeclarationSyntax syntax, string name) | ||
{ | ||
if (syntax.AttributeLists != null) | ||
{ | ||
foreach (var list in syntax.AttributeLists) | ||
foreach (var attr in list.Attributes) | ||
if (attr.Name.ToString() == name) | ||
return attr; | ||
} | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Mallos.Searchable.Attributes | ||
{ | ||
using System; | ||
|
||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = true)] | ||
public abstract class FilterBaseAttribute : Attribute | ||
{ | ||
} | ||
} |
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,16 @@ | ||
namespace Mallos.Searchable.Attributes | ||
{ | ||
using System; | ||
|
||
// TODO: Add record for .NET 5 | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] | ||
public sealed class FilterGenerator : Attribute | ||
{ | ||
public string ClassName { get; } | ||
|
||
public FilterGenerator(string className) | ||
{ | ||
this.ClassName = className; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to be able to remove this, then we don't have to depend on
Mallos.Searchable
project.But how can it work without using Reflection if we don't know more about the syntax?