Пример #1
0
        public void FullCheck()
        {
            var visitor = new WebRockExpressionConverterVisitor();
            Expression <Func <User, bool> > expression = user => !user.IsActive &&
                                                         user.Credentials.Token.CurrentTokenNumber.Equals("6") &&
                                                         user.RegisterDate.ToShortDateString() == "12/10/2012";

            visitor.Visit(expression);

            Assert.AreEqual(5, visitor.Lefts.Count);
            Assert.AreEqual(2, visitor.Rights.Count);
            Assert.AreEqual(4, visitor.LastLevelNodeTypes.Count);
            Assert.AreEqual(2, visitor.ConcatNodeTypes.Count);

            Assert.AreEqual("user.IsActive", visitor.Lefts[0].ToString());
            Assert.AreEqual("user.Credentials.Token.CurrentTokenNumber", visitor.Lefts[1].ToString());
            Assert.AreEqual("user.Credentials.Token", visitor.Lefts[2].ToString());
            Assert.AreEqual("user.Credentials", visitor.Lefts[3].ToString());
            Assert.AreEqual("user.RegisterDate", visitor.Lefts[4].ToString());

            Assert.AreEqual("\"6\"", visitor.Rights[0].ToString());
            Assert.AreEqual("\"12/10/2012\"", visitor.Rights[1].ToString());

            Assert.AreEqual(ExpressionType.Not, visitor.LastLevelNodeTypes[0].ValueOfExpressionType);
            Assert.AreEqual(ExpressionType.Call, visitor.LastLevelNodeTypes[1].ValueOfExpressionType);
            Assert.AreEqual(ExpressionType.Equal, visitor.LastLevelNodeTypes[2].ValueOfExpressionType);
            Assert.AreEqual(ExpressionType.Call, visitor.LastLevelNodeTypes[3].ValueOfExpressionType);

            Assert.AreEqual("UnaryExpression", visitor.LastLevelNodeTypes[0].ExpressionType.Name);
            Assert.AreEqual("MethodCallExpression", visitor.LastLevelNodeTypes[1].ExpressionType.Name);
            Assert.AreEqual("BinaryExpression", visitor.LastLevelNodeTypes[2].ExpressionType.Name);
            Assert.AreEqual("MethodCallExpression", visitor.LastLevelNodeTypes[3].ExpressionType.Name);
        }
Пример #2
0
        public List <ExpressionWithMethodModel> VisitForConcatedBodyType(Expression exp, List <ExpressionType> concatedList)
        {
            WebRockExpressionConverterVisitor converterVisitor = new WebRockExpressionConverterVisitor();

            converterVisitor.Visit(exp);
            concatedList.AddRange(Enumerable.Select <ExpressionWithMethodModel, ExpressionType>((IEnumerable <ExpressionWithMethodModel>)converterVisitor.ConcatNodeTypes, (Func <ExpressionWithMethodModel, ExpressionType>)(c => c.ValueOfExpressionType)));
            return(converterVisitor.LastLevelNodeTypes);
        }
Пример #3
0
        public List <LastLevelNodeExpressionTreeModel> VisitLefts(Expression exp)
        {
            var converterVisitor = new WebRockExpressionConverterVisitor();
            var list             = new List <LastLevelNodeExpressionTreeModel>();

            converterVisitor.Visit(exp);
            var lefts  = converterVisitor.Lefts;
            var rights = converterVisitor.Rights;

            rights.Reverse();

            for (int index = 0; index < lefts.Count; ++index)
            {
                var expression1 = lefts[index];
                var condition   = converterVisitor.LastLevelNodeTypes.Count > index &&
                                  converterVisitor.LastLevelNodeTypes[index].ExpressionType == typeof(UnaryExpression);
                if (condition)
                {
                    rights.Insert(index, null);
                }

                var expression2 = rights.Count <= index || rights.Count == 0
                                        ? null
                                        : rights[index];

                var expressionType = converterVisitor.LastLevelNodeTypes.Count <= index || converterVisitor.LastLevelNodeTypes.Count == 0
                                        ? ExpressionType.Throw
                                        : converterVisitor.LastLevelNodeTypes[index].ValueOfExpressionType;

                var methodInfo = converterVisitor.LastLevelNodeTypes.Count <= index || converterVisitor.LastLevelNodeTypes.Count == 0
                                        ? null
                                        : converterVisitor.LastLevelNodeTypes[index].CurrentMethodInfo;

                list.Add(new LastLevelNodeExpressionTreeModel()
                {
                    LeftExpression            = expression1,
                    RightExpression           = expression2,
                    ExpressionWithMethodModel = new ExpressionWithMethodModel()
                    {
                        ValueOfExpressionType = expressionType,
                        CurrentMethodInfo     = methodInfo
                    }
                });
            }
            return(list);
        }
