Improve JSON expression type handling

This commit is contained in:
Sipke Schoorstra 2023-01-20 19:02:29 +01:00
parent a69812d03d
commit 2eda8dfd3b
6 changed files with 44 additions and 6 deletions

View file

@ -34,7 +34,7 @@ export class CheckList {
const displayName = inputDescriptor.displayName;
const hint = inputDescriptor.description;
const input = getInputPropertyValue(inputContext);
const value = (input?.expression as LiteralExpression)?.value; // TODO: The "value" field is currently hardcoded, but we should be able to be more flexible and potentially have different fields for a given syntax.
const value = (input?.expression as JsonExpression)?.value; // TODO: The "value" field is currently hardcoded, but we should be able to be more flexible and potentially have different fields for a given syntax.
const syntax = input?.expression?.type ?? inputDescriptor.defaultSyntax;
const selectList = this.selectList;

View file

@ -0,0 +1,31 @@
using System.Text.Json;
namespace Elsa.Expressions.Models;
public class JsonLiteral : MemoryBlockReference
{
public JsonLiteral()
{
}
public JsonLiteral(string? value)
{
Value = value;
}
public string? Value { get; }
public override MemoryBlock Declare() => new();
public static JsonLiteral From<T>(T value) => new JsonLiteral<T>(value);
}
public class JsonLiteral<T> : JsonLiteral
{
public JsonLiteral()
{
}
public JsonLiteral(T value) : base(JsonSerializer.Serialize(value!))
{
}
}

View file

@ -26,6 +26,4 @@ public class Literal<T> : Literal
public Literal(T value) : base(value!)
{
}
//public new T? Get(ActivityExecutionContext context) => (T?)base.Get(context);
}

View file

@ -4,6 +4,7 @@ using Elsa.Extensions;
using Elsa.Http.Models;
using Elsa.Http.Services;
using Elsa.Workflows.Core.Attributes;
using Elsa.Workflows.Core.Expressions;
using Elsa.Workflows.Core.Models;
using Elsa.Workflows.Management.Models;
using Microsoft.AspNetCore.Http;
@ -40,7 +41,7 @@ public class HttpEndpoint : Trigger<HttpRequest>
Description = "The HTTP methods to accept.",
Options = new[] { "GET", "POST", "PUT", "HEAD", "DELETE" },
UIHint = InputUIHints.CheckList)]
public Input<ICollection<string>> SupportedMethods { get; set; } = new(new[] { HttpMethod.Get.Method });
public Input<ICollection<string>> SupportedMethods { get; set; } = new(JsonLiteral.From(new[]{HttpMethods.Get}));
/// <summary>
/// Allow authenticated requests only.

View file

@ -11,9 +11,9 @@ public class JsonExpression : IExpression
public string? Value { get; }
}
public class JsonExpression<T> : LiteralExpression
public class JsonExpression<T> : JsonExpression
{
public JsonExpression(T? value) : base(value)
public JsonExpression(T? value) : base(JsonSerializer.Serialize(value))
{
}
}

View file

@ -56,6 +56,14 @@ public class Input<T> : Input
public Input(Literal literal) : base(new LiteralExpression(literal.Value), literal, typeof(T))
{
}
public Input(JsonLiteral<T> literal) : base(new JsonExpression(literal.Value), literal, typeof(T))
{
}
public Input(JsonLiteral literal) : base(new JsonExpression(literal.Value), literal, typeof(T))
{
}
public Input(DelegateBlockReference delegateBlockReference) : base(new DelegateExpression(delegateBlockReference), delegateBlockReference, typeof(T))
{