encodeData function

  1. @visibleForTesting
Map<String, dynamic> encodeData(
  1. dynamic value
)

Implementation

@visibleForTesting
Map<String, dynamic> encodeData(dynamic value) {
  if (value == null) return encodeNullData();

  if (value is String) {
    if (firestoreReferenceRegex.hasMatch(value)) return encodeReferenceData(value);

    final isValidDateTime = dateTimeRegex.hasMatch(value);
    if (isValidDateTime) return encodeTimestampData(value);

    return encodeStringData(value);
  }

  if (value is DateTime) return encodeTimestampData('${value.millisecondsSinceEpoch}');

  if (value is int) return encodeIntData(value);

  if (value is bool) return encodeBoolData(value);

  if (value is double) return encodeDoubleData(value);

  if (value is Map) {
    final isGeoPointData = value.containsKey('latitude') && value.containsKey('longitude') && value.entries.length == 2;
    if (isGeoPointData) return encodeGeoPointData(value);

    return encodeMapData(value);
  }

  if (value is List) return encodeListData(value);

  throw Exception("Type ${value.runtimeType} is not (for now) supported by our plugin.");
}