|
12 | 12 | // limitations under the License.
|
13 | 13 |
|
14 | 14 | using ServerlessWorkflow.Sdk.Models;
|
15 |
| -using Json.Pointer; |
16 |
| -using Neuroglia; |
17 |
| -using Neuroglia.Serialization.Json; |
| 15 | +using System.Collections; |
| 16 | +using System.Reflection; |
18 | 17 |
|
19 | 18 | namespace ServerlessWorkflow.Sdk;
|
20 | 19 |
|
@@ -61,10 +60,24 @@ public static Uri BuildReferenceTo(this WorkflowDefinition workflow, TaskDefinit
|
61 | 60 | public static TComponent GetComponent<TComponent>(this WorkflowDefinition workflow, string path)
|
62 | 61 | {
|
63 | 62 | 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; |
68 | 81 | }
|
69 | 82 |
|
70 | 83 | /// <summary>
|
|
0 commit comments