Пример #4
0
        public void CountOfLists()
        {
            var visitor = new WebRockExpressionConverterVisitor();
            Expression <Func <User, bool> > expression = user => user.IsActive;

            visitor.Visit(expression);

            Assert.AreEqual(true, visitor.Lefts.Count == 1);
            Assert.AreEqual(true, visitor.Rights.Count == 0);
            Assert.AreEqual(true, visitor.LastLevelNodeTypes.Count == 0);
            Assert.AreEqual(true, visitor.ConcatNodeTypes.Count == 0);


            visitor    = new WebRockExpressionConverterVisitor();
            expression = user => user.IsActive && user.Name == "123";
            visitor.Visit(expression);
            Assert.AreEqual(true, visitor.Lefts.Count == 2);
            Assert.AreEqual(true, visitor.Rights.Count == 1);
            Assert.AreEqual(true, visitor.ConcatNodeTypes.Count == 1);

            visitor    = new WebRockExpressionConverterVisitor();
            expression = user => user.Name.Contains("123");
            visitor.Visit(expression);
            Assert.AreEqual(true, visitor.Lefts.Count == 1);
            Assert.AreEqual(true, visitor.Rights.Count == 1);

            visitor    = new WebRockExpressionConverterVisitor();
            expression = user => user.Credentials.Password == "1";
            visitor.Visit(expression);
            Assert.AreEqual(true, visitor.Lefts.Count == 2);
            Assert.AreEqual(true, visitor.Rights.Count == 1);

            visitor    = new WebRockExpressionConverterVisitor();
            expression = user => user.Credentials.Token.CurrentTokenNumber == "1";
            visitor.Visit(expression);
            Assert.AreEqual(true, visitor.Lefts.Count == 3);
            Assert.AreEqual(true, visitor.Rights.Count == 1);

            visitor    = new WebRockExpressionConverterVisitor();
            expression = user => user.Activities.FirstOrDefault() != null;
            visitor.Visit(expression);
            Assert.AreEqual(true, visitor.Lefts.Count == 1);
            Assert.AreEqual(true, visitor.Rights.Count == 1);
            Assert.AreEqual(true, visitor.ConcatNodeTypes.Count == 0);
            Assert.AreEqual(true, visitor.LastLevelNodeTypes.Count == 2);
        }
Пример #5
0
        public List <LastLevelNodeExpressionTreeModel> VisitRights(Expression exp)
        {
            WebRockExpressionConverterVisitor       converterVisitor = new WebRockExpressionConverterVisitor();
            List <LastLevelNodeExpressionTreeModel> list             = new List <LastLevelNodeExpressionTreeModel>();

            converterVisitor.Visit(exp);
            for (int index = 0; index < converterVisitor.Rights.Count; ++index)
            {
                Expression     expression1    = converterVisitor.Lefts.Count < index || converterVisitor.Lefts.Count == 0 ? (Expression)null : converterVisitor.Lefts[index];
                Expression     expression2    = converterVisitor.Rights[index];
                ExpressionType expressionType = converterVisitor.LastLevelNodeTypes.Count < index || converterVisitor.LastLevelNodeTypes.Count == 0 ? ExpressionType.Throw : converterVisitor.LastLevelNodeTypes[index].ValueOfExpressionType;
                MethodInfo     methodInfo     = converterVisitor.LastLevelNodeTypes.Count < index || converterVisitor.LastLevelNodeTypes.Count == 0 ? (MethodInfo)null : converterVisitor.LastLevelNodeTypes[index].CurrentMethodInfo;
                list.Add(new LastLevelNodeExpressionTreeModel()
                {
                    LeftExpression            = expression1,
                    RightExpression           = expression2,
                    ExpressionWithMethodModel = new ExpressionWithMethodModel()
                    {
                        ValueOfExpressionType = expressionType,
                        CurrentMethodInfo     = methodInfo
                    }
                });
            }
            UnaryExpression orDefault = MaybeExtension.GetOrDefault <UnaryExpression>(MaybeExtension.MaybeAs <UnaryExpression>((object)exp, true), (UnaryExpression)null);

            if (orDefault != null)
            {
                list.Add(new LastLevelNodeExpressionTreeModel()
                {
                    LeftExpression            = orDefault.Operand,
                    RightExpression           = (Expression)null,
                    ExpressionWithMethodModel = new ExpressionWithMethodModel()
                    {
                        ValueOfExpressionType = orDefault.NodeType,
                        CurrentMethodInfo     = orDefault.Method
                    }
                });
            }
            return(list);
        }
