< Summary

Information
Class: Elsa.Expressions.CSharp.Handlers.GenerateArgumentAccessors
Assembly: Elsa.Expressions.CSharp
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.CSharp/Handlers/GenerateArgumentAccessors.cs
Line coverage
4%
Covered lines: 1
Uncovered lines: 21
Coverable lines: 22
Total lines: 50
Line coverage: 4.5%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.CSharp/Handlers/GenerateArgumentAccessors.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 GenerateArgumentAccessors(IOptions<CSharpOptions> options) : INotificationHandler<EvaluatingCSharp>
 18{
 19    /// <inheritdoc />
 20    public Task HandleAsync(EvaluatingCSharp notification, CancellationToken cancellationToken)
 21    {
 022        var arguments = notification.Options.Arguments;
 023        var sb = new StringBuilder();
 024        sb.AppendLine("public partial class ArgumentsProxy {");
 025        sb.AppendLine("\tpublic ArgumentsProxy(IDictionary<string, object> arguments) => Arguments = arguments;");
 026        sb.AppendLine("\tpublic IDictionary<string, object> Arguments { get; }");
 027        sb.AppendLine();
 028        sb.AppendLine("\tpublic T? Get<T>(string name) => Arguments.TryGetValue(name, out var v) ? (T?)v : default;");
 029        sb.AppendLine();
 30
 031        if (!options.Value.DisableWrappers)
 32        {
 033            foreach (var argument in arguments.Where(x => x.Key.IsValidVariableName()))
 34            {
 035                var argumentName = argument.Key;
 036                var variableType = argument.Value.GetType();
 037                var friendlyTypeName = variableType.GetFriendlyTypeName(Brackets.Angle);
 038                sb.AppendLine($"\tpublic {friendlyTypeName} {argumentName}");
 039                sb.AppendLine("\t{");
 040                sb.AppendLine($"\t\tget => Get<{friendlyTypeName}>(\"{argumentName}\");");
 041                sb.AppendLine("\t}");
 42            }
 43        }
 44
 045        sb.AppendLine("}");
 046        sb.AppendLine("var Args = new ArgumentsProxy(Arguments);");
 047        notification.AppendScript(sb.ToString());
 048        return Task.CompletedTask;
 49    }
 50}