matchType static method

bool matchType(
  1. dynamic expectedType,
  2. TypeCode actualType, [
  3. dynamic actualValue
])

Matches expected type to an actual type. The types can be specified as types, type names or TypeCode.

  • expectedType an expected type to match.
  • actualType an actual type to match.
  • actualValue an optional value to match its type to the expected one. Returns true if types are matching and false if they don't.

See matchTypeByName See matchTypeByName (for matching by types' string names)

Implementation

static bool matchType(expectedType, TypeCode actualType, [actualValue]) {
  if (expectedType == null) return true;
  // if (actualType == null) throw Exception('Actual type cannot be null');

  if (expectedType is TypeCode) {
    if (expectedType == actualType) return true;
    // Special provisions for dynamic data
    if (expectedType == TypeCode.Integer &&
        (actualType == TypeCode.Long ||
            actualType == TypeCode.Float ||
            actualType == TypeCode.Double)) return true;
    if (expectedType == TypeCode.Long &&
        (actualType == TypeCode.Integer ||
            actualType == TypeCode.Float ||
            actualType == TypeCode.Double)) return true;
    if (expectedType == TypeCode.Float &&
        (actualType == TypeCode.Integer ||
            actualType == TypeCode.Long ||
            actualType == TypeCode.Double)) return true;
    if (expectedType == TypeCode.Double &&
        (actualType == TypeCode.Integer ||
            actualType == TypeCode.Long ||
            actualType == TypeCode.Float)) return true;
    if (expectedType == TypeCode.DateTime &&
        (actualType == TypeCode.String &&
            DateTimeConverter.toNullableDateTime(actualValue) != null)) {
      return true;
    }
    return false;
  }

  if (expectedType is String) {
    return matchTypeByName(expectedType, actualType, actualValue);
  }

  return false;
}