< Summary

Information
Class: Elsa.Expressions.CSharp.Handlers.GenerateWorkflowInputAccessors
Assembly: Elsa.Expressions.CSharp
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.CSharp/Handlers/GenerateWorkflowInputAccessors.cs
Line coverage
3%
Covered lines: 1
Uncovered lines: 26
Coverable lines: 27
Total lines: 57
Line coverage: 3.7%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
HandleAsync(...)0%4260%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.CSharp/Handlers/GenerateWorkflowInputAccessors.cs

#LineLine coverage
 1using System.Text;
 2using Elsa.Expressions.CSharp.Extensions;
 3using Elsa.Expressions.CSharp.Notifications;
 4using Elsa.Expressions.CSharp.Options;
 5using Elsa.Expressions.Models;
 6using Elsa.Extensions;
 7using Elsa.Mediator.Contracts;
 8using JetBrains.Annotations;
 9using Microsoft.Extensions.Options;
 10
 11namespace Elsa.Expressions.CSharp.Handlers;
 12
 13/// <summary>
 14/// Configures the C# evaluator with methods to access workflow variables.
 15/// </summary>
 16[UsedImplicitly]
 31917public class GenerateWorkflowInputAccessors(IOptions<CSharpOptions> options) : INotificationHandler<EvaluatingCSharp>
 18{
 19    /// <inheritdoc />
 20    public Task HandleAsync(EvaluatingCSharp notification, CancellationToken cancellationToken)
 21    {
 022        var expressionExecutionContext = notification.Context;
 23
 024        if (!expressionExecutionContext.TryGetWorkflowExecutionContext(out var workflowExecutionContext))
 025            return Task.CompletedTask;
 26
 027        var workflow = workflowExecutionContext.Workflow;
 028        var inputDefinitions = workflow.Inputs.ToList();
 029        var sb = new StringBuilder();
 030        sb.AppendLine("public partial class WorkflowInputsProxy {");
 031        sb.AppendLine("\tpublic WorkflowInputsProxy(ExecutionContextProxy executionContext) => ExecutionContext = execut
 032        sb.AppendLine("\tpublic ExecutionContextProxy ExecutionContext { get; }");
 033        sb.AppendLine();
 034        sb.AppendLine("\tpublic T? Get<T>(string name) => ExecutionContext.GetInput<T>(name);");
 035        sb.AppendLine();
 36
 037        if (!options.Value.DisableWrappers)
 38        {
 039            foreach (var inputDefinition in inputDefinitions.Where(x => x.Name.IsValidVariableName()))
 40            {
 041                var inputName = inputDefinition.Name;
 042                var variableType = inputDefinition.Type;
 043                var friendlyTypeName = variableType.GetFriendlyTypeName(Brackets.Angle);
 044                sb.AppendLine($"\tpublic {friendlyTypeName} {inputName}");
 045                sb.AppendLine("\t{");
 046                sb.AppendLine($"\t\tget => Get<{friendlyTypeName}>(\"{inputName}\");");
 047                sb.AppendLine("\t}");
 48            }
 49        }
 50
 051        sb.AppendLine("}");
 052        sb.AppendLine("var Inputs = new WorkflowInputsProxy(ExecutionContext);"); // Obsolete; use Input instead.
 053        sb.AppendLine("var Input = Inputs;");
 054        notification.AppendScript(sb.ToString());
 055        return Task.CompletedTask;
 56    }
 57}