| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Expressions.JavaScript.Contracts; |
| | | 6 | | using Elsa.Expressions.JavaScript.TypeDefinitions.Contracts; |
| | | 7 | | using Elsa.Expressions.JavaScript.TypeDefinitions.Models; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Expressions.JavaScript.TypeDefinitions.Services; |
| | | 10 | | |
| | | 11 | | /// <inheritdoc /> |
| | | 12 | | public class TypeDescriber : ITypeDescriber |
| | | 13 | | { |
| | | 14 | | private readonly ITypeAliasRegistry _typeAliasRegistry; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Constructor. |
| | | 18 | | /// </summary> |
| | 323 | 19 | | public TypeDescriber(ITypeAliasRegistry typeAliasRegistry) |
| | | 20 | | { |
| | 323 | 21 | | _typeAliasRegistry = typeAliasRegistry; |
| | 323 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public TypeDefinition DescribeType(Type type) |
| | | 26 | | { |
| | 0 | 27 | | var typeDefinition = new TypeDefinition |
| | 0 | 28 | | { |
| | 0 | 29 | | DeclarationKeyword = GetDeclarationKeyword(type), |
| | 0 | 30 | | Name = type.Name, |
| | 0 | 31 | | Properties = GetPropertyDefinitions(type).DistinctBy(x => x.Name).ToList(), |
| | 0 | 32 | | Methods = GetMethodDefinitions(type).DistinctBy(x => x.Name).ToList() |
| | 0 | 33 | | }; |
| | | 34 | | |
| | 0 | 35 | | return typeDefinition; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | private IEnumerable<FunctionDefinition> GetMethodDefinitions(Type type) |
| | | 39 | | { |
| | 0 | 40 | | if(type.IsEnum) |
| | 0 | 41 | | yield break; |
| | | 42 | | |
| | | 43 | | #pragma warning disable IL2070 |
| | 0 | 44 | | var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) |
| | 0 | 45 | | .Where(x => !x.IsSpecialName) |
| | 0 | 46 | | .Where(x => x.GetCustomAttribute<CompilerGeneratedAttribute>() == null) |
| | 0 | 47 | | .ToList(); |
| | | 48 | | #pragma warning restore IL2070 |
| | | 49 | | |
| | 0 | 50 | | foreach (var method in methods) |
| | | 51 | | { |
| | 0 | 52 | | yield return new FunctionDefinition |
| | 0 | 53 | | { |
| | 0 | 54 | | Name = method.Name, |
| | 0 | 55 | | Parameters = GetMethodParameters(method).ToList(), |
| | 0 | 56 | | ReturnType = _typeAliasRegistry.TryGetAlias(method.ReturnType, out var alias) ? alias : "any" |
| | 0 | 57 | | }; |
| | | 58 | | } |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | private IEnumerable<ParameterDefinition> GetMethodParameters(MethodInfo method) |
| | | 62 | | { |
| | 0 | 63 | | var parameters = method.GetParameters(); |
| | | 64 | | |
| | 0 | 65 | | foreach (var parameter in parameters) |
| | | 66 | | { |
| | 0 | 67 | | yield return new ParameterDefinition |
| | 0 | 68 | | { |
| | 0 | 69 | | Name = parameter.Name!, |
| | 0 | 70 | | Type = _typeAliasRegistry.TryGetAlias(parameter.ParameterType, out var alias) ? alias : "any", |
| | 0 | 71 | | IsOptional = parameter.IsOptional |
| | 0 | 72 | | }; |
| | | 73 | | } |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | private IEnumerable<PropertyDefinition> GetPropertyDefinitions([DynamicallyAccessedMembers(DynamicallyAccessedMember |
| | | 77 | | { |
| | | 78 | | // If the type is an enum, enumerate its members. |
| | 0 | 79 | | if (type.IsEnum) |
| | | 80 | | { |
| | 0 | 81 | | foreach (var name in Enum.GetNames(type)) |
| | | 82 | | { |
| | 0 | 83 | | yield return new PropertyDefinition |
| | 0 | 84 | | { |
| | 0 | 85 | | Name = name, |
| | 0 | 86 | | Type = "string", |
| | 0 | 87 | | IsOptional = false, |
| | 0 | 88 | | }; |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | yield break; |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | var properties = type.GetProperties(); |
| | | 95 | | |
| | 0 | 96 | | foreach (var property in properties) |
| | | 97 | | { |
| | 0 | 98 | | yield return new PropertyDefinition |
| | 0 | 99 | | { |
| | 0 | 100 | | Name = property.Name, |
| | 0 | 101 | | Type = _typeAliasRegistry.TryGetAlias(property.PropertyType, out var alias) ? alias : "any", |
| | 0 | 102 | | IsOptional = property.PropertyType.IsNullableType() |
| | 0 | 103 | | }; |
| | | 104 | | } |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | private static string GetDeclarationKeyword(Type type) => |
| | 0 | 108 | | type switch |
| | 0 | 109 | | { |
| | 0 | 110 | | { IsInterface: true } => "interface", |
| | 0 | 111 | | { IsClass: true } => "class", |
| | 0 | 112 | | { IsEnum: true } => "enum", |
| | 0 | 113 | | { IsValueType: true, IsEnum: false } => "class", |
| | 0 | 114 | | _ => "interface" |
| | 0 | 115 | | }; |
| | | 116 | | } |