public bool TryHandle( FilterOperation operation, IInputType type, IValueNode value, IQueryableFilterVisitorContext context, [NotNullWhen(true)] out Expression?expression) { if (operation.Type == typeof(bool) && type.IsInstanceOfType(value)) { Expression property = context.GetInstance(); if (!operation.IsSimpleArrayType()) { property = Expression.Property(context.GetInstance(), operation.Property); } object parserValue = type.ParseLiteral(value); switch (operation.Kind) { case FilterOperationKind.Equals: expression = FilterExpressionBuilder.Equals( property, parserValue); return(true); case FilterOperationKind.NotEquals: expression = FilterExpressionBuilder.NotEquals( property, parserValue); return(true); } } expression = null; return(false); }
public bool TryHandle( FilterOperation operation, IInputType type, IValueNode value, IQueryableFilterVisitorContext context, out Expression expression) { if (operation.Type == typeof(string) && type.IsInstanceOfType(value)) { object parsedValue = type.ParseLiteral(value); Expression property = context.GetInstance(); if (!operation.IsSimpleArrayType()) { property = Expression.Property( context.GetInstance(), operation.Property); } return(TryCreateExpression( operation, property, parsedValue, out expression)); } expression = null; return(false); }
public bool TryHandle( FilterOperation operation, IInputType type, IValueNode value, IQueryableFilterVisitorContext context, [NotNullWhen(true)] out Expression?expression) { if (operation.Type == typeof(IComparable) && type.IsInstanceOfType(value)) { Expression property = context.GetInstance(); if (!operation.IsSimpleArrayType()) { property = Expression.Property(context.GetInstance(), operation.Property); } if (operation.Kind == FilterOperationKind.In) { expression = FilterExpressionBuilder.In( property, operation.Property.PropertyType, ParseValue()); return(true); } if (operation.Kind == FilterOperationKind.NotIn) { expression = FilterExpressionBuilder.Not( FilterExpressionBuilder.In( property, operation.Property.PropertyType, ParseValue())); return(true); } } expression = null; return(false); object ParseValue() { var parsedValue = type.ParseLiteral(value); Type elementType = type.ElementType().ToRuntimeType(); if (operation.Property.PropertyType != elementType) { Type listType = typeof(List <>).MakeGenericType( operation.Property.PropertyType); parsedValue = context.TypeConverter.Convert( typeof(object), listType, parsedValue); } return(parsedValue); } }
public void Leave( FilterOperationField field, ObjectFieldNode node, IQueryableFilterVisitorContext context) { if (field.Operation.Kind == FilterOperationKind.ArraySome || field.Operation.Kind == FilterOperationKind.ArrayNone || field.Operation.Kind == FilterOperationKind.ArrayAll) { QueryableClosure nestedClosure = context.PopClosure(); LambdaExpression lambda = nestedClosure.CreateLambda(); Type closureType = GetTypeFor(field.Operation); Expression expression; switch (field.Operation.Kind) { case FilterOperationKind.ArraySome: expression = FilterExpressionBuilder.Any( closureType, context.GetInstance(), lambda ); break; case FilterOperationKind.ArrayNone: expression = FilterExpressionBuilder.Not( FilterExpressionBuilder.Any( closureType, context.GetInstance(), lambda ) ); break; case FilterOperationKind.ArrayAll: expression = FilterExpressionBuilder.All( closureType, context.GetInstance(), lambda ); break; default: throw new NotSupportedException(); } if (context.InMemory) { expression = FilterExpressionBuilder.NotNullAndAlso( context.GetInstance(), expression); } context.GetLevel().Enqueue(expression); context.PopInstance(); } }
private static Expression GetProperty( FilterOperation operation, IQueryableFilterVisitorContext context) { Expression property = context.GetInstance(); if (!operation.IsSimpleArrayType()) { property = Expression.Property(context.GetInstance(), operation.Property); } return(property); }
public bool Enter( FilterOperationField field, ObjectFieldNode node, IQueryableFilterVisitorContext context, out ISyntaxVisitorAction action) { if (field.Operation.Kind == FilterOperationKind.ArraySome || field.Operation.Kind == FilterOperationKind.ArrayNone || field.Operation.Kind == FilterOperationKind.ArrayAll) { MemberExpression nestedProperty = Expression.Property( context.GetInstance(), field.Operation.Property); context.PushInstance(nestedProperty); Type closureType = GetTypeFor(field.Operation); context.AddClosure(closureType); action = SyntaxVisitor.Continue; return(true); } action = SyntaxVisitor.Skip; return(false); }
public static bool Enter( FilterOperationField field, ObjectFieldNode node, IQueryableFilterVisitorContext context, [NotNullWhen(true)] out ISyntaxVisitorAction?action) { if (node.Value.IsNull()) { if (field.Operation.IsNullable) { MemberExpression nestedProperty = Expression.Property( context.GetInstance(), field.Operation.Property); Expression expression = FilterExpressionBuilder.Equals(nestedProperty, null !); context.GetLevel().Enqueue(expression); } else { context.ReportError( ErrorHelper.CreateNonNullError(field, node, context)); } action = SyntaxVisitor.Skip; return(true); } if (field.Operation.Kind == FilterOperationKind.Object) { MemberExpression nestedProperty = Expression.Property( context.GetInstance(), field.Operation.Property); context.PushInstance(nestedProperty); action = SyntaxVisitor.Continue; return(true); } action = null; return(false); }
public bool TryHandle( FilterOperation operation, IInputType type, IValueNode value, IQueryableFilterVisitorContext context, [NotNullWhen(true)] out Expression?expression) { if (operation.Type == typeof(IComparable) && type.IsInstanceOfType(value)) { Expression property = context.GetInstance(); if (!operation.IsSimpleArrayType()) { property = Expression.Property(context.GetInstance(), operation.Property); } return(TryCreateExpression( operation, property, ParseValue, out expression)); } expression = null; return(false); object ParseValue() { var parsedValue = type.ParseLiteral(value); if (!operation.Property.PropertyType.IsInstanceOfType(parsedValue)) { parsedValue = context.TypeConverter.Convert( typeof(object), operation.Property.PropertyType, parsedValue); } return(parsedValue); } }
public bool TryHandle( FilterOperation operation, IInputType type, IValueNode value, IQueryableFilterVisitorContext context, [NotNullWhen(true)] out Expression?expression) { if (operation.Type == typeof(string) && type.IsInstanceOfType(value)) { Expression property = context.GetInstance(); if (!operation.IsSimpleArrayType()) { property = Expression.Property( context.GetInstance(), operation.Property); } object?parsedValue = type.ParseLiteral(value); if (operation.Kind == FilterOperationKind.In) { expression = FilterExpressionBuilder.In( property, operation.Property.PropertyType, parsedValue); return(true); } if (operation.Kind == FilterOperationKind.NotIn) { expression = FilterExpressionBuilder.Not( FilterExpressionBuilder.In( property, operation.Property.PropertyType, parsedValue)); return(true); } } expression = null; return(false); }
public static bool NotEquals( FilterOperation operation, IInputType type, IValueNode value, IQueryableFilterVisitorContext context, [NotNullWhen(true)] out Expression?result) { object parsedValue = type.ParseLiteral(value); if (parsedValue == null) { context.ReportError( ErrorHelper.CreateNonNullError(operation, type, value, context)); result = null; return(false); } if (operation.Type == typeof(bool) && type.IsInstanceOfType(value) && parsedValue is bool) { Expression property = context.GetInstance(); if (!operation.IsSimpleArrayType()) { property = Expression.Property(context.GetInstance(), operation.Property); } result = FilterExpressionBuilder.NotEquals(property, parsedValue); return(true); } else { throw new InvalidOperationException(); } }
public bool Enter( FilterOperationField field, ObjectFieldNode node, IQueryableFilterVisitorContext context, out ISyntaxVisitorAction action) { if (field.Operation.Kind == FilterOperationKind.Object) { MemberExpression nestedProperty = Expression.Property( context.GetInstance(), field.Operation.Property); context.PushInstance(nestedProperty); action = SyntaxVisitor.Continue; return(true); } action = SyntaxVisitor.SkipAndLeave; return(false); }
public bool TryHandle( FilterOperation operation, IInputType type, IValueNode value, IQueryableFilterVisitorContext context, [NotNullWhen(true)] out Expression?expression) { if (operation.Kind == FilterOperationKind.ArrayAny && type.IsInstanceOfType(value) && context.InputParser.ParseLiteral(value, type) is bool parsedValue) { MemberExpression property = Expression.Property(context.GetInstance(), operation.Property); Type propertyType = operation.Type; if (operation.TryGetSimpleFilterBaseType(out Type? baseType)) { propertyType = baseType; } if (parsedValue) { expression = FilterExpressionBuilder.Any( propertyType, property); } else { expression = FilterExpressionBuilder.Not( FilterExpressionBuilder.Any( propertyType, property)); } if (context.InMemory) { expression = FilterExpressionBuilder.NotNullAndAlso(property, expression); } return(true); } expression = null; return(false); }
public static bool Enter( FilterOperationField field, ObjectFieldNode node, IQueryableFilterVisitorContext context, [NotNullWhen(true)] out ISyntaxVisitorAction?action) { if (field.Operation.Kind == FilterOperationKind.ArraySome || field.Operation.Kind == FilterOperationKind.ArrayNone || field.Operation.Kind == FilterOperationKind.ArrayAll) { if (!field.Operation.IsNullable && node.Value.IsNull()) { context.ReportError( ErrorHelper.CreateNonNullError(field, node, context)); action = SyntaxVisitor.Skip; return(true); } MemberExpression nestedProperty = Expression.Property( context.GetInstance(), field.Operation.Property); context.PushInstance(nestedProperty); Type closureType = GetTypeFor(field.Operation); if (node.Value.IsNull()) { context.AddIsNullClosure(closureType); action = SyntaxVisitor.SkipAndLeave; } else { context.AddClosure(closureType); action = SyntaxVisitor.Continue; } return(true); } action = null; return(false); }
public static void Leave( FilterOperationField field, ObjectFieldNode node, IQueryableFilterVisitorContext context) { if (field.Operation.Kind == FilterOperationKind.Object) { // Deque last expression to prefix with nullcheck Expression condition = context.GetLevel().Dequeue(); Expression property = context.GetInstance(); // wrap last expression only if in memory if (context.InMemory) { condition = FilterExpressionBuilder.NotNullAndAlso( property, condition); } context.GetLevel().Enqueue(condition); context.PopInstance(); } }
public static bool ArrayAny( FilterOperation operation, IInputType type, IValueNode value, IQueryableFilterVisitorContext context, [NotNullWhen(true)] out Expression?result) { object parsedValue = type.ParseLiteral(value); if (parsedValue == null) { context.ReportError( ErrorHelper.CreateNonNullError(operation, type, value, context)); result = null; return(false); } if (operation.Kind == FilterOperationKind.ArrayAny && type.IsInstanceOfType(value) && parsedValue is bool parsedBool) { MemberExpression property = Expression.Property(context.GetInstance(), operation.Property); Type propertType = operation.Type; if (operation.TryGetSimpleFilterBaseType(out Type? baseType)) { propertType = baseType; } Expression expression; if (parsedBool) { expression = FilterExpressionBuilder.Any( propertType, property); } else { expression = FilterExpressionBuilder.Not( FilterExpressionBuilder.Any( propertType, property)); } if (context.InMemory) { expression = FilterExpressionBuilder.NotNullAndAlso(property, expression); } result = expression; return(true); } else { throw new InvalidOperationException(); } }