Ejemplo n.º 1
0
        public void Single_Order_By_Should_Parse_Local_Custom_Direction_Successfully()
        {
            // Arrange
            var orderBy1           = "FirstName[asc],LastName[down]";
            var directionOverride1 = new Dictionary <OrderByDirection, string>()
            {
                { OrderByDirection.Descending, "down" }
            };

            var orderBy2           = "FirstName[asc],LastName[!asc]";
            var directionOverride2 = new Dictionary <OrderByDirection, string>()
            {
                { OrderByDirection.Descending, "!asc" }
            };

            // Act
            var parser        = new OrderByParser();
            var parseResults1 = parser.Parse(orderBy1, directionOverride1);
            var parseResults2 = parser.Parse(orderBy2, directionOverride2);

            // Assert
            Assert.NotNull(parseResults1);
            Assert.NotEmpty(parseResults1);
            Assert.Equal("FirstName", parseResults1.ElementAt(0).Name);
            Assert.Equal(OrderByDirection.Ascending, parseResults1.ElementAt(0).Direction);
            Assert.Equal("LastName", parseResults1.ElementAt(1).Name);
            Assert.Equal(OrderByDirection.Descending, parseResults1.ElementAt(1).Direction);

            Assert.NotNull(parseResults2);
            Assert.NotEmpty(parseResults2);
            Assert.Equal("FirstName", parseResults2.ElementAt(0).Name);
            Assert.Equal(OrderByDirection.Ascending, parseResults2.ElementAt(0).Direction);
            Assert.Equal("LastName", parseResults2.ElementAt(1).Name);
            Assert.Equal(OrderByDirection.Descending, parseResults2.ElementAt(1).Direction);
        }
        public static IOrderedQueryable OrderBy(this IQueryable source, IEnumerable <OrderByInfo> orderByInfos, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (orderByInfos == null)
            {
                throw new ArgumentNullException(nameof(orderByInfos));
            }

            var evaluationResult = OrderByParser.Parse(orderByInfos.ToArray(), new BuildArgument
            {
                ValidProperties = validProperties,
                EvaluationType  = source.ElementType,
                PropertyMapping = propertyMapping
            });

            if (!evaluationResult.Succeeded)
            {
                throw new InvalidOperationException("The property name or direction is not valid");
            }

            return(evaluationResult.Result.Sort(source));
        }
        public static IOrderedQueryable OrderBy(this IQueryable source, string expression, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (string.IsNullOrWhiteSpace(expression))
            {
                throw new ArgumentException($"{expression} is required", nameof(expression));
            }

            var evaluationResult = OrderByParser.Parse(expression, new BuildArgument
            {
                ValidProperties = validProperties,
                EvaluationType  = source.ElementType,
                PropertyMapping = propertyMapping
            });

            if (!evaluationResult.Succeeded)
            {
                throw new InvalidOperationException("The property name or direction is not valid");
            }

            return(evaluationResult.Result.Sort(source));
        }
Ejemplo n.º 4
0
        public static EvaluationResult <IOrderedEnumerable <T> > TryOrderBy <T>(this IEnumerable <T> source, IEnumerable <OrderByInfo> orderByInfos, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (orderByInfos == null)
            {
                throw new ArgumentNullException(nameof(orderByInfos));
            }

            var evaluationResult = OrderByParser.Parse(orderByInfos.ToArray(), new BuildArgument
            {
                ValidProperties = validProperties,
                EvaluationType  = typeof(T),
                PropertyMapping = propertyMapping
            });

            if (!evaluationResult.Succeeded)
            {
                return(new EvaluationResult <IOrderedEnumerable <T> >
                {
                    InvalidProperties = evaluationResult.InvalidProperties,
                    InvalidOrderByDirections = evaluationResult.InvalidOrderByDirections,
                    Exception = evaluationResult.Exception,
                });
            }

            return(new EvaluationResult <IOrderedEnumerable <T> >
            {
                Result = evaluationResult.Result.Sort(source),
                Succeeded = true
            });
        }
Ejemplo n.º 5
0
        public static EvaluationResult <IOrderedEnumerable <T> > TryOrderBy <T>(this IEnumerable <T> source, string expression, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (string.IsNullOrWhiteSpace(expression))
            {
                throw new ArgumentException($"{expression} is required", nameof(expression));
            }

            var evaluationResult = OrderByParser.Parse(expression, new BuildArgument
            {
                ValidProperties = validProperties,
                EvaluationType  = typeof(T),
                PropertyMapping = propertyMapping
            });

            if (!evaluationResult.Succeeded)
            {
                return(new EvaluationResult <IOrderedEnumerable <T> >
                {
                    InvalidProperties = evaluationResult.InvalidProperties,
                    InvalidOrderByDirections = evaluationResult.InvalidOrderByDirections,
                    Exception = evaluationResult.Exception,
                });
            }

            return(new EvaluationResult <IOrderedEnumerable <T> >
            {
                Result = evaluationResult.Result.Sort(source),
                Succeeded = true
            });
        }
