fromCsv method

  1. @override
Map<String, DynamicMap> fromCsv(
  1. List<List> csv
)
override

CSV data List can be retrieved in NoSqlDatabase Map.

Specify the ID of the document as the key of Map and the data of the document as the value in DynamicMap.

CSVのデータListNoSqlDatabaseで取得可能なMap

MapのキーにドキュメントのIDを指定し、値にドキュメントのデータをDynamicMapで指定します。

Implementation

@override
Map<String, DynamicMap> fromCsv(List<List<dynamic>> csv) {
  final res = <String, dynamic>{};
  final documentId = this.documentId ?? source.hashCode.toString();
  switch (direction) {
    case Axis.horizontal:
      final effectiveOffset = offset ?? const Offset(0, 0);
      if (csv.length <= effectiveOffset.dy + 1) {
        throw Exception("The number of rows is less than the offset.");
      }
      final header = csv[effectiveOffset.dy.toInt()];
      final values = csv[effectiveOffset.dy.toInt() + 1];
      if (header.length <= effectiveOffset.dx ||
          values.length <= effectiveOffset.dx) {
        throw Exception("The number of columns is less than the offset.");
      }
      for (var i = effectiveOffset.dx.toInt(); i < header.length; i++) {
        final key = header[i].toString().trim();
        final value = values[i];
        if (value == null || key.isEmpty) {
          continue;
        }
        final r = _toAny(value);
        if (r != null) {
          if (res.containsKey(key)) {
            if (res[key] is List) {
              res[key].add(r);
            } else {
              res[key] = [res[key], r];
            }
          } else {
            res[key] = r;
          }
        } else if (res.containsKey(key)) {
          if (res[key] is! List) {
            res[key] = [res[key]];
          }
        }
      }
      return {
        documentId: _merged(res),
      };

    case Axis.vertical:
      final effectiveOffset = offset ?? const Offset(0, 0);
      if (csv.length <= effectiveOffset.dy) {
        throw Exception("The number of rows is less than the offset.");
      }
      for (var i = effectiveOffset.dy.toInt(); i < csv.length; i++) {
        final line = csv[i];
        if (line.length <= effectiveOffset.dx + 1) {
          throw Exception("The number of columns is less than the offset.");
        }
        final key = line[effectiveOffset.dx.toInt()].toString().trim();
        final value = line[effectiveOffset.dx.toInt() + 1];
        if (value == null || key.isEmpty) {
          continue;
        }
        final r = _toAny(value);
        if (r != null) {
          res[key] = r;
        }
      }
      return {
        documentId: _merged(res),
      };
  }
}