public static Expression Translate(IQueryable query, Expression expression, Type filterType)
            {
                var visitor = new FilterVisitor(query, filterType);

                visitor.Visit(expression);
                return(visitor._whereExpression);
            }
        public void CanCreateSampleContainsFilterWithNullValue()
        {
            var filter = new FilterDescriptor("PropString", FilterOperator.Contains, null);

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => x.PropString.ToUpper().Contains(null)", expr.ToString());
        }
        public void CanCreateNoFilterExpression()
        {
            var filter = Helper.Filters();

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => True", expr.ToString());
        }
        public void CanCreateSampleEndsWithFilter()
        {
            var filter = new FilterDescriptor("PropString", FilterOperator.EndsWith, "asd");

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => x.PropString.ToUpper().EndsWith(\"ASD\")", expr.ToString());
        }
        public void CanCreateSampleEqualsFilterToIntNullable()
        {
            var filter = new FilterDescriptor("PropIntNullable", FilterOperator.IsEqualTo, 42);

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => (x.PropIntNullable = 42)", expr.ToString());
        }
        public void CanParseSimpleFilterList()
        {
            var filters = new[]
            {
                new FilterDescriptor("PropInt", FilterOperator.IsEqualTo, 42),
                new FilterDescriptor( "PropString", FilterOperator.IsEqualTo, "42")
            };

            var expr = new FilterVisitor<ClassA>().MakePredicate(filters);

            Assert.AreEqual("x => ((x.PropInt = 42) && (x.PropString.ToUpper() = \"42\"))", expr.ToString());
        }
        public void CanParseSimpleFilterListWithCompositeParts()
        {
            var filters = Helper.Filters(
                Helper.Filter("PropInt", FilterOperator.IsEqualTo, 42),
                Helper.Filters(FilterCompositionLogicalOperator.Or,
                    Helper.Filter("PropString", FilterOperator.IsEqualTo, "42"),
                    Helper.Filter("PropB.PropDecimalNullable", FilterOperator.IsEqualTo, 42.0)));

            var expr = new FilterVisitor<ClassA>().MakePredicate(filters);

            Assert.AreEqual("x => ((x.PropInt = 42) && ((x.PropString.ToUpper() = \"42\") || (x.PropB.PropDecimalNullable = 42)))", expr.ToString());
        }
Example #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();
        }
Example #9
0
        private Func <MockItem, bool> BuildQuery(string text)
        {
            var inputStream       = new AntlrInputStream(text);
            var filterLexer       = new FilterLexer(inputStream);
            var commonTokenStream = new CommonTokenStream(filterLexer);
            var filterParser      = new FilterParser(commonTokenStream);
            var context           = filterParser.query();
            var weekParser        = new WeekDatesBehavior();
            var dateParser        = new DateParser(new List <IDateParserBehavior>()
            {
                weekParser
            }, new DateParserConfig());
            var visitor    = new FilterVisitor <MockItem>(dateParser);
            var expression = visitor.Visit(context);

            return(expression.Compile());
        }
Example #10
0
        public static MethodCallExpression CreateCountExpression(Expression expression)
        {
            var filterVisitor = new FilterVisitor();

            filterVisitor.Visit(expression);

            Expression whereExpression = filterVisitor.WhereExpression;

            if (whereExpression == null)
            {
                whereExpression = filterVisitor.Source;
            }

            Type       sourceType      = OeExpressionHelper.GetCollectionItemType(whereExpression.Type);
            MethodInfo countMethodInfo = OeMethodInfoHelper.GetCountMethodInfo(sourceType);

            return(Expression.Call(countMethodInfo, whereExpression));
        }
        public IActionResult Get(ODataQueryOptions <ExampleModel> options)
        {
            if (options?.Filter?.FilterClause?.Expression == null)
            {
                return(Ok(_data));
            }

            var visitor = new FilterVisitor <ExampleAggregate, ExampleColumn>(GetField, ColumnToProperty);

            try
            {
                var expression = options.Filter.FilterClause.Expression.Accept(visitor);
                var pred       = Expression.Lambda <Func <ExampleAggregate, bool> >(expression, visitor.GetParameterExpression());
                var result     = _data.AsQueryable().Where(pred);
                //Mapping to a model to expose
                return(Ok(result));
            }
            catch (Exception e)
            {
                return(BadRequest(e));
            }
        }
Example #12
0
        public MethodCallExpression CreateCountExpression(Expression source)
        {
            if (EntryFactory == null)
            {
                return(null);
            }

            Type sourceType    = EdmModel.GetClrType(EntryFactory.EntitySet);
            var  filterVisitor = new FilterVisitor(sourceType);

            filterVisitor.Visit(source);

            Expression whereExpression = filterVisitor.WhereExpression;

            if (whereExpression == null)
            {
                whereExpression = filterVisitor.Source;
            }

            MethodInfo countMethodInfo = OeMethodInfoHelper.GetCountMethodInfo(sourceType);

            return(Expression.Call(countMethodInfo, whereExpression));
        }
