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

Add additional generator options #9

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: added method to get the access modifier value
amyskippy committed Oct 2, 2024
commit 8dab45155f8e4dc88b025b4e002a74cf56ad9c71
38 changes: 38 additions & 0 deletions MapDataReader/MapperGenerator.cs
Original file line number Diff line number Diff line change
@@ -130,6 +130,44 @@ public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new TargetTypeTracker());
}

private string GetAccessModifer(ClassDeclarationSyntax typeNode)
{
// Retrieve the attribute list
var attributeList = typeNode.AttributeLists
.SelectMany(al => al.Attributes)
.FirstOrDefault(attr => attr.Name.ToString() == "GenerateDataReaderMapper");

if (attributeList?.ArgumentList == null)
return "public";

var arguments = attributeList.ArgumentList.Arguments;

if (arguments.Count == 0)
return "public";

if (arguments.Count == 1)
{
var argumentExpr = arguments[0].Expression as LiteralExpressionSyntax;
return argumentExpr?.Token.ValueText ?? "public";
}

foreach (var argument in arguments)
{
// Check if the argument is a named argument
if (argument is AttributeArgumentSyntax attributeArgument)
{
var nameEquals = attributeArgument.NameEquals;
if (nameEquals?.Name.Identifier.Text == "AccessModifier")
{
var argumentExpr = argument.Expression as LiteralExpressionSyntax;
return argumentExpr?.Token.ValueText ?? "public";
}
}
}

return "public";
}
}

internal class TargetTypeTracker : ISyntaxContextReceiver