Skip to content

Commit

Permalink
Update generics to be TNode vs TElement
Browse files Browse the repository at this point in the history
  • Loading branch information
MattEdwardsWaggleBee committed Jun 14, 2024
1 parent a9331e6 commit 16e57a9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 39 deletions.
8 changes: 4 additions & 4 deletions src/Hyperbee.Json/Descriptors/ITypeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public interface IJsonTypeDescriptor
public FilterFunction GetFilterFunction( ParseExpressionContext context );
}

public interface ITypeDescriptor<TElement> : IJsonTypeDescriptor
public interface ITypeDescriptor<TNode> : IJsonTypeDescriptor
{
public IValueAccessor<TElement> Accessor { get; }
public IFilterEvaluator<TElement> FilterEvaluator { get; }
public IValueAccessor<TNode> Accessor { get; }
public IFilterEvaluator<TNode> FilterEvaluator { get; }

public void Deconstruct( out IValueAccessor<TElement> valueAccessor, out IFilterEvaluator<TElement> filterEvaluator )
public void Deconstruct( out IValueAccessor<TNode> valueAccessor, out IFilterEvaluator<TNode> filterEvaluator )
{
valueAccessor = Accessor;
filterEvaluator = FilterEvaluator;
Expand Down
14 changes: 7 additions & 7 deletions src/Hyperbee.Json/Descriptors/IValueAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Hyperbee.Json.Descriptors;

public interface IValueAccessor<TElement>
public interface IValueAccessor<TNode>
{
IEnumerable<(TElement, string)> EnumerateChildren( TElement value, bool includeValues = true );
TElement GetElementAt( in TElement value, int index );
bool IsObjectOrArray( in TElement current );
bool IsArray( in TElement current, out int length );
bool IsObject( in TElement current );
bool TryGetChildValue( in TElement current, string childKey, out TElement childValue );
IEnumerable<(TNode, string)> EnumerateChildren( TNode value, bool includeValues = true );
TNode GetElementAt( in TNode value, int index );
bool IsObjectOrArray( in TNode current );
bool IsArray( in TNode current, out int length );
bool IsObject( in TNode current );
bool TryGetChildValue( in TNode current, string childKey, out TNode childValue );
}
10 changes: 5 additions & 5 deletions src/Hyperbee.Json/Filters/FilterEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

namespace Hyperbee.Json.Filters;

public sealed class FilterEvaluator<TType> : IFilterEvaluator<TType>
public sealed class FilterEvaluator<TNode> : IFilterEvaluator<TNode>
{
private readonly IJsonTypeDescriptor _typeDescriptor;

// ReSharper disable once StaticMemberInGenericType
private static readonly ConcurrentDictionary<string, Func<TType, TType, bool>> Compiled = new();
private static readonly ConcurrentDictionary<string, Func<TNode, TNode, bool>> Compiled = new();

public FilterEvaluator( ITypeDescriptor<TType> typeDescriptor )
public FilterEvaluator( ITypeDescriptor<TNode> typeDescriptor )
{
_typeDescriptor = typeDescriptor;
}

public object Evaluate( string filter, TType current, TType root )
public object Evaluate( string filter, TNode current, TNode root )
{
var compiled = Compiled.GetOrAdd( filter, _ => JsonPathExpression.Compile<TType>( filter, _typeDescriptor ) );
var compiled = Compiled.GetOrAdd( filter, _ => JsonPathExpression.Compile<TNode>( filter, _typeDescriptor ) );

try
{
Expand Down
4 changes: 2 additions & 2 deletions src/Hyperbee.Json/Filters/IFilterEvaluator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

namespace Hyperbee.Json.Filters;

public interface IFilterEvaluator<in TType>
public interface IFilterEvaluator<in TNode>
{
public object Evaluate( string filter, TType current, TType root );
public object Evaluate( string filter, TNode current, TNode root );
}
8 changes: 4 additions & 4 deletions src/Hyperbee.Json/Filters/Parser/JsonPathExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public class JsonPathExpression

private static readonly MethodInfo ObjectEquals = typeof( object ).GetMethod( "Equals", [typeof( object ), typeof( object )] );

public static Func<TElement, TElement, bool> Compile<TElement>( ReadOnlySpan<char> filter, IJsonTypeDescriptor typeDescriptor )
public static Func<TNode, TNode, bool> Compile<TNode>( ReadOnlySpan<char> filter, IJsonTypeDescriptor typeDescriptor )
{
var currentParam = Expression.Parameter( typeof( TElement ) );
var rootParam = Expression.Parameter( typeof( TElement ) );
var currentParam = Expression.Parameter( typeof( TNode ) );
var rootParam = Expression.Parameter( typeof( TNode ) );
var expressionContext = new ParseExpressionContext( currentParam, rootParam, typeDescriptor );
var expression = Parse( filter, expressionContext );

return Expression
.Lambda<Func<TElement, TElement, bool>>( expression, currentParam, rootParam )
.Lambda<Func<TNode, TNode, bool>>( expression, currentParam, rootParam )
.Compile();
}

Expand Down
22 changes: 11 additions & 11 deletions src/Hyperbee.Json/JsonPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ namespace Hyperbee.Json;
// https://ietf-wg-jsonpath.github.io/draft-ietf-jsonpath-base/draft-ietf-jsonpath-base.html
// https://github.com/ietf-wg-jsonpath/draft-ietf-jsonpath-base

public static class JsonPath<TElement>
public static class JsonPath<TNode>
{
private static readonly ITypeDescriptor<TElement> Descriptor = JsonTypeDescriptorRegistry.GetDescriptor<TElement>();
private static readonly ITypeDescriptor<TNode> Descriptor = JsonTypeDescriptorRegistry.GetDescriptor<TNode>();

public static IEnumerable<TElement> Select( in TElement value, string query )
public static IEnumerable<TNode> Select( in TNode value, string query )
{
return EnumerateMatches( value, value, query );
}

internal static IEnumerable<TElement> Select( in TElement value, TElement root, string query )
internal static IEnumerable<TNode> Select( in TNode value, TNode root, string query )
{
return EnumerateMatches( value, root, query );
}

private static IEnumerable<TElement> EnumerateMatches( in TElement value, in TElement root, string query )
private static IEnumerable<TNode> EnumerateMatches( in TNode value, in TNode root, string query )
{
ArgumentException.ThrowIfNullOrWhiteSpace( query );

Expand All @@ -79,7 +79,7 @@ private static IEnumerable<TElement> EnumerateMatches( in TElement value, in TEl
return EnumerateMatches( root, new NodeArgs( value, segments ) );
}

private static IEnumerable<TElement> EnumerateMatches( TElement root, NodeArgs args )
private static IEnumerable<TNode> EnumerateMatches( TNode root, NodeArgs args )
{
var stack = new Stack<NodeArgs>( 16 );

Expand Down Expand Up @@ -224,7 +224,7 @@ private static IEnumerable<TElement> EnumerateMatches( TElement root, NodeArgs a

yield break;

static void Push( Stack<NodeArgs> n, in TElement v, in JsonPathSegment s ) => n.Push( new NodeArgs( v, s ) );
static void Push( Stack<NodeArgs> n, in TNode v, in JsonPathSegment s ) => n.Push( new NodeArgs( v, s ) );
}

private static bool Truthy( object value )
Expand All @@ -238,7 +238,7 @@ private static IEnumerable<int> EnumerateArrayIndices( int length )
yield return index;
}

private static IEnumerable<int> EnumerateSlice( TElement value, string sliceExpr )
private static IEnumerable<int> EnumerateSlice( TNode value, string sliceExpr )
{
if ( !Descriptor.Accessor.IsArray( value, out var length ) )
yield break;
Expand Down Expand Up @@ -266,12 +266,12 @@ private static IEnumerable<int> EnumerateSlice( TElement value, string sliceExpr
}
}

private sealed class NodeArgs( in TElement value, in JsonPathSegment segment )
private sealed class NodeArgs( in TNode value, in JsonPathSegment segment )
{
public readonly TElement Value = value;
public readonly TNode Value = value;
public readonly JsonPathSegment Segment = segment;

public void Deconstruct( out TElement value, out JsonPathSegment segment )
public void Deconstruct( out TNode value, out JsonPathSegment segment )
{
value = Value;
segment = Segment;
Expand Down
12 changes: 6 additions & 6 deletions src/Hyperbee.Json/JsonTypeDescriptorRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ static JsonTypeDescriptorRegistry()
Register( new NodeTypeDescriptor() );
}

public static void Register<TElement>( ITypeDescriptor<TElement> descriptor )
public static void Register<TNode>( ITypeDescriptor<TNode> descriptor )
{
Descriptors[typeof( TElement )] = descriptor;
Descriptors[typeof( TNode )] = descriptor;
}

public static ITypeDescriptor<TElement> GetDescriptor<TElement>()
public static ITypeDescriptor<TNode> GetDescriptor<TNode>()
{
if ( Descriptors.TryGetValue( typeof( TElement ), out var descriptor ) )
if ( Descriptors.TryGetValue( typeof( TNode ), out var descriptor ) )
{
return descriptor as ITypeDescriptor<TElement>;
return descriptor as ITypeDescriptor<TNode>;
}

throw new InvalidOperationException( $"No JSON descriptors registered for type {typeof( TElement )}." );
throw new InvalidOperationException( $"No JSON descriptors registered for type {typeof( TNode )}." );
}
}

0 comments on commit 16e57a9

Please sign in to comment.