evalOperatorCondition method

bool evalOperatorCondition(
  1. String operator,
  2. dynamic attributeValue,
  3. dynamic conditionValue
)

This function is just a case statement that handles all the possible operators There are basic comparison operators in the form attributeValue {op} conditionValue.

Implementation

bool evalOperatorCondition(String operator, dynamic attributeValue, dynamic conditionValue) {
  /// Evaluate TYPE operator - whether both are of the same type
  if (operator == "\$type") {
    return getType(attributeValue).name == conditionValue;
  }

  /// Evaluate NOT operator - whether condition doesn't contain attribute
  if (operator == "\$not") {
    return !isEvalConditionValue(conditionValue, attributeValue);
  }

  /// Evaluate EXISTS operator - whether condition contains attribute
  if (operator == "\$exists") {
    if (conditionValue.toString() == 'false' && attributeValue == null) {
      return true;
    } else if (conditionValue.toString() == 'true' && attributeValue != null) {
      return true;
    }
  }

  /// There are three operators where conditionValue is an array
  if (conditionValue is List) {
    switch (operator) {
      case '\$in':
        return isIn(attributeValue, conditionValue);

      /// Evaluate NIN operator - attributeValue not in the conditionValue
      /// array.
      case '\$nin':
        return !isIn(attributeValue, conditionValue);

      /// Evaluate ALL operator - whether condition contains all attribute
      case '\$all':
        if (attributeValue is List) {
          /// Loop through conditionValue array
          /// If none of the elements in the attributeValue array pass
          /// evalConditionValue(conditionValue[i], attributeValue[j]),
          /// return false.
          for (var con in conditionValue) {
            var result = false;
            for (var attr in attributeValue) {
              if (isEvalConditionValue(con, attr)) {
                result = true;
              }
            }
            if (!result) {
              return result;
            }
          }
          return true;
        } else {
          /// If attributeValue is not an array, return false
          return false;
        }
      default:
        return false;
    }
  } else if (attributeValue is List) {
    switch (operator) {
      /// Evaluate ELEMENT-MATCH operator - whether condition matches attribute
      case "\$elemMatch":
        return elemMatch(attributeValue, conditionValue);

      /// Evaluate SIE operator - whether condition size is same as that
      /// of attribute
      case "\$size":
        return isEvalConditionValue(conditionValue, attributeValue.length);

      default:
    }
  } else if ((attributeValue as Object?).isPrimitive && (conditionValue as Object?).isPrimitive) {
    final targetPrimitiveValue = double.tryParse(conditionValue.toString());
    final sourcePrimitiveValue = double.tryParse(attributeValue.toString());
    final paddedVersionTarget = GBUtils.paddedVersionString(conditionValue.toString());
    final paddedVersionSource = GBUtils.paddedVersionString(attributeValue?.toString() ?? '0.0');

    /// If condition is bool.
    bool evaluatedValue = false;
    switch (operator) {
      case "\$eq":
        evaluatedValue = conditionValue == attributeValue;
        break;
      case "\$ne":
        evaluatedValue = conditionValue != attributeValue;
        break;
      case "\$veq":
        return paddedVersionSource == paddedVersionTarget;
      case "\$vne":
        return paddedVersionSource != paddedVersionTarget;
      case "\$vgt":
        return paddedVersionSource > paddedVersionTarget;
      case "\$vgte":
        return paddedVersionSource >= paddedVersionTarget;
      case "\$vlt":
        return paddedVersionSource < paddedVersionTarget;
      case "\$vlte":
        return paddedVersionSource <= paddedVersionTarget;

      /// Evaluate LT operator - whether attribute less than to condition
      case '\$lt':
        if (conditionValue is String && attributeValue is String) {
          return attributeValue.compareTo(conditionValue) < 0;
        }
        evaluatedValue = (sourcePrimitiveValue ?? 0.0) < (targetPrimitiveValue ?? 0);
        break;

      /// Evaluate LTE operator - whether attribute less than or equal to condition
      case '\$lte':
        evaluatedValue = (sourcePrimitiveValue ?? 0.0) <= (targetPrimitiveValue ?? 0);
        break;

      /// Evaluate GT operator - whether attribute greater than to condition
      case '\$gt':
        if (conditionValue is String && attributeValue is String) {
          return attributeValue.compareTo(conditionValue) > 0;
        }
        evaluatedValue = (sourcePrimitiveValue ?? 0.0) > (targetPrimitiveValue ?? 0);
        break;

      case '\$gte':
        evaluatedValue = (sourcePrimitiveValue ?? 0.0) >= (targetPrimitiveValue ?? 0);
        break;

      case '\$regex':
        try {
          final regEx = RegExp(conditionValue.toString());
          evaluatedValue = regEx.hasMatch(attributeValue.toString());
        } catch (e) {
          evaluatedValue = false;
        }
        break;

      default:
        conditionValue = false;
    }
    return evaluatedValue;
  }
  return false;
}