Skip to content

Commit 5065659

Browse files
committed
fix(Sdk): Remove obsolete TaskExecutionStrategyDefinition and TaskExecutionMode
fix(Sdk): Fix the GetComponent WorkflowDefinition extension to drastically increase performance Signed-off-by: Charles d'Avernas <charles.davernas@neuroglia.io>
1 parent f58971f commit 5065659

File tree

5 files changed

+22
-88
lines changed

5 files changed

+22
-88
lines changed

src/ServerlessWorkflow.Sdk/Extensions/WorkflowDefinitionExtensions.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
// limitations under the License.
1313

1414
using ServerlessWorkflow.Sdk.Models;
15-
using Json.Pointer;
16-
using Neuroglia;
17-
using Neuroglia.Serialization.Json;
15+
using System.Collections;
16+
using System.Reflection;
1817

1918
namespace ServerlessWorkflow.Sdk;
2019

@@ -61,10 +60,24 @@ public static Uri BuildReferenceTo(this WorkflowDefinition workflow, TaskDefinit
6160
public static TComponent GetComponent<TComponent>(this WorkflowDefinition workflow, string path)
6261
{
6362
ArgumentException.ThrowIfNullOrWhiteSpace(path);
64-
var jsonNode = JsonSerializer.Default.SerializeToNode(workflow)!;
65-
var jsonPointer = JsonPointer.Parse(path);
66-
if (!jsonPointer.TryEvaluate(jsonNode, out var matchNode) || matchNode == null) throw new NullReferenceException($"Failed to find a component definition of type '{typeof(TComponent).Name}' at '{path}'");
67-
return JsonSerializer.Default.Deserialize<TComponent>(matchNode)!;
63+
var pathSegments = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
64+
var currentObject = workflow as object;
65+
foreach (var pathSegment in pathSegments)
66+
{
67+
if (currentObject!.GetType().IsEnumerable() && int.TryParse(pathSegment, out var index)) currentObject = ((IEnumerable)currentObject).OfType<object>().ToList().ElementAt(index);
68+
else
69+
{
70+
var mapEntryType = currentObject.GetType().GetGenericType(typeof(MapEntry<,>));
71+
if (mapEntryType == null)
72+
{
73+
var property = currentObject.GetType().GetProperty(pathSegment, BindingFlags.Default | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase) ?? throw new NullReferenceException($"Failed to find a component definition of type '{typeof(TComponent).Name}' at '{pathSegment}'");
74+
currentObject = property.GetValue(currentObject) ?? throw new NullReferenceException($"Failed to find a component definition of type '{typeof(TComponent).Name}' at '{path}'");
75+
}
76+
else currentObject = mapEntryType.GetProperty(nameof(MapEntry<string, object>.Value))!.GetValue(currentObject);
77+
}
78+
}
79+
if (currentObject is not TComponent component) throw new InvalidCastException($"Component at '{path}' is not of type '{typeof(TComponent).Name}'");
80+
return component;
6881
}
6982

7083
/// <summary>

src/ServerlessWorkflow.Sdk/Models/TaskExecutionStrategyDefinition.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/ServerlessWorkflow.Sdk/Models/Tasks/BranchingDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ public record BranchingDefinition
3131
/// Gets/sets a boolean indicating whether or not the branches should compete each other. If `true` and if a branch completes, it will cancel all other branches then it will return its output as the task's output
3232
/// </summary>
3333
[DataMember(Name = "compete", Order = 1), JsonPropertyName("compete"), JsonPropertyOrder(1), YamlMember(Alias = "compete", Order = 1)]
34-
public required virtual bool Compete { get; set; }
34+
public virtual bool Compete { get; set; }
3535

3636
}

src/ServerlessWorkflow.Sdk/TaskExecutionMode.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/ServerlessWorkflow.Sdk.UnitTests/Cases/IO/WorkflowDefinitionIOTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313

1414
using ServerlessWorkflow.Sdk.IO;
15+
using System.Text;
1516

1617
namespace ServerlessWorkflow.Sdk.UnitTests.Cases.IO;
1718

@@ -31,8 +32,6 @@ public async Task WriteThenRead_Workflow_Definition_ToFrom_Yaml_Should_Work()
3132
await writer.WriteAsync(toSerialize, stream, WorkflowDefinitionFormat.Yaml);
3233
await stream.FlushAsync();
3334
stream.Position = 0;
34-
var yaml = new StreamReader(stream).ReadToEnd();
35-
stream.Position = 0;
3635
var deserialized = await reader.ReadAsync(stream);
3736

3837
//assert

0 commit comments

Comments
 (0)