From 97d9dccea193db418d9f4945a638449e86cdcf1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bojan=20Malini=C4=87?= <94437690+boma96@users.noreply.github.com> Date: Tue, 5 Nov 2024 09:51:23 +0100 Subject: [PATCH] Add support for any dict value type (#197) * Add support for any dict type * Remove unnecessary usings --- src/ConductorSharp.Engine/Util/ExpressionUtil.cs | 10 +++++++++- .../Samples/Tasks/DictionaryInputTask.cs | 4 +++- .../Workflows/DictionaryInitializationWorkflow.cs | 5 ++++- .../Workflows/DictionaryInitializationWorkflow.json | 8 +++++++- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/ConductorSharp.Engine/Util/ExpressionUtil.cs b/src/ConductorSharp.Engine/Util/ExpressionUtil.cs index ff46061..b93b127 100644 --- a/src/ConductorSharp.Engine/Util/ExpressionUtil.cs +++ b/src/ConductorSharp.Engine/Util/ExpressionUtil.cs @@ -192,7 +192,15 @@ private static JArray ParseListInit(ListInitExpression listInitExpression) } private static bool IsDictionaryInitialization(Expression expr) => - expr is ListInitExpression listExpr && listExpr.NewExpression.Type.IsAssignableTo(typeof(IDictionary)); + expr is ListInitExpression listExpr + && listExpr + .NewExpression.Type.GetInterfaces() + .Any( + iface => + iface.IsGenericType + && iface.GetGenericTypeDefinition() == typeof(IDictionary<,>) + && iface.GenericTypeArguments[0] == typeof(string) + ); private static JObject ParseDictionaryInitialization(ListInitExpression listExpression) { diff --git a/test/ConductorSharp.Engine.Tests/Samples/Tasks/DictionaryInputTask.cs b/test/ConductorSharp.Engine.Tests/Samples/Tasks/DictionaryInputTask.cs index d2f2304..3bdec65 100644 --- a/test/ConductorSharp.Engine.Tests/Samples/Tasks/DictionaryInputTask.cs +++ b/test/ConductorSharp.Engine.Tests/Samples/Tasks/DictionaryInputTask.cs @@ -2,7 +2,9 @@ public class DictionaryInputTaskInput : IRequest { - public IDictionary Input { get; set; } + public IDictionary Object { get; set; } + public IDictionary StringObject { get; set; } + public IDictionary IntObject { get; set; } } public class DictionaryInputTaskOutput; diff --git a/test/ConductorSharp.Engine.Tests/Samples/Workflows/DictionaryInitializationWorkflow.cs b/test/ConductorSharp.Engine.Tests/Samples/Workflows/DictionaryInitializationWorkflow.cs index 6c68aed..f02672f 100644 --- a/test/ConductorSharp.Engine.Tests/Samples/Workflows/DictionaryInitializationWorkflow.cs +++ b/test/ConductorSharp.Engine.Tests/Samples/Workflows/DictionaryInitializationWorkflow.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json.Nodes; using System.Threading.Tasks; namespace ConductorSharp.Engine.Tests.Samples.Workflows @@ -31,7 +32,9 @@ public override void BuildDefinition() wf => new() { - Input = new Dictionary() { { "test1", "value" }, { "test2", new { MyProp = "test" } } } + Object = new Dictionary() { { "test1", "value" }, { "test2", new { MyProp = "test" } } }, + StringObject = new Dictionary() { { "test", "test" } }, + IntObject = new Dictionary() { { "test", 1 } } } ); } diff --git a/test/ConductorSharp.Engine.Tests/Samples/Workflows/DictionaryInitializationWorkflow.json b/test/ConductorSharp.Engine.Tests/Samples/Workflows/DictionaryInitializationWorkflow.json index e539515..a32c027 100644 --- a/test/ConductorSharp.Engine.Tests/Samples/Workflows/DictionaryInitializationWorkflow.json +++ b/test/ConductorSharp.Engine.Tests/Samples/Workflows/DictionaryInitializationWorkflow.json @@ -6,11 +6,17 @@ "name": "dictionary_input_task", "taskReferenceName": "dictionary_task", "inputParameters": { - "input": { + "object": { "test1": "value", "test2": { "my_prop": "test" } + }, + "string_object": { + "test": "test" + }, + "int_object": { + "test": 1 } }, "type": "SIMPLE",