< Summary

Information
Class: Elsa.AI.Host.Services.ActivityGroundingMapper
Assembly: Elsa.AI.Host
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.AI.Host/Services/ActivityGroundingMapper.cs
Line coverage
84%
Covered lines: 49
Uncovered lines: 9
Coverable lines: 58
Total lines: 86
Line coverage: 84.4%
Branch coverage
54%
Covered branches: 13
Total branches: 24
Branch coverage: 54.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Map(...)50%2288.88%
MapInput(...)50%44100%
MapOutput(...)50%22100%
GetConstraints()62.5%10866.66%
IsNullable(...)50%22100%
GetFriendlyTypeName(...)50%9655.55%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.AI.Host/Services/ActivityGroundingMapper.cs

#LineLine coverage
 1using Elsa.AI.Abstractions.Models;
 2using Elsa.Workflows.Models;
 3
 4namespace Elsa.AI.Host.Services;
 5
 6public class ActivityGroundingMapper
 7{
 8    public ActivityGroundingSummary Map(ActivityDescriptor descriptor) =>
 29        new()
 210        {
 211            Type = descriptor.TypeName,
 212            Version = descriptor.Version,
 213            Namespace = descriptor.Namespace,
 214            Name = descriptor.Name,
 215            DisplayName = descriptor.DisplayName ?? descriptor.Name,
 216            Description = descriptor.Description,
 217            Category = descriptor.Category,
 218            IsBrowsable = descriptor.IsBrowsable,
 219            IsTrigger = descriptor.IsStart,
 220            IsContainer = descriptor.IsContainer,
 221            IsTerminal = descriptor.IsTerminal,
 022            Inputs = descriptor.Inputs.Select(MapInput).OrderBy(x => x.Name).ToList(),
 023            Outputs = descriptor.Outputs.Select(MapOutput).OrderBy(x => x.Name).ToList(),
 224            Ports = descriptor.Ports.Select(x => x.Name).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringCompar
 225            Constraints = GetConstraints(descriptor).ToList()
 226        };
 27
 28    private static ActivityPortSummary MapInput(InputDescriptor input) =>
 129        new()
 130        {
 131            Name = input.Name,
 132            DisplayName = input.DisplayName ?? input.Name,
 133            Description = input.Description,
 134            Type = GetFriendlyTypeName(input.Type),
 135            Category = input.Category,
 136            IsBrowsable = input.IsBrowsable ?? true,
 137            IsSensitive = input.IsSensitive,
 138            IsRequired = input.DefaultValue == null && !IsNullable(input.Type),
 139            UIHint = input.UIHint,
 140            DefaultSyntax = input.DefaultSyntax
 141        };
 42
 43    private static ActivityPortSummary MapOutput(OutputDescriptor output) =>
 144        new()
 145        {
 146            Name = output.Name,
 147            DisplayName = output.DisplayName ?? output.Name,
 148            Description = output.Description,
 149            Type = GetFriendlyTypeName(output.Type),
 150            IsBrowsable = output.IsBrowsable ?? true
 151        };
 52
 53    private static IEnumerable<string> GetConstraints(ActivityDescriptor descriptor)
 54    {
 255        if (!descriptor.IsBrowsable)
 056            yield return "Not selectable from activity pickers.";
 57
 258        if (descriptor.RunAsynchronously)
 059            yield return "Runs asynchronously by default.";
 60
 261        if (descriptor.IsStart)
 262            yield return "Can start a workflow.";
 63
 264        if (descriptor.IsTerminal)
 065            yield return "Can terminate a workflow path.";
 266    }
 67
 68    private static bool IsNullable(Type type) =>
 169        !type.IsValueType || Nullable.GetUnderlyingType(type) != null;
 70
 71    private static string GetFriendlyTypeName(Type? type)
 72    {
 273        if (type == null)
 074            return "unknown";
 75
 276        var nullableType = Nullable.GetUnderlyingType(type);
 277        if (nullableType != null)
 078            return $"{GetFriendlyTypeName(nullableType)}?";
 79
 280        if (!type.IsGenericType)
 281            return type.Name;
 82
 083        var name = type.Name[..type.Name.IndexOf('`', StringComparison.Ordinal)];
 084        return $"{name}<{string.Join(", ", type.GetGenericArguments().Select(GetFriendlyTypeName))}>";
 85    }
 86}