Exemplo n.º 1
0
        /// <summary>
        /// Evaluates filter expression agains message context
        /// </summary>
        /// <param name="messageContext">Key value pair store with message context</param>
        /// <returns>Evaluation result task</returns>
        public async Task <IComparable> EvaluateAsync(IReadOnlyDictionary <string, object> messageContext)
        {
            // When no actual filter was created as expression we think that evaluation passed successfully, because there were nothing to compare to
            if (Operation == 0 && (LeftExpression == null || RightExpression == null))
            {
                return(true);
            }

            var left  = LeftExpression.EvaluateAsync(messageContext);
            var right = RightExpression.EvaluateAsync(messageContext);

            await Task.WhenAll(left, right);

            if (object.ReferenceEquals(left.Result, NullEvaluationResult.Value))
            {
                return(false);
            }

            //actual operation between left and right
            switch (Operation)
            {
            case FilterOperator.And:
            {
                return((bool)left.Result && (bool)right.Result);
            }

            case FilterOperator.Or:
            {
                return((bool)left.Result || (bool)right.Result);
            }

            case FilterOperator.EqualTo:
            {
                return(left.Result.Equals(right.Result));
            }

            case FilterOperator.NotEqualTo:
            {
                return(!left.Result.Equals(right.Result));
            }

            case FilterOperator.GreaterThan:
            {
                return(left.Result.CompareTo(right.Result) == 1);
            }

            case FilterOperator.GreaterThanOrEqualTo:
            {
                return(left.Result.CompareTo(right.Result) >= 0);
            }

            case FilterOperator.LessThanOrEqualTo:
            {
                return(left.Result.CompareTo(right.Result) <= 0);
            }

            case FilterOperator.LessThan:
            {
                return(left.Result.CompareTo(right.Result) == -1);
            }
            }
            return(false);
        }