fromConfig static method

Future<DBObjectGCSAdapter> fromConfig(
  1. Map<String, dynamic>? config, {
  2. EntityRepositoryProvider? parentRepositoryProvider,
  3. String? workingPath,
})

Implementation

static Future<DBObjectGCSAdapter> fromConfig(Map<String, dynamic>? config,
    {EntityRepositoryProvider? parentRepositoryProvider,
    String? workingPath}) async {
  boot();

  var credential = config?.getIgnoreCase('credential');
  if (credential == null) {
    throw ArgumentError("Config without `credential`");
  }

  var project = config?.getAsString('project', ignoreCase: true);
  if (project == null || project.isEmpty) {
    throw ArgumentError("Config without `project`");
  }

  var bucket = config?.getAsString('bucket', ignoreCase: true);
  if (bucket == null || bucket.isEmpty) {
    throw ArgumentError("Config without `bucket`");
  }

  var cacheConfig = config?.getAsMap('cache', ignoreCase: true);

  Directory? cacheDir;
  int? cacheLimit;

  if (cacheConfig != null) {
    var path = cacheConfig.getAsString('path', ignoreCase: true)?.trim();
    if (path != null && path.isNotEmpty) {
      cacheDir = Directory(path);
    }

    var limitStr = cacheConfig.getAsString('limit', ignoreCase: true)?.trim();

    if (limitStr != null && limitStr.isNotEmpty) {
      limitStr = limitStr.replaceAll(RegExp(r'\s'), '').toLowerCase();

      var nPart = limitStr.length > 1
          ? limitStr.substring(0, limitStr.length - 1)
          : limitStr;

      int unit;
      if (limitStr.endsWith('g')) {
        cacheLimit = int.tryParse(nPart);
        unit = 1024 * 1024 * 1024;
      } else if (limitStr.endsWith('m')) {
        cacheLimit = int.tryParse(nPart);
        unit = 1024 * 1024;
      } else if (limitStr.endsWith('k')) {
        cacheLimit = int.tryParse(nPart);
        unit = 1024;
      } else {
        cacheLimit = int.tryParse(limitStr);
        unit = 1;
      }

      if (cacheLimit != null) {
        cacheLimit = cacheLimit * unit;

        if (cacheLimit <= 0) {
          cacheLimit = null;
        }
      }
    }
  }

  var populate = config?['populate'];

  var generateTables = false;
  Object? populateTables;
  Object? populateSource;
  Object? populateSourceVariables;

  if (populate is Map) {
    generateTables = populate.getAsBool('generateTables', ignoreCase: true) ??
        populate.getAsBool('generate-tables', ignoreCase: true) ??
        populate.getAsBool('generate_tables', ignoreCase: true) ??
        false;

    populateTables = populate['tables'];
    populateSource = populate['source'];
    populateSourceVariables = populate['variables'];
  }

  var client = await createGCSClient(credential);

  var adapter = DBObjectGCSAdapter(
    client,
    project,
    bucket,
    cacheDirectory: cacheDir,
    cacheLimit: cacheLimit,
    parentRepositoryProvider: parentRepositoryProvider,
    generateTables: generateTables,
    populateTables: populateTables,
    populateSource: populateSource,
    populateSourceVariables: populateSourceVariables,
    workingPath: workingPath,
  );

  return adapter;
}