Пример #1
0
    public static bool IsValid <T>(this IGridifyFiltering filtering, IGridifyMapper <T>?mapper = null)
    {
        if (string.IsNullOrWhiteSpace(filtering.Filter))
        {
            return(true);
        }
        try
        {
            var parser     = new Parser(filtering.Filter !, GridifyGlobalConfiguration.CustomOperators.Operators);
            var syntaxTree = parser.Parse();
            if (syntaxTree.Diagnostics.Any())
            {
                return(false);
            }

            var fieldExpressions = syntaxTree.Root.Descendants()
                                   .Where(q => q.Kind is SyntaxKind.FieldExpression)
                                   .Cast <FieldExpressionSyntax>().ToList();

            mapper ??= new GridifyMapper <T>(true);

            if (fieldExpressions.Any(field => !mapper.HasMap(field.FieldToken.Text)))
            {
                return(false);
            }
        }
        catch (Exception)
        {
            return(false);
        }

        return(true);
    }
Пример #2
0
    public static Expression <Func <T, bool> > GetFilteringExpression <T>(this IGridifyFiltering gridifyFiltering, IGridifyMapper <T>?mapper = null)
    {
        if (string.IsNullOrWhiteSpace(gridifyFiltering.Filter))
        {
            throw new GridifyQueryException("Filter is not defined");
        }

        var syntaxTree = SyntaxTree.Parse(gridifyFiltering.Filter !, GridifyGlobalConfiguration.CustomOperators.Operators);

        if (syntaxTree.Diagnostics.Any())
        {
            throw new GridifyFilteringException(syntaxTree.Diagnostics.Last() !);
        }

        mapper = mapper.FixMapper(syntaxTree);
        var(queryExpression, _) = ExpressionToQueryConvertor.GenerateQuery(syntaxTree.Root, mapper);
        if (queryExpression == null)
        {
            throw new GridifyQueryException("Can not create expression with current data");
        }
        return(queryExpression);
    }