cast<T> method

T cast<T>()

Implementation

T cast<T>() {
  if (value is T) return value;
  if (value is int) {
    if (BigInt.zero is T) {
      return BigInt.from(value) as T;
    } else if (T == bool) {
      if (value != 0 && value != 1) {
        throw MessageException("Invalid boolean value.",
            details: {"value": value});
      }
      return (value == 1 ? true : false) as T;
    }
  }
  if (value is BigInt && 0 is T) {
    value as BigInt;
    if ((value as BigInt).isValidInt) return value.toInt();
  }
  if (value is List<int> && T == String) {
    return StringUtils.decode(value) as T;
  }
  throw MessageException("cannot cast value.", details: {
    "Type": "$T",
    "Excepted": value.runtimeType.toString(),
    "value": value
  });
}