< Summary

Information
Class: Elsa.Expressions.Services.WellKnownTypeRegistry
Assembly: Elsa.Expressions
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions/Services/WellKnownTypeRegistry.cs
Line coverage
95%
Covered lines: 20
Uncovered lines: 1
Coverable lines: 21
Total lines: 62
Line coverage: 95.2%
Branch coverage
100%
Covered branches: 8
Total branches: 8
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%22100%
CreateDefault()100%11100%
.ctor()100%11100%
RegisterType(...)100%66100%
TryGetAlias(...)100%11100%
TryGetType(...)100%11100%
ListTypes()100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions/Services/WellKnownTypeRegistry.cs

#LineLine coverage
 1using Elsa.Expressions.Contracts;
 2using Elsa.Expressions.Options;
 3using Microsoft.Extensions.Options;
 4
 5namespace Elsa.Expressions.Services;
 6
 7/// <inheritdoc />
 8public class WellKnownTypeRegistry : IWellKnownTypeRegistry
 9{
 439810    private readonly Dictionary<string, Type> _aliasTypeDictionary = new(StringComparer.OrdinalIgnoreCase);
 439811    private readonly Dictionary<Type, string> _typeAliasDictionary = new();
 12
 13    /// <summary>
 14    /// Creates a new instance of the <see cref="WellKnownTypeRegistry"/> class.
 15    /// </summary>
 16    /// <returns>The new instance.</returns>
 17    public static IWellKnownTypeRegistry CreateDefault()
 18    {
 439519        var registry = new WellKnownTypeRegistry();
 439520        return registry;
 21    }
 22
 23    /// <summary>
 24    /// Initializes a new instance of the <see cref="WellKnownTypeRegistry"/> class.
 25    /// </summary>
 326    public WellKnownTypeRegistry(IOptions<ExpressionOptions> expressionOptions)
 27    {
 17628        foreach (var entry in expressionOptions.Value.AliasTypeDictionary)
 8529            RegisterType(entry.Value, entry.Key);
 330    }
 31
 32    /// <summary>
 33    /// Initializes a new instance of the <see cref="WellKnownTypeRegistry"/> class.
 34    /// </summary>
 439535    public WellKnownTypeRegistry()
 36    {
 439537    }
 38
 39    /// <inheritdoc />
 40    public void RegisterType(Type type, string alias)
 41    {
 8542        _typeAliasDictionary[type] = alias;
 8543        _aliasTypeDictionary[alias] = type;
 44
 8545        if (type.IsPrimitive || type.IsValueType && Nullable.GetUnderlyingType(type) == null)
 46        {
 3947            var nullableType = typeof(Nullable<>).MakeGenericType(type);
 3948            var nullableAlias = alias + "?";
 3949            _typeAliasDictionary[nullableType] = nullableAlias;
 3950            _aliasTypeDictionary[nullableAlias] = nullableType;
 51        }
 8552    }
 53
 54    /// <inheritdoc />
 985755    public bool TryGetAlias(Type type, out string alias) => _typeAliasDictionary.TryGetValue(type, out alias!);
 56
 57    /// <inheritdoc />
 198058    public bool TryGetType(string alias, out Type type) => _aliasTypeDictionary.TryGetValue(alias, out type!);
 59
 60    /// <inheritdoc />
 061    public IEnumerable<Type> ListTypes() => _typeAliasDictionary.Keys;
 62}