load static method

List<Map<String, dynamic>> load({
  1. required String fixturesDir,
  2. required String entity,
})

Loads the fixture records for entity, failing fast.

Implementation

static List<Map<String, dynamic>> load({
  required String fixturesDir,
  required String entity,
}) {
  final file = File(pathFor(fixturesDir: fixturesDir, entity: entity));
  if (!file.existsSync()) {
    throw SimulationFixtureError(
      entity,
      file.path,
      'fixture file is missing',
    );
  }
  final Object? decoded;
  try {
    decoded = jsonDecode(file.readAsStringSync());
  } on FormatException catch (e) {
    throw SimulationFixtureError(
      entity,
      file.path,
      'fixture is not valid JSON: $e',
    );
  }
  if (decoded is! Map<String, dynamic>) {
    throw SimulationFixtureError(
      entity,
      file.path,
      'fixture is not a JSON object',
    );
  }
  final records = decoded['records'];
  if (records is! List) {
    throw SimulationFixtureError(
      entity,
      file.path,
      'fixture has no records list',
    );
  }
  for (final record in records) {
    if (record is! Map<String, dynamic>) {
      throw SimulationFixtureError(
        entity,
        file.path,
        'fixture records must be JSON objects',
      );
    }
  }
  return records.cast<Map<String, dynamic>>();
}