Ejemplo n.º 1
0
        // caller is responsible for ensuring constant is on the right
        public static AstFilter Translate(
            TranslationContext context,
            Expression expression,
            Expression leftExpression,
            AstComparisonFilterOperator comparisonOperator,
            Expression rightExpression)
        {
            if (leftExpression is BinaryExpression leftBinaryExpression &&
                leftBinaryExpression.NodeType == ExpressionType.And)
            {
                var fieldExpression = leftBinaryExpression.Left;
                var field           = ExpressionToFilterFieldTranslator.Translate(context, fieldExpression);

                var bitMaskExpression = leftBinaryExpression.Right;
                var bitMask           = bitMaskExpression.GetConstantValue <object>(containingExpression: expression);
                var serializedBitMask = SerializationHelper.SerializeValue(field.Serializer, bitMask);

                var rightValue = rightExpression.GetConstantValue <object>(containingExpression: expression);
                var zeroValue  = Activator.CreateInstance(bitMask.GetType());

                switch (comparisonOperator)
                {
                case AstComparisonFilterOperator.Eq:
                    if (rightValue.Equals(zeroValue))
                    {
                        return(AstFilter.BitsAllClear(field, serializedBitMask));
                    }
                    else if (rightValue.Equals(bitMask))
                    {
                        return(AstFilter.BitsAllSet(field, serializedBitMask));
                    }
                    break;

                case AstComparisonFilterOperator.Ne:
                    if (rightValue.Equals(zeroValue))
                    {
                        return(AstFilter.BitsAnySet(field, serializedBitMask));
                    }
                    else if (rightValue.Equals(bitMask))
                    {
                        return(AstFilter.BitsAnyClear(field, serializedBitMask));
                    }
                    break;
                }
            }

            throw new ExpressionNotSupportedException(expression);
        }