From 79bf1f532ce18f7803f428e8a33706e38c4af6f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20J=C3=A4rvinen?= Date: Wed, 14 Jun 2023 09:49:40 +0300 Subject: [PATCH] Allow Complex JSON as Template input Previously nested and array JSON was converted to flat dictionary. --- .../RunnerConfiguration.cs | 36 ++++++++++++------- .../IotTelemetrySimulator.Test.csproj | 5 ++- .../RunnerConfigurationTest.cs | 16 +++++++++ .../test9-config-array-template.json | 16 +++++++++ 4 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 test/IotTelemetrySimulator.Test/test_files/test9-config-array-template.json diff --git a/src/IotTelemetrySimulator/RunnerConfiguration.cs b/src/IotTelemetrySimulator/RunnerConfiguration.cs index fe477b0..b13bec9 100644 --- a/src/IotTelemetrySimulator/RunnerConfiguration.cs +++ b/src/IotTelemetrySimulator/RunnerConfiguration.cs @@ -9,6 +9,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Newtonsoft.Json; + using Newtonsoft.Json.Linq; public class RunnerConfiguration { @@ -450,26 +451,37 @@ private static string GetTemplateFromSection(IConfigurationSection subSection, s return valueString; } - var dictionaryVals = new Dictionary(); - ConvertToDictionary(templateConfigSection, dictionaryVals, templateConfigSection); - - return JsonConvert.SerializeObject(dictionaryVals); + return JsonConvert.SerializeObject(ConvertToJToken(templateConfigSection)); } - static void ConvertToDictionary(IConfigurationSection configuration, Dictionary data, IConfigurationSection top = null) + // Implemention from https://stackoverflow.com/a/62533775 + private static JToken ConvertToJToken(IConfiguration config) { - var children = configuration.GetChildren(); - foreach (var child in children) + JObject obj = new JObject(); + + foreach (var child in config.GetChildren()) { - if (child.Value == null) + if (child.Path.EndsWith(":0")) { - ConvertToDictionary(configuration.GetSection(child.Key), data, configuration); - continue; + var arr = new JArray(); + + foreach (var arrayChild in config.GetChildren()) + { + arr.Add(ConvertToJToken(arrayChild)); + } + + return arr; } - var key = top != null ? child.Path.Substring(top.Path.Length + 1) : child.Path; - data[key] = child.Value; + obj.Add(child.Key, ConvertToJToken(child)); } + + if (!obj.HasValues && config is IConfigurationSection section) + { + return new JValue(section.Value); + } + + return obj; } } } diff --git a/test/IotTelemetrySimulator.Test/IotTelemetrySimulator.Test.csproj b/test/IotTelemetrySimulator.Test/IotTelemetrySimulator.Test.csproj index fd2baf2..ff86e7a 100644 --- a/test/IotTelemetrySimulator.Test/IotTelemetrySimulator.Test.csproj +++ b/test/IotTelemetrySimulator.Test/IotTelemetrySimulator.Test.csproj @@ -1,4 +1,4 @@ - + net5.0 @@ -32,6 +32,9 @@ + + PreserveNewest + PreserveNewest diff --git a/test/IotTelemetrySimulator.Test/RunnerConfigurationTest.cs b/test/IotTelemetrySimulator.Test/RunnerConfigurationTest.cs index a9133e4..af5e33f 100644 --- a/test/IotTelemetrySimulator.Test/RunnerConfigurationTest.cs +++ b/test/IotTelemetrySimulator.Test/RunnerConfigurationTest.cs @@ -296,5 +296,21 @@ public void When_DeviceSpecific_Interval_Is_Not_Positive_Number_Should_Throw(str Assert.Throws(() => RunnerConfiguration.Load(configuration, NullLogger.Instance)); } + + [Fact] + public void When_Payload_Array_Template_Loads_Correctly() + { + var configuration = new ConfigurationBuilder() + .AddJsonFile("./test_files/test9-config-array-template.json", false, false) + .Build(); + + var target = RunnerConfiguration.Load(configuration, NullLogger.Instance); + var templatedPayload = Assert.IsType(target.PayloadGenerator.Payloads[0]); + Assert.Equal(1, templatedPayload.Variables.Variables.Count); + Assert.True(templatedPayload.Variables.Variables[0].Sequence); + Assert.Equal("device0001", templatedPayload.DeviceId); + Assert.Equal(new[] { "Counter" }, templatedPayload.Variables.Variables[0].GetReferenceVariableNames()); + Assert.Equal("[{\"value1\":{\"value2\":\"$.Value\"}}]", templatedPayload.Template.ToString()); + } } } diff --git a/test/IotTelemetrySimulator.Test/test_files/test9-config-array-template.json b/test/IotTelemetrySimulator.Test/test_files/test9-config-array-template.json new file mode 100644 index 0000000..e0b61f1 --- /dev/null +++ b/test/IotTelemetrySimulator.Test/test_files/test9-config-array-template.json @@ -0,0 +1,16 @@ +{ + "Variables": [ + { + "name": "Value", + "sequence": true, + "values": [ "$.Counter", "true" ] + } + ], + "Payloads": [ + { + "type": "template", + "deviceId": "device0001", + "template": [{"value1": {"value2": "$.Value" } }] + } + ] +}