Example #1
0
        private bool IsInstanceOfTypeInternal(IValueNode literal)
        {
            if (literal is NullValueNode)
            {
                return(true);
            }

            if (_inputType.IsInstanceOfType(literal))
            {
                return(true);
            }

            if (literal is ListValueNode listValueLiteral)
            {
                foreach (IValueNode element in listValueLiteral.Items)
                {
                    if (!_inputType.IsInstanceOfType(element))
                    {
                        return(false);
                    }
                }

                return(true);
            }
            return(false);
        }
Example #2
0
        public bool IsInstanceOfType(IValueNode literal)
        {
            if (literal == null)
            {
                throw new ArgumentNullException(nameof(literal));
            }

            if (_isInputType)
            {
                if (literal is NullValueNode)
                {
                    return(true);
                }

                if (literal is ListValueNode listValueLiteral)
                {
                    if (listValueLiteral.Items.Any())
                    {
                        IValueNode value = listValueLiteral.Items.First();
                        if (!_inputType.IsInstanceOfType(value))
                        {
                            return(!ElementType.IsNonNullType() &&
                                   value is NullValueNode);
                        }

                        return(true);
                    }
                    return(true);
                }
                return(false);
            }

            throw new InvalidOperationException(
                      "The specified type is not an input type.");
        }
        public static bool EndsWith(
            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(string) &&
                type.IsInstanceOfType(value))
            {
                Expression property = GetProperty(operation, context);

                result = FilterExpressionBuilder.EndsWith(property, parsedValue);
                return(true);
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        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);
        }
Example #5
0
        public bool TryHandle(
            FilterOperation operation,
            IInputType type,
            IValueNode value,
            Expression instance,
            ITypeConversion converter,
            out Expression expression)
        {
            if (operation.Type == typeof(bool) &&
                type.IsInstanceOfType(value))
            {
                MemberExpression property =
                    Expression.Property(instance, 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.Not(
                        FilterExpressionBuilder.Equals(
                            property, parserValue)
                        );
                    return(true);
                }
            }

            expression = null;
            return(false);
        }
Example #6
0
        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);
        }
Example #7
0
        public bool TryHandle(
            FilterOperation operation,
            IInputType type,
            IValueNode value,
            Expression instance,
            ITypeConversion converter,
            out Expression expression)
        {
            if (operation.Type == typeof(string) &&
                type.IsInstanceOfType(value))
            {
                object parsedValue = type.ParseLiteral(value);

                Expression property = instance;

                if (!operation.IsSimpleArrayType())
                {
                    property = Expression.Property(instance, operation.Property);
                }


                return(TryCreateExpression(
                           operation,
                           property,
                           parsedValue,
                           out expression));
            }

            expression = null;
            return(false);
        }
        public bool TryHandle(
            FilterOperation operation,
            IInputType type,
            IValueNode value,
            Expression instance,
            ITypeConversion converter,
            out Expression expression)
        {
            if (operation.Type == typeof(IComparable) &&
                type.IsInstanceOfType(value))
            {
                MemberExpression property =
                    Expression.Property(instance, operation.Property);
                var parsedValue = type.ParseLiteral(value);

                if (operation.Property.PropertyType
                    .IsInstanceOfType(parsedValue))
                {
                    parsedValue = converter.Convert(
                        typeof(object),
                        operation.Property.PropertyType,
                        parsedValue);
                }

                return(TryCreateExpression(
                           operation,
                           property,
                           parsedValue,
                           out expression));
            }

            expression = null;
            return(false);
        }
Example #9
0
        protected sealed override bool IsInstanceOfType(IValueNode literal)
        {
            if (literal is NullValueNode)
            {
                return(true);
            }

            IInputType inputType = InnerInputType !;

            if (_namedType !.IsInstanceOfType(literal))
            {
                return(true);
            }

            if (_isNestedList)
            {
                if (literal is ListValueNode listValueLiteral)
                {
                    foreach (IValueNode element in listValueLiteral.Items)
                    {
                        if (element.Kind != SyntaxKind.ListValue ||
                            !inputType.IsInstanceOfType(element))
                        {
                            return(false);
                        }
                    }

                    return(true);
                }
            }
            else
            {
                if (literal is ListValueNode listValueLiteral)
                {
                    foreach (IValueNode element in listValueLiteral.Items)
                    {
                        if (!inputType.IsInstanceOfType(element))
                        {
                            return(false);
                        }
                    }

                    return(true);
                }
            }
            return(false);
        }
