< Summary

Information
Class: Elsa.Extensions.ActivityExtensions
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/ActivityExtensions.cs
Line coverage
60%
Covered lines: 32
Uncovered lines: 21
Coverable lines: 53
Total lines: 269
Line coverage: 60.3%
Branch coverage
40%
Covered branches: 8
Total branches: 20
Branch coverage: 40%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetNamedInputs(...)100%11100%
GetInputs(...)100%210%
GetInput(...)0%620%
GetOutputs(...)100%11100%
GetOutput(...)50%2280%
GetOutput(...)50%2275%
GetOutput(...)0%620%
GetOutput(...)100%210%
GetOutput(...)100%210%
GetOutput(...)100%210%
GetResult(...)0%620%
GetResult(...)100%210%
GetResult(...)100%210%
GetResult(...)100%210%
GetLastResult(...)0%620%
GetLastResult(...)100%210%
GetLastResult(...)100%210%
GetInputProperties(...)100%11100%
GetDelegate(...)75%8888.88%
GetResumeActivityDelegate(...)100%11100%
GetActivityCompletionCallback(...)100%11100%

File(s)

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

#LineLine coverage
 1using System.Linq.Expressions;
 2using System.Reflection;
 3using Elsa.Expressions.Helpers;
 4using Elsa.Expressions.Models;
 5using Elsa.Workflows;
 6using Elsa.Workflows.Models;
 7using JetBrains.Annotations;
 8
 9// ReSharper disable once CheckNamespace
 10namespace Elsa.Extensions;
 11
 12/// <summary>
 13/// Provides extension methods for <see cref="IActivity"/>.
 14/// </summary>
 15[PublicAPI]
 16public static class ActivityExtensions
 17{
 18    /// <param name="activity">The activity to get the output from.</param>
 19    extension(IActivity activity)
 20    {
 21        /// <summary>
 22        /// Gets the input properties of the specified activity.
 23        /// </summary>
 24        public IDictionary<string, Input> GetNamedInputs()
 25        {
 6126            var inputProps = activity.GetInputProperties().ToList();
 27
 6128            var query =
 6129                from inputProp in inputProps
 48430                let inputValue = (Input?)inputProp.GetValue(activity)
 48431                where inputValue != null
 26332                select (inputProp, inputValue);
 33
 66734            return query.DistinctBy(x => x.inputProp.Name).ToDictionary(x => x.inputProp.Name, x => x.inputValue);
 35        }
 36
 37        /// <summary>
 38        /// Gets the input properties of the specified activity.
 39        /// </summary>
 040        public IEnumerable<Input> GetInputs() => GetNamedInputs(activity).Values;
 41
 42        /// <summary>
 43        /// Gets the input with the specified name.
 44        /// </summary>
 045        public Input? GetInput(string inputName) => GetNamedInputs(activity).TryGetValue(inputName, out var input) ? inp
 46
 47        /// <summary>
 48        /// Gets the output properties of the specified activity.
 49        /// </summary>
 50        public IEnumerable<NamedOutput> GetOutputs()
 51        {
 29018152            var outputProps = activity.GetType().GetProperties().Where(x => typeof(Output).IsAssignableFrom(x.PropertyTy
 53
 1513554            var query =
 1513555                from outputProp in outputProps
 772556                let output = (Output?)outputProp.GetValue(activity)
 772557                where output != null
 1742758                select new NamedOutput(outputProp.Name, output);
 59
 1742760            return query.Select(x => x!).ToList();
 61        }
 62
 63        /// <summary>
 64        /// Gets the output with the specified name.
 65        /// </summary>
 66        /// <param name="context">The activity execution context.</param>
 67        /// <param name="outputName">Name of the output.</param>
 68        /// <returns>The output value.</returns>
 69        public object? GetOutput(ActivityExecutionContext context, string? outputName = null)
 70        {
 181871            var workflowExecutionContext = context.WorkflowExecutionContext;
 181872            var outputRegister = workflowExecutionContext.GetActivityOutputRegister();
 73
 74            // If the provided activity execution context is the same as the current activity's execution context, we re
 181875            if(context.Activity.NodeId == activity.NodeId)
 181876                return outputRegister.FindOutputByActivityInstanceId(context.Id, outputName);
 77
 78            // If the provided activity execution context is different from the current activity's execution context, we
 079            return outputRegister.FindOutputByActivityId(activity.Id, outputName);
 80        }
 81
 82        /// <summary>
 83        /// Gets the output with the specified name.
 84        /// </summary>
 85        /// <param name="context">The expression execution context.</param>
 86        /// <param name="outputName">Name of the output.</param>
 87        /// <returns>The output value.</returns>
 88        public object? GetOutput(ExpressionExecutionContext context, string? outputName = null)
 89        {
 181890            var activityExecutionContext = context.GetActivityExecutionContext();
 91
 181892            if (activityExecutionContext == null)
 093                return null;
 94
 181895            return activity.GetOutput(activityExecutionContext, outputName);
 96        }
 97
 98        /// <summary>
 99        /// Gets the output with the specified name.
 100        /// </summary>
 101        /// <param name="context">The context.</param>
 102        /// <param name="outputName">Name of the output.</param>
 103        /// <typeparam name="T">The type of the output.</typeparam>
 104        /// <returns>The output value.</returns>
 105        public T? GetOutput<T>(ActivityExecutionContext context, string outputName)
 106        {
 0107            var outputValue = activity.GetOutput(context, outputName);
 0108            return outputValue == null ? default! : outputValue.ConvertTo<T>();
 109        }
 110
 111        /// <summary>
 112        /// Gets the output with the specified name.
 113        /// </summary>
 114        /// <param name="context">The context.</param>
 115        /// <param name="outputName">Name of the output.</param>
 116        /// <typeparam name="T">The type of the output.</typeparam>
 117        /// <returns>The output value.</returns>
 118        public T? GetOutput<T>(ExpressionExecutionContext context, string outputName)
 119        {
 0120            return activity.GetOutput<T>(context.GetActivityExecutionContext(), outputName);
 121        }
 122    }
 123
 124    /// <param name="activity">The activity.</param>
 125    /// <typeparam name="TActivity">The type of the activity.</typeparam>
 126    extension<TActivity>(TActivity activity)
 127    {
 128        /// <summary>
 129        /// Gets the output with the specified name.
 130        /// </summary>
 131        /// <param name="context">The context.</param>
 132        /// <param name="outputExpression">The output expression.</param>
 133        /// <typeparam name="T">The type of the output.</typeparam>
 134        /// <returns>The output value.</returns>
 135        public T? GetOutput<T>(ActivityExecutionContext context, Expression<Func<TActivity, object?>> outputExpression)
 136        {
 0137            var outputName = outputExpression.GetPropertyName();
 0138            return ((IActivity)activity!).GetOutput<T>(context, outputName);
 139        }
 140
 141        /// <summary>
 142        /// Gets the output with the specified name.
 143        /// </summary>
 144        /// <param name="context">The context.</param>
 145        /// <param name="outputExpression">The output expression.</param>
 146        /// <typeparam name="T">The type of the output.</typeparam>
 147        /// <returns>The output value.</returns>
 148        public T? GetOutput<T>(ExpressionExecutionContext context, Expression<Func<TActivity, object?>> outputExpression
 149        {
 0150            var outputName = outputExpression.GetPropertyName();
 0151            return ((IActivity)activity!).GetOutput<T>(context, outputName);
 152        }
 153    }
 154
 155    /// <param name="activity">The activity.</param>
 156    extension(IActivity activity)
 157    {
 158        /// <summary>
 159        /// Gets the Result output of the specified activity.
 160        /// </summary>
 161        /// <param name="context">The context.</param>
 162        /// <typeparam name="T">The type as which to return the output.</typeparam>
 163        /// <returns>The output value.</returns>
 164        public T? GetResult<T>(ExpressionExecutionContext context)
 165        {
 0166            var value = GetResult(activity, context);
 0167            return value == null ? default! : value.ConvertTo<T>();
 168        }
 169
 170        /// <summary>
 171        /// Gets the Result output of the specified activity.
 172        /// </summary>
 173        /// <param name="context">The context.</param>
 174        /// <returns>The output value.</returns>
 175        public object? GetResult(ExpressionExecutionContext context)
 176        {
 0177            return activity.GetResult(context.GetActivityExecutionContext());
 178        }
 179
 180        /// <summary>
 181        /// Gets the Result output of the specified activity.
 182        /// </summary>
 183        /// <param name="context">The context.</param>
 184        /// <returns>The output value.</returns>
 185        public object? GetResult(ActivityExecutionContext context)
 186        {
 0187            return activity.GetOutput(context, ActivityOutputRegister.DefaultOutputName);
 188        }
 189    }
 190
 191    /// <summary>
 192    /// Gets the Result output of the specified activity.
 193    /// </summary>
 194    /// <param name="context">The context.</param>
 195    /// <param name="activity">The activity.</param>
 196    /// <returns>The output value.</returns>
 197    public static object? GetResult(this ActivityExecutionContext context, IActivity activity)
 198    {
 0199        return activity.GetOutput(context, ActivityOutputRegister.DefaultOutputName);
 200    }
 201
 202    /// <summary>
 203    /// Gets the result of the last activity.
 204    /// </summary>
 205    /// <param name="context">The context.</param>
 206    public static T? GetLastResult<T>(this ExpressionExecutionContext context)
 207    {
 0208        var value = GetLastResult(context.GetWorkflowExecutionContext());
 0209        return value == null ? default! : value.ConvertTo<T>();
 210    }
 211
 212    /// <summary>
 213    /// Gets the result of the last activity.
 214    /// </summary>
 215    /// <param name="context">The context.</param>
 216    public static object? GetLastResult(this ActivityExecutionContext context)
 217    {
 0218        return context.WorkflowExecutionContext.GetLastResult();
 219    }
 220
 221    /// <summary>
 222    /// Gets the result of the last activity.
 223    /// </summary>
 224    /// <param name="context">The context.</param>
 225    public static object? GetLastResult(this WorkflowExecutionContext context)
 226    {
 0227        return context.GetLastActivityResult();
 228    }
 229
 230    extension(IActivity activity)
 231    {
 232        /// <summary>
 233        /// Gets the input properties of the specified activity.
 234        /// </summary>
 1808235        public IEnumerable<PropertyInfo> GetInputProperties() => activity.GetType().GetProperties().Where(x => typeof(In
 236
 237        /// <summary>
 238        /// Gets the method for the specified method name on the specified activity.
 239        /// </summary>
 240        public TDelegate GetDelegate<TDelegate>(string methodName) where TDelegate : Delegate
 241        {
 112242            var activityType = activity.GetType();
 243            const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | Bindin
 112244            var resumeMethodInfo = default(MethodInfo?);
 112245            var currentType = activityType;
 246
 305247            while (currentType != null && resumeMethodInfo == null)
 248            {
 193249                resumeMethodInfo = currentType.GetMethod(methodName, bindingFlags);
 193250                currentType = currentType.BaseType;
 251            }
 252
 112253            if (resumeMethodInfo == null)
 0254                throw new Exception($"Can't find method name {methodName} on type {activityType} or its base type {activ
 255
 112256            return resumeMethodInfo.IsStatic ? (TDelegate)Delegate.CreateDelegate(typeof(TDelegate), resumeMethodInfo) :
 257        }
 258
 259        /// <summary>
 260        /// Gets the Resume method for the specified activity.
 261        /// </summary>
 20262        public ExecuteActivityDelegate GetResumeActivityDelegate(string resumeMethodName) => activity.GetDelegate<Execut
 263
 264        /// <summary>
 265        /// Gets the Child Activity Completed method for the specified activity.
 266        /// </summary>
 92267        public ActivityCompletionCallback GetActivityCompletionCallback(string completionMethodName) => activity.GetDele
 268    }
 269}

Methods/Properties

GetNamedInputs(Elsa.Workflows.IActivity)
GetInputs(Elsa.Workflows.IActivity)
GetInput(Elsa.Workflows.IActivity,System.String)
GetOutputs(Elsa.Workflows.IActivity)
GetOutput(Elsa.Workflows.IActivity,Elsa.Workflows.ActivityExecutionContext,System.String)
GetOutput(Elsa.Workflows.IActivity,Elsa.Expressions.Models.ExpressionExecutionContext,System.String)
GetOutput(Elsa.Workflows.IActivity,Elsa.Workflows.ActivityExecutionContext,System.String)
GetOutput(Elsa.Workflows.IActivity,Elsa.Expressions.Models.ExpressionExecutionContext,System.String)
GetOutput(TActivity,Elsa.Workflows.ActivityExecutionContext,System.Linq.Expressions.Expression`1<System.Func`2<TActivity,System.Object>>)
GetOutput(TActivity,Elsa.Expressions.Models.ExpressionExecutionContext,System.Linq.Expressions.Expression`1<System.Func`2<TActivity,System.Object>>)
GetResult(Elsa.Workflows.IActivity,Elsa.Expressions.Models.ExpressionExecutionContext)
GetResult(Elsa.Workflows.IActivity,Elsa.Expressions.Models.ExpressionExecutionContext)
GetResult(Elsa.Workflows.IActivity,Elsa.Workflows.ActivityExecutionContext)
GetResult(Elsa.Workflows.ActivityExecutionContext,Elsa.Workflows.IActivity)
GetLastResult(Elsa.Expressions.Models.ExpressionExecutionContext)
GetLastResult(Elsa.Workflows.ActivityExecutionContext)
GetLastResult(Elsa.Workflows.WorkflowExecutionContext)
GetInputProperties(Elsa.Workflows.IActivity)
GetDelegate(Elsa.Workflows.IActivity,System.String)
GetResumeActivityDelegate(Elsa.Workflows.IActivity,System.String)
GetActivityCompletionCallback(Elsa.Workflows.IActivity,System.String)