DatabaseSwagger.fromJson constructor

DatabaseSwagger.fromJson(
  1. Map<String, dynamic> json,
  2. Map<String, List<String>> configEnums,
  3. bool jsonbToDynamic, {
  4. Map<String, JsonbModelConfig>? jsonbModels,
})

Parses the PostgREST schema. configEnums are the enums from supadart.yaml, keyed by type name with or without a public. prefix.

Implementation

factory DatabaseSwagger.fromJson(Map<String, dynamic> json,
    Map<String, List<String>> configEnums, bool jsonbToDynamic,
    {Map<String, JsonbModelConfig>? jsonbModels}) {
  final definitions = json['definitions'] as Map<String, dynamic>;

  final configured = {
    for (final e in configEnums.entries)
      if (e.value.isNotEmpty) publicTypeName(_qualify(e.key))!: e.value
  };
  // PostgREST includes the values of enum columns, but not enum arrays.
  final fromSchema = <String, List<String>>{};
  for (final table in definitions.values) {
    for (final column in (table['properties'] as Map).values) {
      final typeName = publicTypeName(column['format']);
      if (typeName != null && column['enum'] != null) {
        fromSchema[typeName] = List<String>.from(column['enum']);
      }
    }
  }
  final merged = {...configured, ...fromSchema};
  final enums = {
    for (final name in merged.keys.toList()..sort()) name: merged[name]!
  };

  final tables = definitions.map((key, value) => MapEntry(
      key,
      Table.fromJson(key, value, enums, jsonbToDynamic,
          jsonbModels: jsonbModels)));

  return DatabaseSwagger(tables, enums: enums, warnings: [
    ..._configMismatches(configured, fromSchema),
    ..._unresolvedTypes(tables),
  ]);
}