< Summary

Information
Class: Elsa.Workflows.WorkflowExecutionResultExtensions
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/WorkflowExecutionResultExtensions.cs
Line coverage
25%
Covered lines: 3
Uncovered lines: 9
Coverable lines: 12
Total lines: 42
Line coverage: 25%
Branch coverage
12%
Covered branches: 1
Total branches: 8
Branch coverage: 12.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetActivityOutput(...)12.5%35825%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/WorkflowExecutionResultExtensions.cs

#LineLine coverage
 1using Elsa.Extensions;
 2using Elsa.Workflows.Models;
 3
 4namespace Elsa.Workflows;
 5
 6public static class WorkflowExecutionResultExtensions
 7{
 8    public static T GetActivityOutput<T>(this RunWorkflowResult result, IActivity activity, string? outputName = null)
 9    {
 1110        var value = result.WorkflowExecutionContext.GetOutputByActivityId(activity.Id, outputName);
 11
 12        // If the value is already of the requested type, return it directly.
 1113        if (value is T tValue)
 1114            return tValue;
 15
 16        // Handle nulls for reference/nullable types.
 017        if (value is null)
 18        {
 19            // If T is a reference type or nullable, default(T) is fine.
 20            // Otherwise, this is a runtime error.
 021            return default(T) is null ? default! : throw new InvalidCastException($"Cannot convert null to non-nullable 
 22        }
 23
 24        // Try to convert using System.Convert when possible (handles numeric casts like Double -> Int32).
 25        try
 26        {
 027            var targetType = typeof(T);
 28
 29            // Unwrap nullable<T> to its underlying type for conversion.
 030            var underlyingType = Nullable.GetUnderlyingType(targetType) ?? targetType;
 31
 032            var converted = Convert.ChangeType(value, underlyingType, System.Globalization.CultureInfo.InvariantCulture)
 33
 34            // If T is nullable and we converted to the underlying type, just cast.
 035            return (T)converted!;
 36        }
 037        catch (Exception ex)
 38        {
 039            throw new InvalidCastException($"Unable to convert value of type '{value.GetType().FullName}' to type '{type
 40        }
 041    }
 42}