-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [FEATURE]: Improve parser error handling (#32) * Improve query parser error handling of quoted and unquoted select names * Correctly throw when there are unexpected characters at the end of a query * Refactored JsonHelper and extensions --------- Co-authored-by: Brenton Farmer <[email protected]> * [FEATURE]: Improve documentation and comments (#34) * Update comments and documentation --------- Co-authored-by: Brenton Farmer <[email protected]> * Feature/30 feature test with jsonpath compliance test suite (#35) - This project pulls the cts.json directly from the Compliance Test Suite - Improved handling of whitespace - Validation of non-singular queries and constants - Added support for I-Regexp format (RFC-9485). - Improved filter expression parsing - Switched to using an internal type system for comparing values - Memory and performance improvements --------- Co-authored-by: Brenton Farmer <[email protected]> * Previous version was 'v1.2.1'. Version now 'v1.3.0'. --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Brenton Farmer <[email protected]> Co-authored-by: MattEdwardsWaggleBee <[email protected]>
- Loading branch information
1 parent
85f895e
commit 55a9273
Showing
89 changed files
with
26,075 additions
and
1,878 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 14 additions & 11 deletions
25
src/Hyperbee.Json/Descriptors/Element/Functions/CountElementFunction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,25 @@ | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Text.Json; | ||
|
||
using Hyperbee.Json.Filters.Parser; | ||
using Hyperbee.Json.Filters.Values; | ||
|
||
namespace Hyperbee.Json.Descriptors.Element.Functions; | ||
|
||
public class CountElementFunction() : FilterExtensionFunction( argumentCount: 1 ) | ||
public class CountElementFunction() : FilterExtensionFunction( CountMethodInfo, FilterExtensionInfo.MustCompare ) | ||
{ | ||
public const string Name = "count"; | ||
private static readonly Expression CountExpression = Expression.Constant( (Func<IEnumerable<JsonElement>, float>) Count ); | ||
|
||
protected override Expression GetExtensionExpression( Expression[] arguments ) | ||
{ | ||
return Expression.Invoke( CountExpression, arguments[0] ); | ||
} | ||
private static readonly MethodInfo CountMethodInfo = GetMethod<CountElementFunction>( nameof( Count ) ); | ||
|
||
public static float Count( IEnumerable<JsonElement> elements ) | ||
public static INodeType Count( INodeType input ) | ||
{ | ||
return elements.Count(); | ||
switch ( input ) | ||
{ | ||
case NodesType<JsonElement> nodes: | ||
if ( nodes.IsNormalized && !nodes.Any() ) | ||
return new ValueType<float>( 1F ); | ||
return new ValueType<float>( nodes.Count() ); | ||
default: | ||
return new ValueType<float>( 1F ); | ||
} | ||
} | ||
} |
38 changes: 26 additions & 12 deletions
38
src/Hyperbee.Json/Descriptors/Element/Functions/LengthElementFunction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,42 @@ | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using Hyperbee.Json.Filters.Parser; | ||
using Hyperbee.Json.Filters.Values; | ||
|
||
namespace Hyperbee.Json.Descriptors.Element.Functions; | ||
|
||
public class LengthElementFunction() : FilterExtensionFunction( argumentCount: 1 ) | ||
public class LengthElementFunction() : FilterExtensionFunction( LengthMethodInfo, FilterExtensionInfo.MustCompare | FilterExtensionInfo.ExpectNormalized ) | ||
{ | ||
public const string Name = "length"; | ||
private static readonly Expression LengthExpression = Expression.Constant( (Func<IEnumerable<JsonElement>, float>) Length ); | ||
private static readonly MethodInfo LengthMethodInfo = GetMethod<LengthElementFunction>( nameof( Length ) ); | ||
|
||
protected override Expression GetExtensionExpression( Expression[] arguments ) | ||
public static INodeType Length( INodeType input ) | ||
{ | ||
return Expression.Invoke( LengthExpression, arguments[0] ); | ||
return input switch | ||
{ | ||
NodesType<JsonElement> nodes => LengthImpl( nodes.FirstOrDefault() ), | ||
ValueType<string> valueString => new ValueType<float>( valueString.Value.Length ), | ||
Null or Nothing => input, | ||
_ => Constants.Nothing | ||
}; | ||
} | ||
|
||
public static float Length( IEnumerable<JsonElement> elements ) | ||
public static INodeType LengthImpl( object value ) | ||
{ | ||
var element = elements.FirstOrDefault(); | ||
return element.ValueKind switch | ||
return value switch | ||
{ | ||
JsonValueKind.String => element.GetString()?.Length ?? 0, | ||
JsonValueKind.Array => element.GetArrayLength(), | ||
JsonValueKind.Object => element.EnumerateObject().Count(), | ||
_ => 0 | ||
string str => new ValueType<float>( str.Length ), | ||
Array array => new ValueType<float>( array.Length ), | ||
System.Collections.ICollection collection => new ValueType<float>( collection.Count ), | ||
System.Collections.IEnumerable enumerable => new ValueType<float>( enumerable.Cast<object>().Count() ), | ||
JsonElement node => node.ValueKind switch | ||
{ | ||
JsonValueKind.String => new ValueType<float>( node.GetString()?.Length ?? 0 ), | ||
JsonValueKind.Array => new ValueType<float>( node.EnumerateArray().Count() ), | ||
JsonValueKind.Object => new ValueType<float>( node.EnumerateObject().Count() ), | ||
_ => Constants.Null | ||
}, | ||
_ => Constants.Null | ||
}; | ||
} | ||
} |
35 changes: 22 additions & 13 deletions
35
src/Hyperbee.Json/Descriptors/Element/Functions/MatchElementFunction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,39 @@ | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using System.Text.RegularExpressions; | ||
using Hyperbee.Json.Filters; | ||
using Hyperbee.Json.Filters.Parser; | ||
using Hyperbee.Json.Filters.Values; | ||
|
||
namespace Hyperbee.Json.Descriptors.Element.Functions; | ||
|
||
public class MatchElementFunction() : FilterExtensionFunction( argumentCount: 2 ) | ||
public class MatchElementFunction() : FilterExtensionFunction( MatchMethodInfo, FilterExtensionInfo.MustNotCompare ) | ||
{ | ||
public const string Name = "match"; | ||
private static readonly Expression MatchExpression = Expression.Constant( (Func<IEnumerable<JsonElement>, string, bool>) Match ); | ||
private static readonly MethodInfo MatchMethodInfo = GetMethod<MatchElementFunction>( nameof( Match ) ); | ||
|
||
protected override Expression GetExtensionExpression( Expression[] arguments ) | ||
public static INodeType Match( INodeType input, INodeType regex ) | ||
{ | ||
return Expression.Invoke( MatchExpression, arguments[0], arguments[1] ); | ||
return input switch | ||
{ | ||
NodesType<JsonElement> nodes when regex is ValueType<string> stringValue => | ||
MatchImpl( nodes, stringValue.Value ), | ||
NodesType<JsonElement> nodes when regex is NodesType<JsonElement> stringValue => | ||
MatchImpl( nodes, stringValue.Value.FirstOrDefault().GetString() ), | ||
_ => Constants.False | ||
}; | ||
} | ||
|
||
public static bool Match( IEnumerable<JsonElement> elements, string regex ) | ||
public static INodeType MatchImpl( NodesType<JsonElement> nodes, string regex ) | ||
{ | ||
var value = elements.FirstOrDefault().GetString(); | ||
var value = nodes.FirstOrDefault(); | ||
|
||
if ( value == null ) | ||
{ | ||
return false; | ||
} | ||
if ( value.ValueKind != JsonValueKind.String ) | ||
return Constants.False; | ||
|
||
var stringValue = value.GetString() ?? string.Empty; | ||
|
||
var regexPattern = new Regex( regex.Trim( '\"', '\'' ) ); | ||
return regexPattern.IsMatch( $"^{value}$" ); | ||
var regexPattern = new Regex( $"^{IRegexp.ConvertToIRegexp( regex )}$" ); | ||
return new ValueType<bool>( regexPattern.IsMatch( stringValue ) ); | ||
} | ||
} |
Oops, something went wrong.