Ejemplo n.º 6
0
        public static EvaluationResult <OrderByClause> TryBuildOrderByClause(this string expression, Type type, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                throw new ArgumentException($"{nameof(expression)} is required", nameof(expression));
            }
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var arg = new BuildArgument
            {
                ValidProperties = validProperties,
                EvaluationType  = type,
                PropertyMapping = propertyMapping
            };

            return(OrderByParser.Parse(expression, arg));
        }
        internal static EvaluationResult <IOrderedQueryable> TryOrderBy(this IQueryable source, IEnumerable <OrderByInfo> orderByInfos, BuildArgument arg)
        {
            var evaluationResult = OrderByParser.Parse(orderByInfos.ToArray(), arg);

            if (!evaluationResult.Succeeded)
            {
                return(new EvaluationResult <IOrderedQueryable>
                {
                    InvalidProperties = evaluationResult.InvalidProperties,
                    InvalidOrderByDirections = evaluationResult.InvalidOrderByDirections,
                    Exception = evaluationResult.Exception,
                });
            }

            return(new EvaluationResult <IOrderedQueryable>
            {
                Result = evaluationResult.Result.Sort(source),
                Succeeded = true
            });
        }
        internal static EvaluationResult <IOrderedQueryable> TryOrderBy(this IQueryable source, string expression, BuildArgument arg)
        {
            var evaluationResult = OrderByParser.Parse(expression, arg);

            if (!evaluationResult.Succeeded)
            {
                return(new EvaluationResult <IOrderedQueryable>
                {
                    InvalidProperties = evaluationResult.InvalidProperties,
                    InvalidOrderByDirections = evaluationResult.InvalidOrderByDirections,
                    Exception = evaluationResult.Exception,
                });
            }

            return(new EvaluationResult <IOrderedQueryable>
            {
                Result = evaluationResult.Result.Sort(source),
                Succeeded = true
            });
        }
Ejemplo n.º 9
0
        public static EvaluationResult <OrderByClause> TryBuildOrderByClause(this OrderByInfo orderByInfo, Type type, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (orderByInfo is null)
            {
                throw new ArgumentNullException(nameof(orderByInfo));
            }
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var arg = new BuildArgument
            {
                ValidProperties = validProperties,
                EvaluationType  = type,
                PropertyMapping = propertyMapping
            };

            return(OrderByParser.Parse(new[] { orderByInfo }, arg));
        }
Ejemplo n.º 10
0
        public void Single_Order_By_Should_Parse_Global_Custom_Direction_Successfully()
        {
            // Arrange
            var orderBy           = "FirstName[up],LastName[desc]";
            var directionOverride = new Dictionary <OrderByDirection, string>()
            {
                { OrderByDirection.Ascending, "up" }
            };

            // Act
            var parser       = new OrderByParser(directionOverride);
            var parseResults = parser.Parse(orderBy);

            // Assert
            Assert.NotNull(parseResults);
            Assert.NotEmpty(parseResults);

            Assert.Equal("FirstName", parseResults.ElementAt(0).Name);
            Assert.Equal(OrderByDirection.Ascending, parseResults.ElementAt(0).Direction);

            Assert.Equal("LastName", parseResults.ElementAt(1).Name);
            Assert.Equal(OrderByDirection.Descending, parseResults.ElementAt(1).Direction);
        }
Ejemplo n.º 11
0
        public virtual Condition BuildCondition(Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                VariableResolver = variableResolver,
                EvaluationType   = type,
                PropertyMapping  = propertyMapping
            };

            var           error         = new ConditionBase.ErrorInfo();
            var           result        = new Condition();
            var           predicates    = new List <LambdaExpression>();
            var           exceptions    = new List <Exception>();
            OrderByClause orderByClause = null;

            if (Filters != null && Filters.Any())
            {
                var filtersResult = Filters.TryBuildPredicate(type, arg);
                if (filtersResult.Succeeded)
                {
                    predicates.Add(filtersResult.Result);
                }
                else
                {
                    if (filtersResult.Exception != null)
                    {
                        exceptions.Add(filtersResult.Exception);
                    }
                }
            }

            if (FilterGroups != null && FilterGroups.Any())
            {
                var filterGroupsResult = FilterGroups.TryBuildPredicate(type, arg);
                if (filterGroupsResult.Succeeded)
                {
                    predicates.Add(filterGroupsResult.Result);
                }
                else
                {
                    if (filterGroupsResult.Exception != null)
                    {
                        exceptions.Add(filterGroupsResult.Exception);
                    }
                }
            }

            if (!string.IsNullOrEmpty(Where))
            {
                var whereResult = ExpressionParser.Parse(Where, type, arg);
                if (whereResult.Succeeded)
                {
                    predicates.Add(whereResult.Result);
                }
                else
                {
                    if (whereResult.Exception != null)
                    {
                        exceptions.Add(whereResult.Exception);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(OrderBy))
            {
                var orderByResult = OrderByParser.Parse(OrderBy, arg);
                if (orderByResult.Succeeded)
                {
                    orderByClause = orderByResult.Result;
                }
                else
                {
                    if (orderByResult.Exception != null)
                    {
                        exceptions.Add(orderByResult.Exception);
                    }
                }
            }
            else if (OrderBys != null && OrderBys.Any())
            {
                var orderBysResult = OrderByParser.Parse(OrderBys.ToArray(), arg);
                if (orderBysResult.Succeeded)
                {
                    orderByClause = orderBysResult.Result;
                }
                else
                {
                    if (orderBysResult.Exception != null)
                    {
                        exceptions.Add(orderBysResult.Exception);
                    }
                }
            }

            var isInvalid = arg.InvalidProperties.Any() || arg.InvalidOperators.Any() || arg.InvalidVariables.Any() || exceptions.Any();

            result.IsValid = !isInvalid;
            if (isInvalid)
            {
                error.EvaluationResult = new EvaluationResultBase
                {
                    InvalidProperties        = arg.InvalidProperties,
                    InvalidValues            = arg.InvalidValues,
                    InvalidOperators         = arg.InvalidOperators,
                    InvalidVariables         = arg.InvalidVariables,
                    InvalidOrderByDirections = arg.InvalidOrderByDirections
                };
                error.Exceptions = exceptions;
                result.Error     = error;
            }
            else
            {
                result.Predicates    = predicates;
                result.OrderByClause = orderByClause;
            }

            return(result);
        }