Example #13
0
        public MethodCallExpression CreateCountExpression(Expression source)
        {
            if (EntryFactory == null)
            {
                throw new InvalidOperationException("Cannot get count expression for scalar result expression");
            }

            Type sourceType    = EdmModel.GetClrType(EntryFactory.EntitySet);
            var  filterVisitor = new FilterVisitor(sourceType);

            filterVisitor.Visit(source);

            Expression?whereExpression = filterVisitor.WhereExpression;

            if (whereExpression == null)
            {
                whereExpression = filterVisitor.Source;
            }

            MethodInfo countMethodInfo = OeMethodInfoHelper.GetCountMethodInfo(sourceType);

            return(Expression.Call(countMethodInfo, whereExpression));
        }
        public Expression CreateCountExpression(IQueryable query, Expression expression)
        {
            Type       filterType       = EntryFactory == null ? query.ElementType : EdmModel.GetClrType(EntryFactory.EntityType);
            Expression filterExpression = ODataUri.Filter == null ? query.Expression : FilterVisitor.Translate(query, expression, filterType);

            Type       sourceType      = OeExpressionHelper.GetCollectionItemType(filterExpression.Type);
            MethodInfo countMethodInfo = OeMethodInfoHelper.GetCountMethodInfo(sourceType);

            return(Expression.Call(countMethodInfo, filterExpression));
        }
Example #15
0
        public SymbolVisitorAdapter(YamlModelGenerator generator, SyntaxLanguage language, Compilation compilation, ExtractMetadataOptions options)
        {
            _generator                 = generator;
            Language                   = language;
            _currentCompilation        = compilation;
            _currentCompilationRef     = compilation.ToMetadataReference();
            _preserveRawInlineComments = options.PreserveRawInlineComments;
            var configFilterRule = ConfigFilterRule.LoadWithDefaults(options.FilterConfigFile);
            var filterVisitor    = options.DisableDefaultFilter ? (IFilterVisitor) new AllMemberFilterVisitor() : new DefaultFilterVisitor();

            FilterVisitor     = filterVisitor.WithConfig(configFilterRule).WithCache();
            _extensionMethods = options.RoslynExtensionMethods != null?options.RoslynExtensionMethods.ToDictionary(p => p.Key, p => p.Value.Where(e => FilterVisitor.CanVisitApi(e))) : new Dictionary <Compilation, IEnumerable <IMethodSymbol> >();

            _codeSourceBasePath = options.CodeSourceBasePath;
        }
        public void CanCreateSampleEqualsFilterToIntWithQueryableVersion()
        {
            var filter = new FilterDescriptor("PropInt", FilterOperator.IsEqualTo, 42);
            var param = Expression.Parameter(typeof(IQueryable<ClassA>), "q");
            var expr = new FilterVisitor<ClassA>().Visit(param, filter);

            Assert.AreEqual("q.Where(x => (x.PropInt = 42))", expr.ToString());
        }
Example #17
0
 public SymbolVisitorAdapter(YamlModelGenerator generator, SyntaxLanguage language, Compilation compilation, bool preserveRawInlineComments = false, string filterConfigFile = null, IReadOnlyDictionary <Compilation, IEnumerable <IMethodSymbol> > extensionMethods = null)
 {
     _generator                 = generator;
     Language                   = language;
     _currentCompilation        = compilation;
     _currentCompilationRef     = compilation.ToMetadataReference();
     _preserveRawInlineComments = preserveRawInlineComments;
     FilterVisitor              = string.IsNullOrEmpty(filterConfigFile) ? new DefaultFilterVisitor() : new DefaultFilterVisitor().WithConfig(filterConfigFile).WithCache();
     _extensionMethods          = extensionMethods != null?extensionMethods.ToDictionary(p => p.Key, p => p.Value.Where(e => FilterVisitor.CanVisitApi(e))) : new Dictionary <Compilation, IEnumerable <IMethodSymbol> >();
 }
        public void CanCreateSampleGreaterFilterToClassBWithDoubleValue()
        {
            var filter = new FilterDescriptor("PropB.PropDecimalNullable", FilterOperator.IsGreaterThan, 42.0);

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => (x.PropB.PropDecimalNullable > 42)", expr.ToString());
        }
        public void CanCreateSampleIsContainedInFilterWithIntList()
        {
            var list = new[] { 1, 2, 3 };
            var filter = new FilterDescriptor("PropInt", FilterOperator.IsContainedIn, list);

            Expression<Func<int, bool>> ex = x => list.Contains(x);

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => value(System.Int32[]).Contains(x.PropInt)", expr.ToString());
        }
        public void CanCreateSampleLessFilterToClassBDateTimeNullable()
        {
            var now = DateTime.Now;
            var filter = new FilterDescriptor("PropB.PropDateTimeNullable", FilterOperator.IsLessThan, now);

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual(string.Format("x => (x.PropB.PropDateTimeNullable < {0})", now.ToString()), expr.ToString());
        }
        public void CanCreateSampleNotEqualsToFilter()
        {
            var filter = new FilterDescriptor("PropString", FilterOperator.IsNotEqualTo, "asd");

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => (x.PropString.ToUpper() != \"ASD\")", expr.ToString());
        }
        public void CanCreateNullFilterExpression()
        {
            var expr = new FilterVisitor<ClassA>().MakePredicate(null);

            Assert.AreEqual("x => True", expr.ToString());
        }