Skip to content

Commit

Permalink
Add generation (and tests) for SubOrchestrators
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartleeks committed Jan 21, 2019
1 parent a826738 commit 9ed4cc3
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public override async Task RunAsync()
var project = workspace.CurrentSolution.Projects.FirstOrDefault(p => p.FilePath == csproj);


var methodsBySemanticModel = await GetActivityFunctionMethods(project);
var activityAndOrchestratorMethods = await GetActivityAndOrchestratorFunctionMethods(project);

var compilationUnitSyntax = GenerateCode(project, methodsBySemanticModel);
var compilationUnitSyntax = GenerateCode(project, activityAndOrchestratorMethods);


var generatedCode = compilationUnitSyntax.ToString();
Expand All @@ -70,9 +70,22 @@ public override async Task RunAsync()
await FileSystemHelpers.WriteAllTextToFileAsync(generatedFilePath, generatedCode);
}

private static bool MethodHasActivityTriggerParameter(IMethodSymbol method)
enum FunctionType
{
return method.Parameters.Any(ParameterIsActivityTrigger);
Unknown,
Activity,
Orchestrator
}
private static FunctionType GetFunctionType(IMethodSymbol method)
{
foreach (var parameter in method.Parameters)
{
if (ParameterIsActivityTrigger(parameter))
return FunctionType.Activity;
if (ParameterIsOrchestratorTrigger(parameter))
return FunctionType.Orchestrator;
}
return FunctionType.Unknown;
}

private static bool ParameterIsActivityTrigger(IParameterSymbol parameter)
Expand All @@ -81,7 +94,14 @@ private static bool ParameterIsActivityTrigger(IParameterSymbol parameter)
.Any(a => a.AttributeClass.ToString() == "Microsoft.Azure.WebJobs.ActivityTriggerAttribute");
}

private static async Task<IEnumerable<IMethodSymbol>> GetActivityFunctionMethods(Project project)
private static bool ParameterIsOrchestratorTrigger(IParameterSymbol parameter)
{
return parameter.Type.ToString() == "Microsoft.Azure.WebJobs.DurableOrchestrationContext"
&& parameter.GetAttributes()
.Any(a => a.AttributeClass.ToString() == "Microsoft.Azure.WebJobs.OrchestrationTriggerAttribute");
}

private static async Task<IEnumerable<(FunctionType functionType, IMethodSymbol method)>> GetActivityAndOrchestratorFunctionMethods(Project project)
{
var attributeType = typeof(FunctionNameAttribute);
string[] attributeNames;
Expand Down Expand Up @@ -124,7 +144,7 @@ bool attributeMatchesName(AttributeSyntax attribute)
var root = await document.GetSyntaxRootAsync();
var model = await document.GetSemanticModelAsync();
IEnumerable<IMethodSymbol> matchingMethods = root.DescendantNodes()
var functionMethods = root.DescendantNodes()
.OfType<AttributeSyntax>()
.Where(attributeMatchesName)
// Project attribute -> attribute list -> method
Expand All @@ -134,9 +154,13 @@ bool attributeMatchesName(AttributeSyntax attribute)
// project to method declaration
.Select(m => model.GetDeclaredSymbol(m))
// filter to just methods with attribute of the correct type
.Where(m => m.GetAttributes().Any(a => a.AttributeClass.ToString() == attributeType.FullName))
.Where(MethodHasActivityTriggerParameter);
return matchingMethods;
.Where(m => m.GetAttributes().Any(a => a.AttributeClass.ToString() == attributeType.FullName));
var actvityAndOrchestratorMethods = functionMethods
.Select(m => (functionType: GetFunctionType(m), method: m ))
.Where(f => f.functionType != FunctionType.Unknown);
return actvityAndOrchestratorMethods;
})
.WaitAllAndUnwrap()
)
Expand Down
Loading

0 comments on commit 9ed4cc3

Please sign in to comment.