< Summary

Information
Class: Elsa.Workflows.UIHints.Dictionary.DictionaryValueEvaluator
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/UIHints/Dictionary/DictionaryValueEvaluator.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 26
Coverable lines: 26
Total lines: 59
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
EvaluateAsync()0%210140%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/UIHints/Dictionary/DictionaryValueEvaluator.cs

#LineLine coverage
 1using System.Text.Json;
 2using Elsa.Expressions.Models;
 3using Elsa.Extensions;
 4using Microsoft.Extensions.Logging;
 5
 6namespace Elsa.Workflows.UIHints.Dictionary;
 7
 8/// <summary>
 9/// A class that evaluates activity inputs configured to be dictionaries. It resolves expressions and modifies the input
 10/// </summary>
 011public class DictionaryValueEvaluator(ILogger<DictionaryValueEvaluator> logger) : IActivityInputEvaluator
 12{
 13    public async Task<object?> EvaluateAsync(ActivityInputEvaluatorContext context)
 14    {
 015        var wrappedInput = context.Input;
 016        var evaluator = context.ExpressionEvaluator;
 017        var expressionExecutionContext = context.ExpressionExecutionContext;
 018        var inputDescriptor = context.InputDescriptor;
 019        var defaultValue = inputDescriptor.DefaultValue;
 020        var value = wrappedInput.Expression != null ? await evaluator.EvaluateAsync(wrappedInput, expressionExecutionCon
 21
 022        if (value is not IDictionary<string, object> dictionary || inputDescriptor.UIHint != InputUIHints.Dictionary)
 023            return value;
 24
 025        var tempDictionary = new Dictionary<string, object?>(dictionary.Count);
 026        foreach (var dict in dictionary)
 27        {
 028            if (dict.Value is not JsonElement json)
 29            {
 30                // Not a JSON object, so just use the value as-is.
 031                tempDictionary[dict.Key] = dict.Value;
 032                continue;
 33            }
 34
 35            // JSON object, so extract the type and value properties.
 036            var hasType = json.TryGetProperty("type", out var typeProperty);
 037            var hasValue = json.TryGetProperty("value", out var valueProperty);
 38
 039            if (!hasType || !hasValue)
 40            {
 41                // Skip this entry or handle as needed (e.g., log, throw, etc.)
 042                logger.LogWarning("Dictionary entry is missing type or value property: {Json}", JsonSerializer.Serialize
 043                continue;
 44            }
 45
 46            // Evaluate the expression.
 047            var expression = new Expression(typeProperty.ToString(), valueProperty.ToString());
 048            var val = await evaluator.EvaluateAsync<object>(expression, expressionExecutionContext);
 49
 50            // Add the evaluated value to the dictionary.
 051            tempDictionary[dict.Key] = val;
 052        }
 53
 54        // Replace the original dictionary with the evaluated one.
 055        value = tempDictionary;
 56
 057        return value;
 058    }
 59}