Example #10
0
        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 bool TryHandle(
            FilterOperation operation,
            IInputType type,
            IValueNode value,
            Expression instance,
            ITypeConversion converter,
            out Expression expression)
        {
            if (operation.Type == typeof(IComparable) &&
                type.IsInstanceOfType(value))
            {
                MemberExpression property =
                    Expression.Property(instance, operation.Property);

                switch (operation.Kind)
                {
                case FilterOperationKind.In:
                    expression = FilterExpressionBuilder.In(
                        property,
                        operation.Property.PropertyType,
                        ParseValue());
                    return(true);

                case 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().ToClrType();

                if (operation.Property.PropertyType != elementType)
                {
                    Type listType = typeof(List <>).MakeGenericType(
                        operation.Property.PropertyType);

                    parsedValue = converter.Convert(
                        typeof(object),
                        listType,
                        parsedValue);
                }

                return(parsedValue);
            }
        }
Example #12
0
        public bool IsInstanceOfType(IValueNode literal)
        {
            if (_isInputType)
            {
                if (literal is NullValueNode)
                {
                    return(false);
                }

                return(_inputType.IsInstanceOfType(literal));
            }

            throw new InvalidOperationException(
                      TypeResources.NonNullType_NotAnInputType);
        }
Example #13
0
        public bool IsInstanceOfType(IValueNode literal)
        {
            if (_isInputType)
            {
                if (literal is NullValueNode)
                {
                    return(false);
                }

                return(_inputType.IsInstanceOfType(literal));
            }

            throw new InvalidOperationException(
                      "The specified type is not an input type.");
        }
Example #14
0
        public bool TryHandle(
            FilterOperation operation,
            IInputType type,
            IValueNode value,
            Expression instance,
            ITypeConversion converter,
            bool inMemory,
            out Expression expression)
        {
            if (operation.Kind == FilterOperationKind.ArrayAny &&
                type.IsInstanceOfType(value) &&
                type.ParseLiteral(value) is bool parsedValue)
            {
                MemberExpression property =
                    Expression.Property(instance, operation.Property);
                Type propertType = operation.Type;

                if (operation.TryGetSimpleFilterBaseType(out Type baseType))
                {
                    propertType = baseType;
                }

                if (parsedValue)
                {
                    expression = FilterExpressionBuilder.Any(
                        propertType,
                        property);
                }
                else
                {
                    expression = FilterExpressionBuilder.Not(
                        FilterExpressionBuilder.Any(
                            propertType,
                            property));
                }
                if (inMemory)
                {
                    expression = FilterExpressionBuilder.NotNullAndAlso(property, expression);
                }
                return(true);
            }
            expression = null;
            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);
        }
Example #16
0
        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);
            }
        }
Example #17
0
        public bool TryHandle(
            FilterOperation operation,
            IInputType type,
            IValueNode value,
            Expression instance,
            ITypeConversion converter,
            bool inMemory,
            out Expression expression)
        {
            if (operation.Type == typeof(string) && type.IsInstanceOfType(value))
            {
                Expression property = instance;

                if (!operation.IsSimpleArrayType())
                {
                    property = Expression.Property(instance, operation.Property);
                }

                var parsedValue = type.ParseLiteral(value);

                switch (operation.Kind)
                {
                case FilterOperationKind.In:
                    expression = FilterExpressionBuilder.In(
                        property,
                        operation.Property.PropertyType,
                        parsedValue);
                    return(true);

                case FilterOperationKind.NotIn:
                    expression = FilterExpressionBuilder.Not(
                        FilterExpressionBuilder.In(
                            property,
                            operation.Property.PropertyType,
                            parsedValue));
                    return(true);
                }
            }

            expression = null;
            return(false);
        }
        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);
        }
Example #19
0
        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 TryHandle(
            FilterOperation operation,
            IInputType type,
            IValueNode value,
            Expression instance,
            ITypeConversion converter,
            out Expression expression)
        {
            if (operation.Kind == FilterOperationKind.ArrayAny &&
                type.IsInstanceOfType(value) &&
                type.ParseLiteral(value) is bool parsedValue)
            {
                Expression property = instance;

                if (!operation.IsSimpleArrayType())
                {
                    property = Expression.Property(instance, operation.Property);
                }

                if (parsedValue)
                {
                    expression = FilterExpressionBuilder.Any(
                        operation.Type,
                        property);
                }
                else
                {
                    expression = FilterExpressionBuilder.Not(
                        FilterExpressionBuilder.Any(
                            operation.Type,
                            property));
                }
                return(true);
            }
            expression = null;
            return(false);
        }
        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();
            }
        }