public override Expression HandleOperation(
            QueryableFilterContext context,
            IFilterOperationField field,
            IValueNode value,
            object parsedValue)
        {
            if (context.RuntimeTypes.Count > 0 &&
                context.RuntimeTypes.Peek().TypeArguments is { Count : > 0 } args&&
                parsedValue is bool parsedBool)
            {
                Expression?property = context.GetInstance();

                Expression expression;
                if (parsedBool)
                {
                    expression = FilterExpressionBuilder.Any(args[0].Source, property);
                }
                else
                {
                    expression = FilterExpressionBuilder.Not(
                        FilterExpressionBuilder.Any(args[0].Source, property));
                }

                if (context.InMemory)
                {
                    expression = FilterExpressionBuilder.NotNullAndAlso(property, expression);
                }

                return(expression);
            }
            throw new InvalidOperationException();
        }
        protected object?ParseValue(
            IValueNode node,
            object?parsedValue,
            IType type,
            QueryableFilterContext context)
        {
            if (parsedValue is null)
            {
                return(parsedValue);
            }

            Type returnType = context.RuntimeTypes.Peek().Source;

            if (type.IsListType())
            {
                Type elementType = type.ElementType().ToRuntimeType();

                if (returnType != elementType)
                {
                    Type listType = typeof(List <>).MakeGenericType(returnType);
                    parsedValue = TypeConverter.Convert(typeof(object), listType, parsedValue) ??
                                  throw ThrowHelper.FilterConvention_CouldNotConvertValue(node);
                }

                return(parsedValue);
            }

            if (!returnType.IsInstanceOfType(parsedValue))
            {
                parsedValue = TypeConverter.Convert(typeof(object), returnType, parsedValue) ??
                              throw ThrowHelper.FilterConvention_CouldNotConvertValue(node);
            }

            return(parsedValue);
        }
예제 #3
0
 protected override Expression HandleListOperation(
     QueryableFilterContext context,
     IFilterField field,
     ObjectFieldNode node,
     Type closureType,
     LambdaExpression lambda) =>
 FilterExpressionBuilder.All(closureType, context.GetInstance(), lambda);
예제 #4
0
        public override Expression HandleOperation(
            QueryableFilterContext context,
            IFilterOperationField field,
            IValueNode value,
            object?parsedValue)
        {
            Expression property = context.GetInstance();

            return(FilterExpressionBuilder.Contains(property, parsedValue));
        }
        public override Expression HandleOperation(
            QueryableFilterContext context,
            IFilterOperationField field,
            IValueNode value,
            object?parsedValue)
        {
            Expression property = context.GetInstance();

            parsedValue = ParseValue(value, parsedValue, field.Type, context);
            return(FilterExpressionBuilder.NotEquals(property, parsedValue));
        }
        public override Expression HandleOperation(
            QueryableFilterContext context,
            IFilterOperationField field,
            IValueNode value,
            object parsedValue)
        {
            Expression property = context.GetInstance();

            return(FilterExpressionBuilder.In(
                       property,
                       context.RuntimeTypes.Peek().Source,
                       parsedValue));
        }
        public static FilterScope <Expression> AddIsNullClosure(
            this QueryableFilterContext context)
        {
            var closure = new QueryableScope(
                context.RuntimeTypes.Peek(), "_s" + context.Scopes.Count, false);

            context.Scopes.Push(closure);

            context.GetLevel().Enqueue(
                FilterExpressionBuilder.Equals(context.GetClosure().Parameter, null));

            return(closure);
        }
예제 #8
0
        public Func <T, bool> Build <T>(IValueNode filter)
        {
            var visitorContext = new QueryableFilterContext(_inputType, true);
            var visitor        = new FilterVisitor <QueryableFilterContext, Expression>(
                new QueryableCombinator());

            visitor.Visit(filter, visitorContext);

            if (visitorContext.TryCreateLambda(out Expression <Func <T, bool> >?where))
            {
                return(where.Compile());
            }

            throw new InvalidOperationException();
        }
        /// <summary>
        /// Tries to build the a typed expression based on the items that are stored on the scope
        /// </summary>
        /// <param name="context">the context</param>
        /// <param name="expression">The query that was build</param>
        /// <typeparam name="T">The generic type of the expression</typeparam>
        /// <returns>True in case the query has been build successfully, otherwise false</returns>
        public static bool TryCreateLambda <T>(
            this QueryableFilterContext context,
            [NotNullWhen(true)] out Expression <T>?expression)
        {
            if (context.Scopes.TryPeekElement(out FilterScope <Expression>?scope) &&
                scope is QueryableScope closure &&
                closure.Level.TryPeekElement(out Queue <Expression>?levels) &&
                levels.TryPeekElement(out Expression? level))
            {
                expression = Expression.Lambda <T>(level, closure.Parameter);
                return(true);
            }

            expression = null;
            return(false);
        }
        public override Expression HandleOperation(
            QueryableFilterContext context,
            IFilterOperationField field,
            IValueNode value,
            object?parsedValue)
        {
            Expression property = context.GetInstance();

            parsedValue = ParseValue(value, parsedValue, field.Type, context);

            if (parsedValue is null)
            {
                throw new InvalidOperationException();
            }

            return(FilterExpressionBuilder.GreaterThan(property, parsedValue));
        }
        public static bool TryCreateLambda(
            this QueryableFilterContext context,
            [NotNullWhen(true)] out LambdaExpression?expression)
        {
            if (context.Scopes.TryPeek(out FilterScope <Expression>?scope) &&
                scope is QueryableScope closure)
            {
                expression = null;

                if (closure.Level.Peek().Count == 0)
                {
                    return(false);
                }

                expression = Expression.Lambda(closure.Level.Peek().Peek(), closure.Parameter);
                return(true);
            }

            expression = null;
            return(false);
        }
 /// <summary>
 /// Reads the current closure from the context
 /// </summary>
 /// <param name="context">The context</param>
 /// <returns>The current closure</returns>
 public static QueryableScope GetClosure(
     this QueryableFilterContext context) =>
 (QueryableScope)context.GetScope();