Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Mallos.Searchable.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mallos.Searchable", "src\Ma
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{D375808D-C9F7-4748-9253-BF93762A3E74}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mallos.Searchable.Test", "test\Mallos.Searchable.Test\Mallos.Searchable.Test.csproj", "{54CF0052-174A-43EE-9B57-6B940BC7DB59}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mallos.Searchable.Test", "test\Mallos.Searchable.Test\Mallos.Searchable.Test.csproj", "{54CF0052-174A-43EE-9B57-6B940BC7DB59}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8406D9E0-8BD7-47CD-955E-3DD53500F70B}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
README.md = README.md
source-generator-issues.md = source-generator-issues.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mallos.Searchable.CodeAnalyzer", "src\Mallos.Searchable.CodeAnalyzer\Mallos.Searchable.CodeAnalyzer.csproj", "{D2510BCB-B1F5-4409-86AD-54CD7CBE1C7B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mallos.Searchable.TestData", "test\Mallos.Searchable.TestData\Mallos.Searchable.TestData.csproj", "{81C8953E-24EA-4C91-BD34-29DB60F9CF1C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -31,13 +36,23 @@ Global
{54CF0052-174A-43EE-9B57-6B940BC7DB59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{54CF0052-174A-43EE-9B57-6B940BC7DB59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{54CF0052-174A-43EE-9B57-6B940BC7DB59}.Release|Any CPU.Build.0 = Release|Any CPU
{D2510BCB-B1F5-4409-86AD-54CD7CBE1C7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2510BCB-B1F5-4409-86AD-54CD7CBE1C7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2510BCB-B1F5-4409-86AD-54CD7CBE1C7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2510BCB-B1F5-4409-86AD-54CD7CBE1C7B}.Release|Any CPU.Build.0 = Release|Any CPU
{81C8953E-24EA-4C91-BD34-29DB60F9CF1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{81C8953E-24EA-4C91-BD34-29DB60F9CF1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{81C8953E-24EA-4C91-BD34-29DB60F9CF1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{81C8953E-24EA-4C91-BD34-29DB60F9CF1C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{16741BDE-60CD-48BA-9ADE-992B6906E5DB} = {E85FD8AD-8255-4D2C-99BD-2750358B7074}
{54CF0052-174A-43EE-9B57-6B940BC7DB59} = {D375808D-C9F7-4748-9253-BF93762A3E74}
{D2510BCB-B1F5-4409-86AD-54CD7CBE1C7B} = {E85FD8AD-8255-4D2C-99BD-2750358B7074}
{81C8953E-24EA-4C91-BD34-29DB60F9CF1C} = {D375808D-C9F7-4748-9253-BF93762A3E74}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C40FF5E9-F426-4AC5-B158-22E7487733C4}
Expand Down
13 changes: 13 additions & 0 deletions source-generator-issues.md
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.
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>
17 changes: 17 additions & 0 deletions src/Mallos.Searchable.CodeAnalyzer/ReflectionHelper.cs
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 src/Mallos.Searchable.CodeAnalyzer/SearchableSourceGenerator.cs
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 src/Mallos.Searchable.CodeAnalyzer/SearchableSyntaxReceiver.cs
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();
Copy link
Member Author

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?

}

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;
}
}
}
9 changes: 9 additions & 0 deletions src/Mallos.Searchable/Attributes/FilterBaseAttribute.cs
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
{
}
}
16 changes: 16 additions & 0 deletions src/Mallos.Searchable/Attributes/FilterGenerator.cs
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;
}
}
}
Loading