Пример #6
0
        private static PredicateSet GetSinglePredicateSetFromBinaryExpression(Maybe <BinaryExpression> binBody)
        {
            if (binBody == Maybe.Nothing)
            {
                return(null);
            }
            var body = binBody.Value;

            var obj =
                body.Right.MaybeAs <MethodCallExpression>()
                .Bind(c => c.Arguments[0])
                .GetOrDefault(null)
                .MaybeAs <MemberExpression>()
                .Bind(c => c.Expression)
                .GetOrDefault(null)
                .MaybeAs <ConstantExpression>()
                .Bind(c => c.Value)
                .GetOrDefault(null);
            string fieldName = "value";

            if (obj != null)
            {
                fieldName = obj.GetType().GetFields()[0].Name;
            }
            var value = obj.NothingIfNull().Bind(c => c.GetType().GetField(fieldName)).Bind(c => c.GetValue(obj).ToString()).GetOrDefault(null);

            if (string.IsNullOrEmpty(value))
            {
                obj =
                    body.Right.MaybeAs <MemberExpression>()
                    .Bind(c => c.Expression)
                    .GetOrDefault(null)
                    .MaybeAs <ConstantExpression>()
                    .Bind(c => c.Value)
                    .GetOrDefault(null)
                    ??
                    body.Right.MaybeAs <MemberExpression>()
                    .Bind(c => c.Expression)
                    .GetOrDefault(null)
                    .MaybeAs <MemberExpression>()
                    .Bind(c => c.Expression)
                    .GetOrDefault(null)
                    .MaybeAs <ConstantExpression>()
                    .Bind(c => c.Value)
                    .GetOrDefault(null);
                if (obj != null)
                {
                    fieldName = obj.GetType().GetFields()[0].Name;
                }
                if (obj != null && obj.GetType() != typeof(string) && !obj.GetType().IsPrimitive&& obj.GetType().IsClass)
                {
                    //TODO достать значение свойства из object
                }
                value = obj.NothingIfNull().Bind(c => c.GetType().GetField(fieldName)).Bind(c => c.GetValue(obj).ToString()).GetOrDefault(null);
            }
            var listOfValues = new List <string>
            {
                body.Right.MaybeAs <ConstantExpression>().Bind(c => c.Value.ToString()).GetOrDefault(null),
                value
            };

            var property       = body.Left.MaybeAs <MethodCallExpression>().Bind(c => c.Object).GetOrDefault(null).MaybeAs <MemberExpression>().Bind(c => c.Member.Name).GetOrDefault(null);
            var listOfProperty = new List <string>()
            {
                body.Left.MaybeAs <MemberExpression>().Bind(c => c.Member.Name).GetOrDefault(null),
                property
            };

            if (property == null)
            {
                var vt = new WebRockExpressionConverterVisitor();
                vt.Visit(body.Left);
                foreach (var expression in vt.Lefts)
                {
                    expression.MaybeAs <MemberExpression>().Do((c) => listOfProperty.Add(c.Member.Name));
                }
            }
            var rightProperty = body.Right.MaybeAs <MethodCallExpression>().Bind(c => c.Object).GetOrDefault(null).MaybeAs <MemberExpression>().Bind(c => c.Member.Name).GetOrDefault(null);

            return(new PredicateSet
            {
                Method = body.Method == null?body.Left.MaybeAs <MethodCallExpression>().Bind(c => c.Method.Name).GetOrDefault(null) ?? "Contains" : body.Method.Name,
                Property = string.Join("&&", listOfProperty.Where(c => c != null).Distinct()) + "&&" + rightProperty,
                Value = listOfValues.FirstOrDefault(c => c != null)
            });
        }