Skip to content

Commit 2ba5aa1

Browse files
committed
Generate parameters syntax using Roslyn API
1 parent 712f86d commit 2ba5aa1

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

SqlMarshal/AbstractGenerator.cs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace SqlMarshal;
1111
using System.Text;
1212
using Microsoft.CodeAnalysis;
1313
using Microsoft.CodeAnalysis.CSharp;
14-
using Microsoft.CodeAnalysis.CSharp.Syntax;
1514
using Microsoft.CodeAnalysis.Text;
1615
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1716
using static SqlMarshal.Extensions;
@@ -101,6 +100,15 @@ internal static IEnumerable<string> GetUsings(ClassGenerationContext classGenera
101100
}
102101
}
103102

103+
/// <summary>
104+
/// Gets code for parameter declaration.
105+
/// </summary>
106+
/// <param name="methodSymbol">Method symbol from which we copy parameter.</param>
107+
/// <param name="parameter">Parameter to copy.</param>
108+
/// <param name="index">Index of parameter to copy.</param>
109+
/// <returns>Generated syntax node for the parameter.</returns>
110+
protected abstract SyntaxNode GetParameterDeclaration(IMethodSymbol methodSymbol, IParameterSymbol parameter, int index);
111+
104112
private static string GetAccessibility(Accessibility a)
105113
{
106114
return a switch
@@ -138,26 +146,6 @@ private static string GetAccessibility(Accessibility a)
138146
return null;
139147
}
140148

141-
private static string GetParameterDeclaration(IMethodSymbol methodSymbol, IParameterSymbol parameter, int index)
142-
{
143-
if (parameter.RefKind == RefKind.Out)
144-
{
145-
return $"out {parameter.Type.ToDisplayString()} {parameter.Name}";
146-
}
147-
148-
if (parameter.RefKind == RefKind.Ref)
149-
{
150-
return $"ref {parameter.Type.ToDisplayString()} {parameter.Name}";
151-
}
152-
153-
if (methodSymbol.IsExtensionMethod && index == 0)
154-
{
155-
return $"this {parameter.Type.ToDisplayString()} {parameter.Name}";
156-
}
157-
158-
return $"{parameter.Type.ToDisplayString()} {parameter.Name}";
159-
}
160-
161149
private static string GetParameterPassing(IParameterSymbol parameter)
162150
{
163151
var parameterName = NameMapper.MapName(parameter.Name);
@@ -929,7 +917,7 @@ private void ProcessMethod(
929917
var originalParameters = methodSymbol.Parameters;
930918

931919
bool hasCustomSql = methodGenerationContext.CustomSqlParameter != null;
932-
var signature = $"({string.Join(", ", originalParameters.Select((parameterSymbol, index) => GetParameterDeclaration(methodSymbol, parameterSymbol, index)))})";
920+
var signature = $"({string.Join(", ", originalParameters.Select((parameterSymbol, index) => this.GetParameterDeclaration(methodSymbol, parameterSymbol, index)))})";
933921
var itemType = methodGenerationContext.ItemType;
934922
var getConnection = this.GetConnectionStatement(methodGenerationContext);
935923
var isList = methodGenerationContext.IsList || methodGenerationContext.IsEnumerable;

SqlMarshal/CSharpGenerator.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
namespace SqlMarshal;
88

99
using Microsoft.CodeAnalysis;
10+
using Microsoft.CodeAnalysis.CSharp;
1011
using Microsoft.CodeAnalysis.CSharp.Syntax;
1112
using System.Collections.Generic;
1213
using System.Linq;
14+
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1315

1416
/// <summary>
1517
/// Stored procedures generator for C#.
@@ -64,6 +66,32 @@ public override void Initialize(GeneratorInitializationContext context)
6466
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
6567
}
6668

69+
/// <inheritdoc/>
70+
protected override SyntaxNode GetParameterDeclaration(IMethodSymbol methodSymbol, IParameterSymbol parameter, int index)
71+
{
72+
var typeAsClause = ParseTypeName(parameter.Type.ToDisplayString()).WithTrailingTrivia(Whitespace(" "));
73+
if (parameter.RefKind == RefKind.Out)
74+
{
75+
var parameterSyntax = Parameter(default, SyntaxTokenList.Create(Token(SyntaxKind.OutKeyword).WithTrailingTrivia(Whitespace(" "))), typeAsClause, Identifier(parameter.Name), @default: null);
76+
return parameterSyntax;
77+
}
78+
else if (parameter.RefKind == RefKind.Ref)
79+
{
80+
var parameterSyntax = Parameter(default, SyntaxTokenList.Create(Token(SyntaxKind.RefKeyword).WithTrailingTrivia(Whitespace(" "))), typeAsClause, Identifier(parameter.Name), @default: null);
81+
return parameterSyntax;
82+
}
83+
else if (methodSymbol.IsExtensionMethod && index == 0)
84+
{
85+
var parameterSyntax = Parameter(default, SyntaxTokenList.Create(Token(SyntaxKind.ThisKeyword).WithTrailingTrivia(Whitespace(" "))), typeAsClause, Identifier(parameter.Name), @default: null);
86+
return parameterSyntax;
87+
}
88+
else
89+
{
90+
var parameterSyntax = Parameter(default, default, typeAsClause, Identifier(parameter.Name), @default: null);
91+
return parameterSyntax;
92+
}
93+
}
94+
6795
internal class SyntaxReceiver : ISqlMarshalSyntaxReceiver
6896
{
6997
public List<IMethodSymbol> Methods { get; } = new List<IMethodSymbol>();

SqlMarshal/VisualBasicGenerator.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
namespace SqlMarshal;
88

99
using Microsoft.CodeAnalysis;
10+
using Microsoft.CodeAnalysis.VisualBasic;
1011
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
1112
using System.Collections.Generic;
1213
using System.Linq;
14+
using static Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory;
1315

1416
/// <summary>
1517
/// Stored procedures generator for C#.
@@ -23,6 +25,8 @@ public class VisualBasicGenerator : AbstractGenerator
2325
' regenerated.
2426
' </auto-generated>
2527
28+
Namespace SqlMarshal.Annotations
29+
2630
<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple:=True)>
2731
Friend NotInheritable Class SqlMarshalAttribute
2832
Inherits System.Attribute
@@ -57,6 +61,7 @@ End Sub
5761
Public Property EntityType As System.Type
5862
End Class
5963
64+
End Namespace
6065
";
6166

6267
/// <inheritdoc/>
@@ -69,6 +74,27 @@ public override void Initialize(GeneratorInitializationContext context)
6974
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
7075
}
7176

