From 60c0d2b0ec4d82138e5a290b33d2c5289be9e481 Mon Sep 17 00:00:00 2001 From: "Dr. Jochen Manns" Date: Sat, 24 Aug 2024 21:18:14 +0200 Subject: [PATCH] Add support for object properties. (ZeraGmbH/websam#683) --- Library/Extensions/Builder/ModelBlock.cs | 15 ++++++---- Tests/BlocklyNetTests.csproj | 2 +- Tests/Customization/ModelGeneratorTests.cs | 33 +++++++++++++++++++++- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Library/Extensions/Builder/ModelBlock.cs b/Library/Extensions/Builder/ModelBlock.cs index c3dff88..a5ce741 100644 --- a/Library/Extensions/Builder/ModelBlock.cs +++ b/Library/Extensions/Builder/ModelBlock.cs @@ -95,6 +95,8 @@ public PropertyInformation(PropertyInfo info) : this(info.Name, info.PropertyTyp {typeof(int?), new("Number","math_number","NUM","0")}, /* String. */ {typeof(string), new("String","text","TEXT","")}, + /* Object. */ + {typeof(object), new("","","","")}, }; /// @@ -245,7 +247,7 @@ private static JsonObject CreateBlockDefinition(ModelCache models) { ["type"] = "input_value", ["name"] = prop.Name, - ["check"] = new JsonArray($"Array({itemType})", "Array") + ["check"] = string.IsNullOrEmpty(itemType) ? "Array" : new JsonArray($"Array({itemType})", "Array") }); continue; @@ -257,12 +259,15 @@ private static JsonObject CreateBlockDefinition(ModelCache models) : models[prop.Type]; /* Generate the related input. */ - args.Add(new JsonObject + var input = new JsonObject { ["type"] = "input_value", ["name"] = prop.Name, - ["check"] = knownType - }); + }; + + if (!string.IsNullOrEmpty(knownType)) input["check"] = knownType; + + args.Add(input); } /* Build a message string from our arguments. */ @@ -305,7 +310,7 @@ private static JsonObject CreateToolboxEntry(ModelCache models) ["fields"] = new JsonObject { ["VALUE"] = Enum.GetValues(prop.Type).GetValue(0)!.ToString() } } }; - else if (_supportedTypes.TryGetValue(prop.Type, out var info)) + else if (_supportedTypes.TryGetValue(prop.Type, out var info) && !string.IsNullOrEmpty(info.BlockType)) inputs[prop.Name] = new JsonObject { ["shadow"] = new JsonObject diff --git a/Tests/BlocklyNetTests.csproj b/Tests/BlocklyNetTests.csproj index 9994f32..85e7483 100644 --- a/Tests/BlocklyNetTests.csproj +++ b/Tests/BlocklyNetTests.csproj @@ -14,7 +14,7 @@ - + diff --git a/Tests/Customization/ModelGeneratorTests.cs b/Tests/Customization/ModelGeneratorTests.cs index 4a95d7d..935fd74 100644 --- a/Tests/Customization/ModelGeneratorTests.cs +++ b/Tests/Customization/ModelGeneratorTests.cs @@ -55,6 +55,17 @@ public class ArrayRef public List Values { get; set; } = []; } + private class ObjectValue + { + public object SingleRequired { get; set; } = null!; + + public object? Single { get; set; } + + public List MultipleRequired { get; set; } = []; + + public List Multiple { get; set; } = []; + } + private class ConstantBlock(object? value) : Block { private readonly object? _value = value; @@ -163,4 +174,24 @@ public async Task Can_Create_Blockly_Model_Dynamically() Assert.That(testModel.StringProp, Is.EqualTo("testString")); }); } -} \ No newline at end of file + + [Test] + public void Can_Use_Object_Data_Type() + { + var models = new ModelCache(); + + models.Add("withObject"); + + var outer = ModelBlock.Initialize("obj", "OBJ", models, (type, key, name) => false); + + var blockJson = JsonSerializer.Serialize(outer.Item1, JsonUtils.JsonSettings); + var toolJson = JsonSerializer.Serialize(outer.Item2, JsonUtils.JsonSettings); + + Assert.Multiple(() => + { + Assert.That(blockJson, Has.Length.EqualTo(642)); + Assert.That(toolJson, Has.Length.EqualTo(55)); + }); + } +} +