Skip to content

Commit

Permalink
Merge pull request ZeraGmbH#29 from JMS-1/master
Browse files Browse the repository at this point in the history
Add support for object properties. (ZeraGmbH/websam#683)
  • Loading branch information
JMS-1 authored Aug 24, 2024
2 parents b22cb8d + 60c0d2b commit 9c2a1dc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
15 changes: 10 additions & 5 deletions Library/Extensions/Builder/ModelBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("","","","")},
};

/// <summary>
Expand Down Expand Up @@ -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;
Expand All @@ -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. */
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Tests/BlocklyNetTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NUnit" Version="4.2.0" />
<PackageReference Include="NUnit" Version="4.2.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2" />
Expand Down
33 changes: 32 additions & 1 deletion Tests/Customization/ModelGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ public class ArrayRef
public List<InnerRef> Values { get; set; } = [];
}

private class ObjectValue
{
public object SingleRequired { get; set; } = null!;

public object? Single { get; set; }

public List<object> MultipleRequired { get; set; } = [];

public List<object?> Multiple { get; set; } = [];
}

private class ConstantBlock(object? value) : Block
{
private readonly object? _value = value;
Expand Down Expand Up @@ -163,4 +174,24 @@ public async Task Can_Create_Blockly_Model_Dynamically()
Assert.That(testModel.StringProp, Is.EqualTo("testString"));
});
}
}

[Test]
public void Can_Use_Object_Data_Type()
{
var models = new ModelCache();

models.Add<ObjectValue>("withObject");

var outer = ModelBlock<ObjectValue>.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));
});
}
}

0 comments on commit 9c2a1dc

Please sign in to comment.