< Summary

Information
Class: Elsa.Workflows.Management.Providers.DefaultExpressionDescriptorProvider
Assembly: Elsa.Workflows.Management
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Providers/DefaultExpressionDescriptorProvider.cs
Line coverage
78%
Covered lines: 67
Uncovered lines: 18
Coverable lines: 85
Total lines: 136
Line coverage: 78.8%
Branch coverage
50%
Covered branches: 12
Total branches: 24
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetDescriptors()100%11100%
CreateLiteralDescriptor()100%22100%
CreateObjectDescriptor()100%11100%
CreateJsonDescriptor()100%11100%
CreateDelegateDescriptor()100%11100%
CreateVariableDescriptor()0%9658.33%
CreateInputDescriptor()0%8665.21%
CreateDescriptor(...)100%1010100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Providers/DefaultExpressionDescriptorProvider.cs

#LineLine coverage
 1using System.Diagnostics;
 2using System.Text.Json;
 3using Elsa.Expressions;
 4using Elsa.Expressions.Contracts;
 5using Elsa.Expressions.Models;
 6using Elsa.Extensions;
 7using Elsa.Workflows.Expressions;
 8using Elsa.Workflows.Memory;
 9using Elsa.Workflows.Models;
 10using Microsoft.Extensions.DependencyInjection;
 11
 12namespace Elsa.Workflows.Management.Providers;
 13
 14/// <inheritdoc />
 15public class DefaultExpressionDescriptorProvider : IExpressionDescriptorProvider
 16{
 17    /// <inheritdoc />
 18    public IEnumerable<ExpressionDescriptor> GetDescriptors()
 19    {
 31620        yield return CreateLiteralDescriptor();
 31621        yield return CreateObjectDescriptor();
 31622        yield return CreateJsonDescriptor();
 31623        yield return CreateDelegateDescriptor();
 31624        yield return CreateVariableDescriptor();
 31625        yield return CreateInputDescriptor();
 31626    }
 27
 28    private ExpressionDescriptor CreateLiteralDescriptor()
 29    {
 31630        return CreateDescriptor<LiteralExpressionHandler>(
 31631            "Literal",
 31632            "Literal",
 31633            isBrowsable: false,
 168634            memoryBlockReferenceFactory: () => new Literal(),
 31635            deserialize: context =>
 31636            {
 168637                var elementValue = context.JsonElement.TryGetProperty("value", out var v) ? v : default;
 168638                var value = elementValue.GetValue();
 168639                return new Expression("Literal", value);
 31640            });
 41    }
 42
 31643    private ExpressionDescriptor CreateObjectDescriptor() => CreateDescriptor<ObjectExpressionHandler>("Object", "Object
 44
 45    [Obsolete("Use Object instead.")]
 31646    private ExpressionDescriptor CreateJsonDescriptor() => CreateDescriptor<ObjectExpressionHandler>("Json", "Json", mon
 47
 31648    private ExpressionDescriptor CreateDelegateDescriptor() => CreateDescriptor<DelegateExpressionHandler>("Delegate", "
 49
 50    private ExpressionDescriptor CreateVariableDescriptor()
 51    {
 31652        return CreateDescriptor<VariableExpressionHandler>(
 31653            "Variable",
 31654            "Variable",
 31655            isBrowsable: true,
 056            memoryBlockReferenceFactory: () => new Variable(),
 31657            deserialize: context =>
 31658            {
 059                var valueElement = context.JsonElement.TryGetProperty("value", out var v) ? v : default;
 060                var valueString = valueElement.GetValue()?.ToString();
 31661
 062                if (string.IsNullOrWhiteSpace(valueString))
 063                    return new Expression("Variable", null);
 31664
 31665                try
 31666                {
 067                    var value = JsonSerializer.Deserialize(valueString, context.MemoryBlockType, context.Options);
 068                    return new Expression("Variable", value);
 31669                }
 070                catch (Exception)
 31671                {
 072                    return new Expression("Variable", null);
 31673                }
 074            }
 31675        );
 76    }
 77
 78    private ExpressionDescriptor CreateInputDescriptor()
 79    {
 31680        return CreateDescriptor<InputExpressionHandler>(
 31681            "Input",
 31682            "Input", // Displayed text in UI (input selection dropdown)
 31683            isBrowsable: true,
 31684            deserialize: context =>
 31685            {
 086                var valueElement = context.JsonElement.TryGetProperty("value", out var v) ? v : default;
 087                var valueString = valueElement.GetValue()?.ToString();
 31688
 089                if (string.IsNullOrWhiteSpace(valueString)) return new Expression("Input", null);
 31690
 31691                try
 31692                {
 093                    var value = JsonSerializer.Deserialize(valueString, typeof(InputDefinition), context.Options);
 31694
 095                    return new Expression("Input", value);
 31696                }
 097                catch (Exception)
 31698                {
 099                    return new Expression("Input", null);
 316100                }
 0101            }
 316102        );
 103    }
 104
 105    private static ExpressionDescriptor CreateDescriptor<THandler>(
 106        string expressionType,
 107        string displayName,
 108        bool isSerializable = true,
 109        bool isBrowsable = true,
 110        string? monacoLanguage = null,
 111        Func<MemoryBlockReference>? memoryBlockReferenceFactory = default,
 112        Func<ExpressionSerializationContext, Expression>? deserialize = default)
 113        where THandler : IExpressionHandler
 114    {
 1896115        var descriptor = new ExpressionDescriptor
 1896116        {
 1896117            Type = expressionType,
 1896118            DisplayName = displayName,
 1896119            IsSerializable = isSerializable,
 1896120            IsBrowsable = isBrowsable,
 2269121            HandlerFactory = sp => ActivatorUtilities.GetServiceOrCreateInstance<THandler>(sp),
 263122            MemoryBlockReferenceFactory = memoryBlockReferenceFactory ?? (() => new MemoryBlockReference())
 1896123        };
 124
 1896125        if (deserialize != null)
 948126            descriptor.Deserialize = deserialize;
 127
 1896128        if (monacoLanguage != null)
 632129            descriptor.Properties = new
 632130            {
 632131                MonacoLanguage = monacoLanguage
 632132            }.ToDictionary();
 133
 1896134        return descriptor;
 135    }
 136}