77+
/// <inheritdoc/>
78+
protected override SyntaxNode GetParameterDeclaration(IMethodSymbol methodSymbol, IParameterSymbol parameter, int index)
79+
{
80+
var typeAsClause = SimpleAsClause(ParseTypeName(parameter.Type.ToDisplayString()).WithLeadingTrivia(Whitespace(" ")));
81+
if (parameter.RefKind == RefKind.Out)
82+
{
83+
var parameterSyntax = Parameter(default, SyntaxTokenList.Create(Token(SyntaxKind.ByRefKeyword).WithTrailingTrivia(Whitespace(" "))), ModifiedIdentifier(parameter.Name).WithTrailingTrivia(Whitespace(" ")), typeAsClause, @default: null);
84+
return parameterSyntax;
85+
}
86+
else if (parameter.RefKind == RefKind.Ref)
87+
{
88+
var parameterSyntax = Parameter(default, SyntaxTokenList.Create(Token(SyntaxKind.ByRefKeyword).WithTrailingTrivia(Whitespace(" "))), ModifiedIdentifier(parameter.Name).WithTrailingTrivia(Whitespace(" ")), typeAsClause, @default: null);
89+
return parameterSyntax;
90+
}
91+
else
92+
{
93+
var parameterSyntax = Parameter(default, default, ModifiedIdentifier(parameter.Name).WithTrailingTrivia(Whitespace(" ")), typeAsClause, @default: null);
94+
return parameterSyntax;
95+
}
96+
}
97+
7298
internal class SyntaxReceiver : ISqlMarshalSyntaxReceiver
7399
{
74100
public List<IMethodSymbol> Methods { get; } = new List<IMethodSymbol>();

0 commit comments

Comments
 (0)