Column.fromJson constructor

Column.fromJson(
  1. String colName,
  2. Map<String, dynamic> json,
  3. List<String> parentTableRequiredFields,
  4. Map<String, List<String>> mapOfEnums, {
  5. bool jsonbToDynamic = false,
  6. String? schema,
  7. String? tableName,
  8. Map<String, JsonbModelConfig>? jsonbModels,
})

Implementation

factory Column.fromJson(
    String colName,
    Map<String, dynamic> json,
    List<String> parentTableRequiredFields,
    Map<String, List<String>> mapOfEnums,
    {bool jsonbToDynamic = false,
    String? schema,
    String? tableName,
    Map<String, JsonbModelConfig>? jsonbModels}) {
  List<String> enumValues =
      json['enum'] != null ? List<String>.from(json['enum']) : <String>[];
  if (json['format'].toString().contains("public.")) {
    for (var enumName in mapOfEnums.keys) {
      if (json['format'].toString().contains(enumName)) {
        enumValues = mapOfEnums[enumName]!;
        // print("enumValues set for ${json['format']} to $enumValues");
      }
    }
  }

  // Look up JSONB model config if available
  JsonbModelConfig? jsonbConfig;
  if (jsonbModels != null && schema != null && tableName != null) {
    final lookupKey = '$schema.$tableName.$colName';
    jsonbConfig = jsonbModels[lookupKey];
  }

  return Column(
    postgresFormat: json['format'],
    dbColName: colName,
    camelColName: snakeCasingToCamelCasing(colName),
    enumValues: enumValues,
    hasDefaultValue: json['description']?.contains('[supadart:serial]') ??
        json['default'] != null,
    description: json['description'],
    maxLength: json['maxLength'],
    isPrimaryKey: json['description']?.contains('<pk/>') ?? false,
    isSerialType: json['description']?.contains('[supadart:serial]') ?? false,
    isInRequiredColumn: parentTableRequiredFields.contains(colName),
    isEnum: json['enum'] != null || enumValues.isNotEmpty,
    jsonbToDynamic: jsonbToDynamic,
    jsonbModelConfig: jsonbConfig,
  );
}