Skip to content

Commit

Permalink
Refactor namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
MattEdwardsWaggleBee committed Jun 12, 2024
1 parent cc46d7e commit 147345b
Show file tree
Hide file tree
Showing 46 changed files with 349 additions and 263 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
namespace Hyperbee.Json.Evaluators.Parser.Element;
using System.Text.Json;
using Hyperbee.Json.Descriptors.Element.Functions;
using Hyperbee.Json.Filters;
using Hyperbee.Json.Filters.Parser;

public class JsonElementTypeDescriptor : IJsonTypeDescriptor
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 IJsonValueAccessor<TElement> GetAccessor<TElement>() =>
new JsonElementValueAccessor() as IJsonValueAccessor<TElement>;
public IValueAccessor<JsonElement> Accessor
{
get => _accessor ??= new ElementValueAccessor();
}

public IJsonPathFilterEvaluator<TElement> GetFilterEvaluator<TElement>() =>
new JsonPathFilterEvaluator<TElement>( this );
public IFilterEvaluator<JsonElement> FilterEvaluator
{
get => _evaluator ??= new FilterEvaluator<JsonElement>( this );
}

public FilterFunction GetFilterFunction( ParseExpressionContext context ) =>
new FilterElementFunction( context );

public JsonElementTypeDescriptor()
public ElementTypeDescriptor()
{
Functions = new Dictionary<string, FunctionCreator>(
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text.Json;
using Hyperbee.Json.Filters;

namespace Hyperbee.Json.Evaluators.Parser.Element;
namespace Hyperbee.Json.Descriptors.Element;

public class JsonElementValueAccessor : IJsonValueAccessor<JsonElement>
public class ElementValueAccessor : IValueAccessor<JsonElement>
{
public IEnumerable<(JsonElement, string)> EnumerateChildValues( JsonElement value )
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Reflection;
using System.Text.Json;

namespace Hyperbee.Json.Evaluators.Parser.Element;
namespace Hyperbee.Json.Descriptors.Element;

public static class FilterElementHelper
{
public static readonly MethodInfo SelectFirstElementValueMethod;
public static readonly MethodInfo SelectFirstMethod;

public static readonly MethodInfo SelectElementsMethod;

static FilterElementHelper()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Evaluators.Parser.Element;
namespace Hyperbee.Json.Descriptors.Element.Functions;

public class CountElementFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) :
FilterExpressionFunction( methodName, arguments, context )
FilterExtensionFunction( methodName, arguments, context )
{
public const string Name = "count";

Expand All @@ -22,7 +23,7 @@ static CountElementFunction()
.MakeGenericMethod( typeof( JsonElement ) );
}

public override Expression GetExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
public override Expression GetExtensionExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
{
if ( arguments.Count != 1 )
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Linq.Expressions;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Evaluators.Parser.Element;
namespace Hyperbee.Json.Descriptors.Element.Functions;

public class FilterElementFunction( ParseExpressionContext context ) : FilterFunction
{
protected override Expression Evaluate( ReadOnlySpan<char> data, ReadOnlySpan<char> item, ref int start, ref int from )
protected override Expression GetExpressionImpl( ReadOnlySpan<char> data, ReadOnlySpan<char> item, ref int start, ref int from )
{
var queryExp = Expression.Constant( item.ToString() );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Evaluators.Parser.Element;
namespace Hyperbee.Json.Descriptors.Element.Functions;

public class LengthElementFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExpressionFunction( methodName, arguments, context )
public class LengthElementFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExtensionFunction( methodName, arguments, context )
{
public const string Name = "length";

Expand All @@ -15,7 +16,7 @@ static LengthElementFunction()
LengthMethod = typeof( LengthElementFunction ).GetMethod( nameof( Length ), [typeof( JsonElement )] );
}

public override Expression GetExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
public override Expression GetExtensionExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
{
if ( arguments.Count != 1 )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Reflection;
using System.Text.Json;
using System.Text.RegularExpressions;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Evaluators.Parser.Element;
namespace Hyperbee.Json.Descriptors.Element.Functions;

public class MatchElementFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExpressionFunction( methodName, arguments, context )
public class MatchElementFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExtensionFunction( methodName, arguments, context )
{
public const string Name = "match";

Expand All @@ -16,7 +17,7 @@ static MatchElementFunction()
MatchMethod = typeof( MatchElementFunction ).GetMethod( nameof( Match ), [typeof( JsonElement ), typeof( string )] );
}

public override Expression GetExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
public override Expression GetExtensionExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
{
if ( arguments.Count != 2 )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Reflection;
using System.Text.Json;
using System.Text.RegularExpressions;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Evaluators.Parser.Element;
namespace Hyperbee.Json.Descriptors.Element.Functions;

public class SearchElementFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExpressionFunction( methodName, arguments, context )
public class SearchElementFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExtensionFunction( methodName, arguments, context )
{
public const string Name = "search";

Expand All @@ -16,7 +17,7 @@ static SearchElementFunction()
SearchMethod = typeof( SearchElementFunction ).GetMethod( nameof( Search ), [typeof( JsonElement ), typeof( string )] );
}

public override Expression GetExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
public override Expression GetExtensionExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
{
if ( arguments.Count != 2 )
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Linq.Expressions;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Evaluators.Parser.Element;
namespace Hyperbee.Json.Descriptors.Element.Functions;

public class ValueElementFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExpressionFunction( methodName, arguments, context )
public class ValueElementFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExtensionFunction( methodName, arguments, context )
{
public const string Name = "value";

public override Expression GetExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
public override Expression GetExtensionExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
{
if ( arguments.Count != 1 )
{
Expand Down
18 changes: 18 additions & 0 deletions src/Hyperbee.Json/Descriptors/ITypeDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Hyperbee.Json.Filters;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Descriptors;


public interface IJsonTypeDescriptor
{
public Dictionary<string, FunctionCreator> Functions { get; }

public FilterFunction GetFilterFunction( ParseExpressionContext context );
}

public interface ITypeDescriptor<TElement> : IJsonTypeDescriptor
{
public IValueAccessor<TElement> Accessor { get; }
public IFilterEvaluator<TElement> FilterEvaluator { get; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Hyperbee.Json.Evaluators;
namespace Hyperbee.Json.Descriptors;

public interface IJsonValueAccessor<TElement>
public interface IValueAccessor<TElement>
{
IEnumerable<(TElement, string)> EnumerateChildValues( TElement value );
TElement GetElementAt( TElement value, int index );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Text.Json.Nodes;
using Hyperbee.Json.Extensions;

namespace Hyperbee.Json.Evaluators.Parser.Node;
namespace Hyperbee.Json.Descriptors.Node;

public static class FilterNodeHelper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json.Nodes;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Evaluators.Parser.Node;
namespace Hyperbee.Json.Descriptors.Node.Functions;

public class CountNodeFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) :
FilterExpressionFunction( methodName, arguments, context )
FilterExtensionFunction( methodName, arguments, context )
{
public const string Name = "count";

Expand All @@ -22,7 +23,7 @@ static CountNodeFunction()
.MakeGenericMethod( typeof( JsonNode ) );
}

public override Expression GetExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
public override Expression GetExtensionExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
{
if ( arguments.Count != 1 )
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Linq.Expressions;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Evaluators.Parser.Node;
namespace Hyperbee.Json.Descriptors.Node.Functions;

public class FilterNodeFunction( ParseExpressionContext context ) : FilterFunction
{
protected override Expression Evaluate( ReadOnlySpan<char> data, ReadOnlySpan<char> item, ref int start, ref int from )
protected override Expression GetExpressionImpl( ReadOnlySpan<char> data, ReadOnlySpan<char> item, ref int start, ref int from )
{
var queryExp = Expression.Constant( item.ToString() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Evaluators.Parser.Node;
namespace Hyperbee.Json.Descriptors.Node.Functions;

public class LengthNodeFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExpressionFunction( methodName, arguments, context )
public class LengthNodeFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExtensionFunction( methodName, arguments, context )
{
public const string Name = "length";

Expand All @@ -16,7 +17,7 @@ static LengthNodeFunction()
LengthMethod = typeof( LengthNodeFunction ).GetMethod( nameof( Length ), [typeof( JsonNode )] );
}

public override Expression GetExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
public override Expression GetExtensionExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
{
if ( arguments.Count != 1 )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Reflection;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Evaluators.Parser.Node;
namespace Hyperbee.Json.Descriptors.Node.Functions;

public class MatchNodeFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExpressionFunction( methodName, arguments, context )
public class MatchNodeFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExtensionFunction( methodName, arguments, context )
{
public const string Name = "match";

Expand All @@ -16,7 +17,7 @@ static MatchNodeFunction()
MatchMethod = typeof( MatchNodeFunction ).GetMethod( nameof( Match ), [typeof( JsonNode ), typeof( string )] );
}

public override Expression GetExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
public override Expression GetExtensionExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
{
if ( arguments.Count != 2 )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Reflection;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Evaluators.Parser.Node;
namespace Hyperbee.Json.Descriptors.Node.Functions;

public class SearchNodeFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExpressionFunction( methodName, arguments, context )
public class SearchNodeFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExtensionFunction( methodName, arguments, context )
{
public const string Name = "search";

Expand All @@ -16,7 +17,7 @@ static SearchNodeFunction()
SearchMethod = typeof( SearchNodeFunction ).GetMethod( nameof( Search ), [typeof( JsonNode ), typeof( string )] );
}

public override Expression GetExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
public override Expression GetExtensionExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
{
if ( arguments.Count != 2 )
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Linq.Expressions;
using Hyperbee.Json.Filters.Parser;

namespace Hyperbee.Json.Evaluators.Parser.Node;
namespace Hyperbee.Json.Descriptors.Node.Functions;

public class ValueNodeFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExpressionFunction( methodName, arguments, context )
public class ValueNodeFunction( string methodName, IList<string> arguments, ParseExpressionContext context ) : FilterExtensionFunction( methodName, arguments, context )
{
public const string Name = "value";

public override Expression GetExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
public override Expression GetExtensionExpression( string methodName, IList<string> arguments, ParseExpressionContext context )
{
if ( arguments.Count != 1 )
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
namespace Hyperbee.Json.Evaluators.Parser.Node;
using System.Text.Json.Nodes;
using Hyperbee.Json.Descriptors.Node.Functions;
using Hyperbee.Json.Filters;
using Hyperbee.Json.Filters.Parser;

public class JsonNodeTypeDescriptor : IJsonTypeDescriptor
namespace Hyperbee.Json.Descriptors.Node;

public class NodeTypeDescriptor : ITypeDescriptor<JsonNode>
{
public Dictionary<string, FunctionCreator> Functions { get; }
private FilterEvaluator<JsonNode> _evaluator;
private NodeValueAccessor _accessor;
public Dictionary<string, FunctionCreator> Functions { get; init; }

public IJsonValueAccessor<TElement> GetAccessor<TElement>() =>
new JsonNodeValueAccessor() as IJsonValueAccessor<TElement>;
public IValueAccessor<JsonNode> Accessor
{
get => _accessor ??= new NodeValueAccessor();
}

public IJsonPathFilterEvaluator<TElement> GetFilterEvaluator<TElement>() =>
new JsonPathFilterEvaluator<TElement>( this );
public IFilterEvaluator<JsonNode> FilterEvaluator
{
get => _evaluator ??= new FilterEvaluator<JsonNode>( this );
}

public FilterFunction GetFilterFunction( ParseExpressionContext context ) =>
new FilterNodeFunction( context );

public JsonNodeTypeDescriptor()
public NodeTypeDescriptor()
{
Functions = new Dictionary<string, FunctionCreator>(
[
Expand Down
Loading

0 comments on commit 147345b

Please sign in to comment.