< Summary

Information
Class: Elsa.Workflows.Activities.SetVariable<T>
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/SetVariable.cs
Line coverage
57%
Covered lines: 11
Uncovered lines: 8
Coverable lines: 19
Total lines: 113
Line coverage: 57.8%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

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 />
 621    public SetVariable([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 22    {
 623    }
 24
 25    /// <inheritdoc />
 626    public SetVariable(Variable<T> variable, Input<T> value, [CallerFilePath] string? source = null, [CallerLineNumber] 
 27    {
 628        Variable = variable;
 629        Value = value;
 630    }
 31
 32    /// <inheritdoc />
 33    public SetVariable(Variable<T> variable, Variable<T> value, [CallerFilePath] string? source = null, [CallerLineNumbe
 034        : this(variable, new Input<T>(value), source, line)
 35    {
 036    }
 37
 38    /// <inheritdoc />
 39    public SetVariable(Variable<T> variable, Func<ExpressionExecutionContext, T> value, [CallerFilePath] string? source 
 040        : this(variable, new Input<T>(value), source, line)
 41    {
 042    }
 43
 44    /// <inheritdoc />
 45    public SetVariable(Variable<T> variable, Func<T> value, [CallerFilePath] string? source = null, [CallerLineNumber] i
 046        : this(variable, new Input<T>(value), source, line)
 47    {
 048    }
 49
 50    /// <inheritdoc />
 51    public SetVariable(Variable<T> variable, T value, [CallerFilePath] string? source = null, [CallerLineNumber] int? li
 052        : this(variable, new Input<T>(value), source, line)
 53    {
 054    }
 55
 56    /// <summary>
 57    /// The variable to assign the value to.
 58    /// </summary>
 59    [Input(Description = "The variable to assign the value to.")]
 1560    public Variable<T> Variable { get; set; } = null!;
 61
 62    /// <summary>
 63    /// The value to assign.
 64    /// </summary>
 65    [Input(Description = "The value to assign.")]
 2166    public Input<T> Value { get; set; } = new(new Literal<T>());
 67
 68    /// <inheritdoc />
 69    protected override void Execute(ActivityExecutionContext context)
 70    {
 371        var value = context.Get(Value);
 372        Variable.Set(context, value);
 273    }
 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 />
 84    public SetVariable([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 85    {
 86    }
 87
 88    /// <summary>
 89    /// The variable to assign the value to.
 90    /// </summary>
 91    [Input(Description = "The variable to assign the value to.")]
 92    public Variable? Variable { get; set; }
 93
 94    /// <summary>
 95    /// The value to assign.
 96    /// </summary>
 97    [Input(Description = "The value to assign.")]
 98    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.
 104        var variableId = Variable?.Id;
 105        var variable = context.ExpressionExecutionContext.EnumerateVariablesInScope().FirstOrDefault(x => x.Id == variab
 106
 107        if (variable == null)
 108            throw new InvalidOperationException($"Variable '{variableId}' not found.");
 109
 110        var value = context.Get(Value);
 111        variable.Set(context, value);
 112    }
 113}