-
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]: Function Registration (#20)
* Add custom function example and test * Made FilterParser generic with TNode * Context maybe everything, but it need not be everywhere * Separated parser expressions from extension functions * Improved whitespace handling * Switching to ParserState * Simplified Function Signatures * Removed base FilterFunction and changed GetSelectFunction so it has a default implementation * Simplified TypeDescriptor and simplified Filter compare and converting. --------- Co-authored-by: Brenton Farmer <[email protected]> Co-authored-by: Matt Edwards <[email protected]>
- Loading branch information
1 parent
bdd85df
commit a25ca64
Showing
50 changed files
with
994 additions
and
836 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
44 changes: 11 additions & 33 deletions
44
src/Hyperbee.Json/Descriptors/Element/ElementTypeDescriptor.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,50 +1,28 @@ | ||
using System.Linq.Expressions; | ||
using System.Text.Json; | ||
using System.Text.Json; | ||
using Hyperbee.Json.Descriptors.Element.Functions; | ||
using Hyperbee.Json.Filters; | ||
using Hyperbee.Json.Filters.Parser; | ||
|
||
namespace Hyperbee.Json.Descriptors.Element; | ||
|
||
public class ElementTypeDescriptor : ITypeDescriptor<JsonElement> | ||
{ | ||
private FilterEvaluator<JsonElement> _evaluator; | ||
private ElementValueAccessor _accessor; | ||
public Dictionary<string, FunctionCreator> Functions { get; init; } | ||
|
||
public IValueAccessor<JsonElement> Accessor | ||
{ | ||
get => _accessor ??= new ElementValueAccessor(); | ||
} | ||
|
||
public IFilterEvaluator<JsonElement> FilterEvaluator | ||
{ | ||
get => _evaluator ??= new FilterEvaluator<JsonElement>( this ); | ||
} | ||
public FunctionRegistry Functions { get; } = new(); | ||
|
||
public FilterFunction GetSelectFunction( ParseExpressionContext context ) => | ||
new SelectElementFunction( context ); | ||
|
||
public Expression GetValueExpression( Expression expression ) | ||
{ | ||
if ( expression is null ) | ||
return null; | ||
|
||
return expression.Type == typeof( IEnumerable<JsonElement> ) | ||
? Expression.Invoke( ValueElementFunction.ValueExpression, expression ) | ||
: expression; | ||
} | ||
public IValueAccessor<JsonElement> Accessor => | ||
_accessor ??= new ElementValueAccessor(); | ||
|
||
public IFilterEvaluator<JsonElement> FilterEvaluator => | ||
_evaluator ??= new FilterEvaluator<JsonElement>( this ); | ||
|
||
public ElementTypeDescriptor() | ||
{ | ||
Functions = new Dictionary<string, FunctionCreator>( | ||
[ | ||
new KeyValuePair<string, FunctionCreator>( CountElementFunction.Name, context => new CountElementFunction( context ) ), | ||
new KeyValuePair<string, FunctionCreator>( LengthElementFunction.Name, context => new LengthElementFunction( context ) ), | ||
new KeyValuePair<string, FunctionCreator>( MatchElementFunction.Name, context => new MatchElementFunction( context ) ), | ||
new KeyValuePair<string, FunctionCreator>( SearchElementFunction.Name, context => new SearchElementFunction( context ) ), | ||
new KeyValuePair<string, FunctionCreator>( ValueElementFunction.Name, context => new ValueElementFunction( context ) ), | ||
] ); | ||
Functions.Register( CountElementFunction.Name, () => new CountElementFunction() ); | ||
Functions.Register( LengthElementFunction.Name, () => new LengthElementFunction() ); | ||
Functions.Register( MatchElementFunction.Name, () => new MatchElementFunction() ); | ||
Functions.Register( SearchElementFunction.Name, () => new SearchElementFunction() ); | ||
Functions.Register( ValueElementFunction.Name, () => new ValueElementFunction() ); | ||
} | ||
} |
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
Oops, something went wrong.