Skip to content

Commit

Permalink
Refactor input output assignment and JSON converter
Browse files Browse the repository at this point in the history
The code has been updated to provide clear and meaningful block reference names using the Humanizer library in the IdentityGraphService. The redundancy in assigning input/output Id's in both IdentityGraphService and InputJsonConverter has also been removed, resulting in cleaner, less convoluted code.
  • Loading branch information
sfmskywalker committed Dec 27, 2023
1 parent 4c11545 commit dd0711c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
29 changes: 14 additions & 15 deletions src/modules/Elsa.Workflows.Core/Services/IdentityGraphService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;
using Elsa.Workflows.Models;
using Humanizer;

namespace Elsa.Workflows.Services;

Expand Down Expand Up @@ -56,32 +57,30 @@ public void AssignIdentities(ICollection<ActivityNode> flattenedList)
public void AssignInputOutputs(IActivity activity)
{
var activityDescriptor = _activityRegistry.Find(activity.Type, activity.Version) ?? throw new Exception("Activity descriptor not found");
var inputs = activityDescriptor.GetWrappedInputProperties(activity).Values.Cast<Input>().ToList();
var seed = 0;
var inputDictionary = activityDescriptor.GetWrappedInputProperties(activity);

foreach (var input in inputs)
foreach (var (inputName, input) in inputDictionary)
{
var blockReference = input?.MemoryBlockReference();

if (blockReference != null!)
if (string.IsNullOrEmpty(blockReference.Id))
blockReference.Id = $"{activity.Id}:input-{seed}";

seed++;
if (blockReference == null!)
continue;

if (string.IsNullOrEmpty(blockReference.Id))
blockReference.Id = $"{activity.Id}:input-{inputName.Humanize().Kebaberize()}";
}

seed = 0;

var outputs = activity.GetOutputs();

foreach (var output in outputs)
{
var blockReference = output.Value.MemoryBlockReference();

if (blockReference != null!)
if (string.IsNullOrEmpty(blockReference.Id))
blockReference.Id = $"{activity.Id}:output-{seed}";

seed++;
if (blockReference == null!)
continue;

if (string.IsNullOrEmpty(blockReference.Id))
blockReference.Id = $"{activity.Id}:output-{output.Name.Humanize().Kebaberize()}";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,12 @@ public override Input<T> Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
var expressionTypeNameElement = expressionElement.ValueKind != JsonValueKind.Undefined ? expressionElement.TryGetProperty("type", out var expressionTypeNameElementValue) ? expressionTypeNameElementValue : default : default;
var expressionTypeName = expressionTypeNameElement.ValueKind != JsonValueKind.Undefined ? expressionTypeNameElement.GetString() ?? "Literal" : default;
var expressionDescriptor = expressionTypeName != null ? _expressionDescriptorRegistry.Find(expressionTypeName) : default;

doc.RootElement.TryGetProperty("memoryReference", out var memoryReferenceElement);

var memoryReferenceId = memoryReferenceElement.ValueKind is JsonValueKind.Undefined or JsonValueKind.Null
? default
: memoryReferenceElement.TryGetProperty("id", out var memoryReferenceIdElement)
? memoryReferenceIdElement.GetString()
: default;

var expression = expressionElement.ValueKind == JsonValueKind.Object ? expressionElement.Deserialize<Expression>(options) : new Expression(expressionTypeName!, null);
var memoryBlockReference = expressionDescriptor?.MemoryBlockReferenceFactory();

if (memoryBlockReference == null)
return default!;

if (memoryBlockReference.Id == null!)
memoryBlockReference.Id = memoryReferenceId!;

return (Input<T>)Activator.CreateInstance(typeof(Input<T>), expression, memoryBlockReference)!;
}

Expand All @@ -75,17 +63,12 @@ public override void Write(Utf8JsonWriter writer, Input<T> value, JsonSerializer
throw new JsonException($"Could not find an expression descriptor for expression type '{expressionType}'.");

var targetType = value.Type;
var memoryReferenceId = value.MemoryBlockReference().Id;
var expressionValue = expressionDescriptor.IsSerializable ? expression : null;

var model = new
{
TypeName = targetType,
Expression = expressionValue!,
MemoryReference = new
{
Id = memoryReferenceId
}
Expression = expressionValue!
};

JsonSerializer.Serialize(writer, model, options);
Expand Down

0 comments on commit dd0711c

Please sign in to comment.