Skip to content

Commit

Permalink
ObjectMerge allow arrays of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoraes committed Sep 26, 2024
1 parent a77114e commit 25ea688
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 14 additions & 1 deletion JexlNet.ExtendedGrammar/ExtendedGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,7 @@ public static JsonNode ObjectEntries(JsonNode input)
/// <returns>A new object with the properties of the input objects merged together</returns>
public static JsonNode ObjectMerge(JsonNode[] args)
{
JsonObject result = new JsonObject();
JsonObject result = [];

Check failure on line 1535 in JexlNet.ExtendedGrammar/ExtendedGrammar.cs

View workflow job for this annotation

GitHub Actions / build

Feature 'collection expressions' is not available in C# 7.3. Please use language version 12.0 or greater.

Check failure on line 1535 in JexlNet.ExtendedGrammar/ExtendedGrammar.cs

View workflow job for this annotation

GitHub Actions / build

Feature 'collection expressions' is not available in C# 7.3. Please use language version 12.0 or greater.

Check failure on line 1535 in JexlNet.ExtendedGrammar/ExtendedGrammar.cs

View workflow job for this annotation

GitHub Actions / build

Feature 'collection expressions' is not available in C# 7.3. Please use language version 12.0 or greater.

Check failure on line 1535 in JexlNet.ExtendedGrammar/ExtendedGrammar.cs

View workflow job for this annotation

GitHub Actions / build

Feature 'collection expressions' is not available in C# 7.3. Please use language version 12.0 or greater.
foreach (JsonNode arg in args)
{
if (arg is JsonObject obj)
Expand All @@ -1542,6 +1542,19 @@ public static JsonNode ObjectMerge(JsonNode[] args)
result[item.Key] = item.Value?.DeepClone();
}
}
else if (arg is JsonArray array)
{
foreach (JsonNode item in array)
{
if (item is JsonObject objItem)
{
foreach (var subItem in objItem)
{
result[subItem.Key] = subItem.Value?.DeepClone();
}
}
}
}
}
return result;
}
Expand Down
3 changes: 3 additions & 0 deletions JexlNet.Test/ExtendedGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ public void Reduce(string expression, string expected)
[Theory]
[InlineData("$merge({'foo':'bar'},{baz:'tek'})")]
[InlineData("{'foo':'bar'}|merge({baz:'tek'})")]
[InlineData("{'foo':'bar'}|merge([{baz:'tek'}])")]
[InlineData("[{'foo':'bar'}]|merge([{baz:'tek'}])")]
[InlineData("[{foo:'bar'},{baz:'tek'}]|merge")]
[InlineData("[['foo','bar'],['baz','tek']]|toObject")]
public void Objects(string expression)
{
Expand Down

0 comments on commit 25ea688

Please sign in to comment.