Skip to content

Commit

Permalink
Support length for objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoraes committed Nov 5, 2024
1 parent 70fe5b2 commit c71b977
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions JexlNet.ExtendedGrammar/ExtendedGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ public static JsonNode Length(JsonNode input)
{
return JsonValue.Create(array.Count);
}
else if (input is JsonObject obj)
{
return JsonValue.Create(obj.Count);
}
else if (input is JsonValue value)
{
return JsonValue.Create(value.ToString().Length);
Expand Down
2 changes: 2 additions & 0 deletions JexlNet.Test/Evaluator/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public async void EvaluateExpression_ReturnString(string input, string expected)
[InlineData(@"2 > 1", true)]
[InlineData(@"2 <= 1", false)]
[InlineData(@"""foo"" && 6 >= 6 && 0 + 1 && true", true)]
[InlineData(@"!!'foo'", true)]
public async void EvaluateExpression_ReturnBoolean(string input, bool expected)
{
Evaluator _evaluator = new(new Grammar());
Expand All @@ -58,6 +59,7 @@ public async void EvaluateExpression_ReturnBoolean(string input, bool expected)
[InlineData("$['foo'].baz.bar", "tek")]
[InlineData("$[{bar:'foo'}.bar].baz.bar", "tek")]
[InlineData("$[{$:'foo'}.$].baz.bar", "tek")]
[InlineData("(foo.bar || foo.baz).bar", "tek")]
public async void EvaluateExpression_WithContext(string input, string expected)
{
JsonObject context = new()
Expand Down
2 changes: 2 additions & 0 deletions JexlNet.Test/ExtendedGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public void String(string expression, string expected)
[InlineData("length('test123')", 7)]
[InlineData("[\"a\",1,\"b\"]|length", 3)]
[InlineData("$length([\"a\",1,\"b\"])", 3)]
[InlineData("{foo:[]}.foo|length", 0)]
[InlineData("{foo:'a',bar:'b'}|length", 2)]
public void Length(string expression, int expected)
{
var jexl = new Jexl(new ExtendedGrammar());
Expand Down

0 comments on commit c71b977

Please sign in to comment.