Add variable types to Type Definition output
This commit is contained in:
parent
64ceb843d0
commit
11b684274c
|
|
@ -8,7 +8,24 @@ namespace Elsa.JavaScript.Builders;
|
|||
public class TypeDefinitionBuilder
|
||||
{
|
||||
private readonly TypeDefinition _typeDefinition = new();
|
||||
|
||||
/// <summary>
|
||||
/// Set the name of the function.
|
||||
/// </summary>
|
||||
public TypeDefinitionBuilder Name(string name)
|
||||
{
|
||||
_typeDefinition.Name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the return type of the function.
|
||||
/// </summary>
|
||||
public TypeDefinitionBuilder DeclarationKeyword(string keyword)
|
||||
{
|
||||
_typeDefinition.DeclarationKeyword = keyword;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build a <see cref="TypeDefinition"/> using the collected information.
|
||||
|
|
|
|||
|
|
@ -43,8 +43,10 @@ public class JavaScriptFeature : FeatureBase
|
|||
.AddSingleton<ITypeDescriber, TypeDescriber>()
|
||||
.AddSingleton<ITypeDefinitionDocumentRenderer, TypeDefinitionDocumentRenderer>()
|
||||
.AddSingleton<ITypeAliasRegistry, TypeAliasRegistry>()
|
||||
.AddSingleton<IFunctionDefinitionProvider, CommonFunctionDefinitionProvider>()
|
||||
.AddSingleton<ITypeDefinitionProvider, VariableTypeDefinitionProvider>();
|
||||
.AddSingleton<IFunctionDefinitionProvider, CommonFunctionsDefinitionProvider>()
|
||||
.AddSingleton<ITypeDefinitionProvider, CommonTypeDefinitionProvider>()
|
||||
.AddSingleton<ITypeDefinitionProvider, VariableTypeDefinitionProvider>()
|
||||
;
|
||||
|
||||
Module.UseWorkflowManagement(management => management.AddActivitiesFrom<JavaScriptFeature>());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
using System.Dynamic;
|
||||
using Elsa.JavaScript.Extensions;
|
||||
using Elsa.JavaScript.Services;
|
||||
using Elsa.Workflows.Management.Options;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Elsa.JavaScript.Implementations;
|
||||
|
||||
|
|
@ -11,9 +14,10 @@ public class TypeAliasRegistry : ITypeAliasRegistry
|
|||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
public TypeAliasRegistry()
|
||||
public TypeAliasRegistry(IOptions<ManagementOptions> managementOptions)
|
||||
{
|
||||
this.RegisterType<object>("any");
|
||||
this.RegisterType<ExpandoObject>("any");
|
||||
this.RegisterType<string>("string");
|
||||
this.RegisterType<bool>("boolean");
|
||||
this.RegisterType<short>("number");
|
||||
|
|
@ -26,6 +30,12 @@ public class TypeAliasRegistry : ITypeAliasRegistry
|
|||
this.RegisterType<DateTimeOffset>("Date");
|
||||
this.RegisterType<DateOnly>("Date");
|
||||
this.RegisterType<TimeOnly>("Date");
|
||||
|
||||
foreach (var variableDescriptor in managementOptions.Value.VariableDescriptors)
|
||||
{
|
||||
if(!_typeAliasDictionary.ContainsKey(variableDescriptor.Type))
|
||||
RegisterType(variableDescriptor.Type, variableDescriptor.Type.Name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ public class TypeDefinitionDocumentRenderer : ITypeDefinitionDocumentRenderer
|
|||
string RenderParameters(IEnumerable<ParameterDefinition> parameters) => string.Join(", ", parameters.Select(RenderParameter));
|
||||
|
||||
var returnType = functionDefinition.ReturnType != null ? $": {functionDefinition.ReturnType}" : "";
|
||||
output.AppendLine($"declare function {functionDefinition.Name}({RenderParameters(functionDefinition.Parameters)}){returnType}");
|
||||
output.AppendLine($"declare function {functionDefinition.Name}({RenderParameters(functionDefinition.Parameters)}){returnType};");
|
||||
}
|
||||
|
||||
private void Render(TypeDefinition typeDefinition, StringBuilder output)
|
||||
|
|
@ -40,5 +40,5 @@ public class TypeDefinitionDocumentRenderer : ITypeDefinitionDocumentRenderer
|
|||
output.AppendLine("}");
|
||||
}
|
||||
|
||||
private void Render(PropertyDefinition property, StringBuilder output) => output.AppendLine($"{property.Name}{(property.IsOptional ? "?" : "")}: {property.Type}");
|
||||
private void Render(PropertyDefinition property, StringBuilder output) => output.AppendLine($"{property.Name}{(property.IsOptional ? "?" : "")}: {property.Type};");
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Elsa.JavaScript.Abstractions;
|
||||
using Elsa.JavaScript.Models;
|
||||
using Elsa.JavaScript.Services;
|
||||
|
||||
namespace Elsa.JavaScript.Providers;
|
||||
|
||||
/// <summary>
|
||||
/// Produces <see cref="FunctionDefinition"/>s for common functions.
|
||||
/// </summary>
|
||||
internal class CommonTypeDefinitionProvider : TypeDefinitionProvider
|
||||
{
|
||||
private readonly ITypeDescriber _typeDescriber;
|
||||
|
||||
public CommonTypeDefinitionProvider(ITypeDescriber typeDescriber)
|
||||
{
|
||||
_typeDescriber = typeDescriber;
|
||||
}
|
||||
|
||||
protected override IEnumerable<TypeDefinition> GetTypeDefinitions(TypeDefinitionContext context)
|
||||
{
|
||||
yield return _typeDescriber.DescribeType(typeof(Guid));
|
||||
yield return _typeDescriber.DescribeType(typeof(JsonObject));
|
||||
}
|
||||
}
|
||||
|
|
@ -10,5 +10,5 @@ public interface IActivityPropertyOptionsProvider
|
|||
/// <summary>
|
||||
/// Returns options for the specified property.
|
||||
/// </summary>
|
||||
object GetOptions(PropertyInfo property);
|
||||
object? GetOptions(PropertyInfo property);
|
||||
}
|
||||
Loading…
Reference in a new issue