Skip to content

Allow Complex JSON as Template input #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions src/IotTelemetrySimulator/RunnerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class RunnerConfiguration
{
Expand Down Expand Up @@ -450,26 +451,37 @@ private static string GetTemplateFromSection(IConfigurationSection subSection, s
return valueString;
}

var dictionaryVals = new Dictionary<string, string>();
ConvertToDictionary(templateConfigSection, dictionaryVals, templateConfigSection);

return JsonConvert.SerializeObject(dictionaryVals);
return JsonConvert.SerializeObject(ConvertToJToken(templateConfigSection));
}

static void ConvertToDictionary(IConfigurationSection configuration, Dictionary<string, string> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
Expand Down Expand Up @@ -32,6 +32,9 @@
<AdditionalFiles Include="../../stylecop.json" Link="stylecop.json" />
</ItemGroup>
<ItemGroup>
<None Update="test_files\test9-config-array-template.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="test_files\test5-config-payloads-as-json.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
16 changes: 16 additions & 0 deletions test/IotTelemetrySimulator.Test/RunnerConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,5 +296,21 @@ public void When_DeviceSpecific_Interval_Is_Not_Positive_Number_Should_Throw(str

Assert.Throws<ConfigurationErrorsException>(() => 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<TemplatedPayload>(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());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Variables": [
{
"name": "Value",
"sequence": true,
"values": [ "$.Counter", "true" ]
}
],
"Payloads": [
{
"type": "template",
"deviceId": "device0001",
"template": [{"value1": {"value2": "$.Value" } }]
}
]
}