convertCell function Null safety

dynamic convertCell(
  1. String type,
  2. String? stringValue
)

If the value of the cell is null, returns null. Otherwise converts the string value to the correct type.

type A postgres column type stringValue The cell value

@example convertCell('bool', 'true')
=> true
@example convertCell('int8', '10')
=> 10
@example convertCell('_int4', '{1,2,3,4}')
=> [1,2,3,4]

Implementation

dynamic convertCell(String type, String? stringValue) {
  try {
    if (stringValue == null) return null;

    // if data type is an array
    if (type[0] == '_') {
      final arrayType = type.substring(1, type.length);
      return toArray(type: arrayType, stringValue: stringValue);
    }

    final typeEnum = PostgresTypes.values
        .firstWhereOrNull((e) => e.toString() == 'PostgresTypes.$type');
    // If not null, convert to correct type.
    switch (typeEnum) {
      case PostgresTypes.abstime:
        return noop(stringValue); // To allow users to cast it based on Timezone
      case PostgresTypes.bool:
        return toBoolean(stringValue);
      case PostgresTypes.date:
        return noop(stringValue); // To allow users to cast it based on Timezone
      case PostgresTypes.daterange:
        return toDateRange(stringValue);
      case PostgresTypes.float4:
        return toDouble(stringValue);
      case PostgresTypes.float8:
        return toDouble(stringValue);
      case PostgresTypes.int2:
        return toInt(stringValue);
      case PostgresTypes.int4:
        return toInt(stringValue);
      case PostgresTypes.int4range:
        return toIntRange(stringValue);
      case PostgresTypes.int8:
        return toInt(stringValue);
      case PostgresTypes.int8range:
        return toIntRange(stringValue);
      case PostgresTypes.json:
        return toJson(stringValue);
      case PostgresTypes.jsonb:
        return toJson(stringValue);
      case PostgresTypes.money:
        return toDouble(stringValue);
      case PostgresTypes.numeric:
        return toDouble(stringValue);
      case PostgresTypes.oid:
        return toInt(stringValue);
      case PostgresTypes.reltime:
        return noop(stringValue); // To allow users to cast it based on Timezone
      case PostgresTypes.time:
        return noop(stringValue); // To allow users to cast it based on Timezone
      case PostgresTypes.timestamp:
        return toTimestampString(stringValue); // Tobe consistent with PostgREST
      case PostgresTypes.timestamptz:
        return noop(stringValue); // To allow users to cast it based on Timezone
      case PostgresTypes.timetz:
        return noop(stringValue); // To allow users to cast it based on Timezone
      case PostgresTypes.tsrange:
        return toDateRange(stringValue);
      case PostgresTypes.tstzrange:
        return toDateRange(stringValue);
      default:
        // All the rest will be returned as strings
        return noop(stringValue);
    }
  } catch (error) {
    //print('Could not convert cell of type $type and value $stringValue');
    //print('This is the error: $error');
    return stringValue;
  }
}