| | | 1 | | using Elsa.Expressions.Contracts; |
| | | 2 | | using Elsa.Expressions.Options; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Expressions.Services; |
| | | 6 | | |
| | | 7 | | /// <inheritdoc /> |
| | | 8 | | public class WellKnownTypeRegistry : IWellKnownTypeRegistry |
| | | 9 | | { |
| | 4398 | 10 | | private readonly Dictionary<string, Type> _aliasTypeDictionary = new(StringComparer.OrdinalIgnoreCase); |
| | 4398 | 11 | | 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 | | { |
| | 4395 | 19 | | var registry = new WellKnownTypeRegistry(); |
| | 4395 | 20 | | return registry; |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="WellKnownTypeRegistry"/> class. |
| | | 25 | | /// </summary> |
| | 3 | 26 | | public WellKnownTypeRegistry(IOptions<ExpressionOptions> expressionOptions) |
| | | 27 | | { |
| | 176 | 28 | | foreach (var entry in expressionOptions.Value.AliasTypeDictionary) |
| | 85 | 29 | | RegisterType(entry.Value, entry.Key); |
| | 3 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="WellKnownTypeRegistry"/> class. |
| | | 34 | | /// </summary> |
| | 4395 | 35 | | public WellKnownTypeRegistry() |
| | | 36 | | { |
| | 4395 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | | 40 | | public void RegisterType(Type type, string alias) |
| | | 41 | | { |
| | 85 | 42 | | _typeAliasDictionary[type] = alias; |
| | 85 | 43 | | _aliasTypeDictionary[alias] = type; |
| | | 44 | | |
| | 85 | 45 | | if (type.IsPrimitive || type.IsValueType && Nullable.GetUnderlyingType(type) == null) |
| | | 46 | | { |
| | 39 | 47 | | var nullableType = typeof(Nullable<>).MakeGenericType(type); |
| | 39 | 48 | | var nullableAlias = alias + "?"; |
| | 39 | 49 | | _typeAliasDictionary[nullableType] = nullableAlias; |
| | 39 | 50 | | _aliasTypeDictionary[nullableAlias] = nullableType; |
| | | 51 | | } |
| | 85 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | 9857 | 55 | | public bool TryGetAlias(Type type, out string alias) => _typeAliasDictionary.TryGetValue(type, out alias!); |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 1980 | 58 | | public bool TryGetType(string alias, out Type type) => _aliasTypeDictionary.TryGetValue(alias, out type!); |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | 0 | 61 | | public IEnumerable<Type> ListTypes() => _typeAliasDictionary.Keys; |
| | | 62 | | } |