feat: Name companion class

This commit is contained in:
ValentinVignal 2024-03-06 21:00:13 +08:00
parent ca0c70eac9
commit d768ceeb1a
No known key found for this signature in database
GPG Key ID: 040FFDADFB7EF05A
8 changed files with 42 additions and 10 deletions

View File

@ -308,7 +308,11 @@ final class TableIndex {
class DataClassName {
/// The overridden name to use when generating the data class for a table.
/// {@macro drift_custom_data_class}
final String name;
final String? name;
/// The overridden name to use when generating the companion class for a table.
/// {@macro drift_custom_data_class}
final String? companion;
/// The parent type of the data class generated by drift.
///
@ -345,7 +349,11 @@ class DataClassName {
/// Customize the data class name for a given table.
/// {@macro drift_custom_data_class}
const DataClassName(this.name, {this.extending});
const DataClassName(this.name, {this.extending, this.companion});
/// Customize the data class name for a given table.
/// {@macro drift_custom_data_class}
const DataClassName.custom({this.name, this.extending, this.companion});
}
/// An annotation specifying an existing class to be used as a data class.

View File

@ -198,11 +198,13 @@ extension TypeUtils on DartType {
class DataClassInformation {
final String enforcedName;
final String? companionName;
final CustomParentClass? extending;
final ExistingRowClass? existingClass;
DataClassInformation(
this.enforcedName,
this.companionName,
this.extending,
this.existingClass,
);
@ -233,16 +235,16 @@ class DataClassInformation {
));
}
String name;
var name = dataClassName?.getField('name')!.toStringValue() ??
dataClassNameForClassName(element.name);
final companionName =
dataClassName?.getField('companionName')?.toStringValue();
CustomParentClass? customParentClass;
ExistingRowClass? existingClass;
if (dataClassName != null) {
name = dataClassName.getField('name')!.toStringValue()!;
customParentClass =
parseCustomParentClass(name, dataClassName, element, resolver);
} else {
name = dataClassNameForClassName(element.name);
}
if (useRowClass != null) {
@ -277,7 +279,12 @@ class DataClassInformation {
}
}
return DataClassInformation(name, customParentClass, existingClass);
return DataClassInformation(
name,
companionName,
customParentClass,
existingClass,
);
}
}

View File

@ -55,6 +55,7 @@ class DartTableResolver extends LocalElementResolver<DiscoveredDartTable> {
columns: columns,
references: references.toList(),
nameOfRowClass: dataClassInfo.enforcedName,
nameOfCompanionClass: dataClassInfo.companionName,
existingRowClass: dataClassInfo.existingClass,
customParentClass: dataClassInfo.extending,
baseDartName: element.name,

View File

@ -40,6 +40,9 @@ abstract class DriftElementWithResultSet extends DriftSchemaElement {
/// The name for the data class associated with this table or view.
String get nameOfRowClass;
/// The name for the companion class associated with this table or view.
String? get nameOfCompanionClass;
/// All [columns] of this table, indexed by their name in SQL.
late final Map<String, DriftColumn> columnBySqlName = CaseInsensitiveMap.of({
for (final column in columns) column.nameInSql: column,

View File

@ -32,6 +32,9 @@ class DriftTable extends DriftElementWithResultSet {
@override
final String nameOfRowClass;
@override
final String? nameOfCompanionClass;
final bool withoutRowId;
/// Information about the virtual table creating statement backing this table,
@ -69,6 +72,7 @@ class DriftTable extends DriftElementWithResultSet {
required this.columns,
required this.baseDartName,
required this.nameOfRowClass,
this.nameOfCompanionClass,
this.references = const [],
this.existingRowClass,
this.customParentClass,

View File

@ -25,6 +25,9 @@ class DriftView extends DriftElementWithResultSet {
@override
final String nameOfRowClass;
@override
final String? nameOfCompanionClass;
@override
List<DriftElement> references;
@ -38,6 +41,7 @@ class DriftView extends DriftElementWithResultSet {
required this.existingRowClass,
required this.nameOfRowClass,
required this.references,
this.nameOfCompanionClass,
});
@override

View File

@ -57,6 +57,7 @@ class ElementSerializer {
'fixed_entity_info_name': element.fixedEntityInfoName,
'base_dart_name': element.baseDartName,
'row_class_name': element.nameOfRowClass,
'companion_class_name': element.nameOfCompanionClass,
'without_rowid': element.withoutRowId,
'strict': element.strict,
if (element.isVirtual)
@ -146,6 +147,7 @@ class ElementSerializer {
'custom_parent_class':
_serializeCustomParentClass(element.customParentClass),
'name_of_row_class': element.nameOfRowClass,
'name_of_companion_class': element.nameOfCompanionClass,
'source': serializedSource,
};
} else if (element is BaseDriftAccessor) {
@ -536,6 +538,7 @@ class ElementDeserializer {
fixedEntityInfoName: json['fixed_entity_info_name'] as String?,
baseDartName: json['base_dart_name'] as String,
nameOfRowClass: json['row_class_name'] as String,
nameOfCompanionClass: json['companion_class_name'] as String?,
withoutRowId: json['without_rowid'] as bool,
strict: json['strict'] as bool,
virtualTableData: virtualTableData,
@ -675,6 +678,7 @@ class ElementDeserializer {
customParentClass:
_readCustomParentClass(json['custom_parent_class'] as Map?),
nameOfRowClass: json['name_of_row_class'] as String,
nameOfCompanionClass: json['name_of_companion_class'] as String,
existingRowClass: json['existing_data_class'] != null
? await _readExistingRowClass(
id.libraryUri, json['existing_data_class'] as Map)

View File

@ -72,9 +72,10 @@ abstract class _NodeOrWriter {
}
AnnotatedDartCode companionType(DriftTable table) {
final baseName = writer.options.useDataClassNameForCompanions
? table.nameOfRowClass
: table.baseDartName;
final baseName = table.nameOfCompanionClass ??
(writer.options.useDataClassNameForCompanions
? table.nameOfRowClass
: table.baseDartName);
return generatedElement(table, '${baseName}Companion');
}