Check if Azure Function method is async before adding output binding (#1297)

* check async before adding output binding

* nit
This commit is contained in:
Lucy Zhang
2021-11-11 15:48:44 -08:00
committed by GitHub
parent 8487703c21
commit aa902b78d8
7 changed files with 174 additions and 5 deletions

View File

@@ -64,7 +64,7 @@ namespace Microsoft.SqlTools.ServiceLayer.AzureFunctions
}
MethodDeclarationSyntax azureFunction = matchingMethods.First();
var newParam = this.Parameters.bindingType == BindingType.input ? this.GenerateInputBinding() : this.GenerateOutputBinding();
var newParam = this.Parameters.bindingType == BindingType.input ? this.GenerateInputBinding() : this.GenerateOutputBinding(azureFunction);
// Generate updated method with the new parameter
// normalizewhitespace gets rid of any newline whitespace in the leading trivia, so we add that back
@@ -128,8 +128,12 @@ namespace Microsoft.SqlTools.ServiceLayer.AzureFunctions
/// <summary>
/// Generates a parameter for the sql output binding that looks like
/// [Sql("[dbo].[table1]", ConnectionStringSetting = "SqlConnectionString")] out Object output
/// if the Azure Function method is not async and
/// [Sql("[dbo].[table1]", ConnectionStringSetting = "SqlConnectionString")] IAsyncCollector<Object> output
/// if the Azure Function method is async.
/// <param name="azureFunction"> Azure Function method. </param>
/// </summary>
private ParameterSyntax GenerateOutputBinding()
private ParameterSyntax GenerateOutputBinding(MethodDeclarationSyntax azureFunction)
{
// Create arguments for the Sql Output Binding attribute
var argumentList = SyntaxFactory.AttributeArgumentList();
@@ -140,9 +144,22 @@ namespace Microsoft.SqlTools.ServiceLayer.AzureFunctions
attributesList = attributesList.Add(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList<AttributeSyntax>(SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("Sql")).WithArgumentList(argumentList))));
var syntaxTokenList = new SyntaxTokenList();
syntaxTokenList = syntaxTokenList.Add(SyntaxFactory.Token(SyntaxKind.OutKeyword));
TypeSyntax typeSyntax;
ParameterSyntax newParam = SyntaxFactory.Parameter(attributesList, syntaxTokenList, SyntaxFactory.ParseTypeName(typeof(Object).Name), SyntaxFactory.Identifier("output"), null);
// Check if Azure Function method is async
IEnumerable<SyntaxToken> asyncModifier = azureFunction.Modifiers.Where(m => m.ToString() == "async");
bool asyncMethod = asyncModifier.Count() > 0;
if (asyncMethod)
{
typeSyntax = SyntaxFactory.ParseTypeName("IAsyncCollector<Object>");
} else
{
syntaxTokenList = syntaxTokenList.Add(SyntaxFactory.Token(SyntaxKind.OutKeyword));
typeSyntax = SyntaxFactory.ParseTypeName(typeof(Object).Name);
}
ParameterSyntax newParam = SyntaxFactory.Parameter(attributesList, syntaxTokenList, typeSyntax, SyntaxFactory.Identifier("output"), null);
return newParam;
}
}