< Summary

Information
Class: Elsa.Workflows.Activities.SetVariable
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/SetVariable.cs
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 113
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Variable()100%11100%
get_Value()100%11100%
Execute(...)100%44100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/SetVariable.cs

#LineLine coverage
 1using System.ComponentModel;
 2using System.Runtime.CompilerServices;
 3using Elsa.Expressions.Models;
 4using Elsa.Extensions;
 5using Elsa.Workflows.Attributes;
 6using Elsa.Workflows.Memory;
 7using Elsa.Workflows.Models;
 8using JetBrains.Annotations;
 9
 10namespace Elsa.Workflows.Activities;
 11
 12/// <summary>
 13/// Assign a workflow variable a value.
 14/// </summary>
 15[Browsable(false)]
 16[Activity("Elsa", "Primitives", "Assign value to a workflow variable.")]
 17[PublicAPI]
 18public class SetVariable<T> : CodeActivity
 19{
 20    /// <inheritdoc />
 21    public SetVariable([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 22    {
 23    }
 24
 25    /// <inheritdoc />
 26    public SetVariable(Variable<T> variable, Input<T> value, [CallerFilePath] string? source = null, [CallerLineNumber] 
 27    {
 28        Variable = variable;
 29        Value = value;
 30    }
 31
 32    /// <inheritdoc />
 33    public SetVariable(Variable<T> variable, Variable<T> value, [CallerFilePath] string? source = null, [CallerLineNumbe
 34        : this(variable, new Input<T>(value), source, line)
 35    {
 36    }
 37
 38    /// <inheritdoc />
 39    public SetVariable(Variable<T> variable, Func<ExpressionExecutionContext, T> value, [CallerFilePath] string? source 
 40        : this(variable, new Input<T>(value), source, line)
 41    {
 42    }
 43
 44    /// <inheritdoc />
 45    public SetVariable(Variable<T> variable, Func<T> value, [CallerFilePath] string? source = null, [CallerLineNumber] i
 46        : this(variable, new Input<T>(value), source, line)
 47    {
 48    }
 49
 50    /// <inheritdoc />
 51    public SetVariable(Variable<T> variable, T value, [CallerFilePath] string? source = null, [CallerLineNumber] int? li
 52        : this(variable, new Input<T>(value), source, line)
 53    {
 54    }
 55
 56    /// <summary>
 57    /// The variable to assign the value to.
 58    /// </summary>
 59    [Input(Description = "The variable to assign the value to.")]
 60    public Variable<T> Variable { get; set; } = null!;
 61
 62    /// <summary>
 63    /// The value to assign.
 64    /// </summary>
 65    [Input(Description = "The value to assign.")]
 66    public Input<T> Value { get; set; } = new(new Literal<T>());
 67
 68    /// <inheritdoc />
 69    protected override void Execute(ActivityExecutionContext context)
 70    {
 71        var value = context.Get(Value);
 72        Variable.Set(context, value);
 73    }
 74}
 75
 76/// <summary>
 77/// Assign a workflow variable a value.
 78/// </summary>
 79[Activity("Elsa", "Primitives", "Assign a value to a workflow variable.")]
 80[PublicAPI]
 81public class SetVariable : CodeActivity
 82{
 83    /// <inheritdoc />
 4084    public SetVariable([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 85    {
 4086    }
 87
 88    /// <summary>
 89    /// The variable to assign the value to.
 90    /// </summary>
 91    [Input(Description = "The variable to assign the value to.")]
 7392    public Variable? Variable { get; set; }
 93
 94    /// <summary>
 95    /// The value to assign.
 96    /// </summary>
 97    [Input(Description = "The value to assign.")]
 18698    public Input<object?> Value { get; set; } = new(default(object));
 99
 100    /// <inheritdoc />
 101    protected override void Execute(ActivityExecutionContext context)
 102    {
 103        // Always refer to the variable by ID to ensure that the variable is resolved from the correct scope.
 7104        var variableId = Variable?.Id;
 19105        var variable = context.ExpressionExecutionContext.EnumerateVariablesInScope().FirstOrDefault(x => x.Id == variab
 106
 7107        if (variable == null)
 1108            throw new InvalidOperationException($"Variable '{variableId}' not found.");
 109
 6110        var value = context.Get(Value);
 6111        variable.Set(context, value);
 6112    }
 113}