Skip to content

Commit

Permalink
Merge branch 'more-plurals' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Nov 22, 2023
2 parents 4c3b128 + 28c3c44 commit 08241bf
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 51 deletions.
4 changes: 4 additions & 0 deletions drift_dev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 2.14.0-dev

- __Breaking change__: The name of the generated row class derived from the name
of the Dart table name now supports more forms of plurals.
For instance, a table without a `@DataClassName` annotation named `Categories`
would now generate a `Category` class instead of `Categorie`.
- Don't generate `const` row classes when they are extending a class which
isn't const.

Expand Down
19 changes: 12 additions & 7 deletions drift_dev/lib/src/analysis/resolver/shared/data_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ String dataClassNameForClassName(String tableName) {
// they're storing (users, products, ...). We try to find the singular word
// from the table name.

// todo we might want to implement some edge cases according to
// https://en.wikipedia.org/wiki/English_plurals

if (tableName.endsWith('s')) {
return tableName.substring(0, tableName.length - 1);
if (tableName.endsWith('ss') ||
tableName.endsWith('us') ||
tableName.endsWith('sses')) {
return tableName;
} else if (tableName.endsWith('ies')) {
return '${tableName.substring(0, tableName.length - 3)}y';
} else {
return tableName.substring(0, tableName.length - 1);
}
} else {
// Default behavior if the table name is not a valid plural.
return '${tableName}Data';
}

// Default behavior if the table name is not a valid plural.
return '${tableName}Data';
}

CustomParentClass? parseCustomParentClass(
Expand Down
45 changes: 45 additions & 0 deletions drift_dev/test/analysis/resolver/dart/table_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ class InvalidConstraints extends Table {
IntColumn get a => integer().autoIncrement().customConstraint('foo')();
IntColumn get b => integer().customConstraint('a').customConstraint('b')();
}
class Properties extends Table {
IntColumn get id => integer().autoIncrement()();
}
class Cats extends Table {
IntColumn get id => integer().autoIncrement()();
}
class Messages extends Table {
IntColumn get id => integer().autoIncrement()();
}
class Pianos extends Table {
IntColumn get id => integer().autoIncrement()();
}
''',
});
});
Expand Down Expand Up @@ -142,6 +158,35 @@ class InvalidConstraints extends Table {
});
});

group("class name inflection", () {
test("singularizes table name that end in -ies", () async {
final result = await findTable('Properties');
final table = result!.result as DriftTable;

expect(table.nameOfRowClass, 'Property');
});

test("singularizes table name that end in -s", () async {
final result = await findTable('Cats');
final table = result!.result as DriftTable;

expect(table.nameOfRowClass, 'Cat');
});

test("singularizes table name that end in -es", () async {
final result = await findTable('Messages');
final table = result!.result as DriftTable;

expect(table.nameOfRowClass, 'Message');
});

test("singularizes table name that end in -os", () async {
final result = await findTable('Pianos');
final table = result!.result as DriftTable;

expect(table.nameOfRowClass, 'Piano');
});
});
group('Columns', () {
test('should use field name if no name has been set explicitly', () async {
final result = await findTable('Users');
Expand Down
28 changes: 14 additions & 14 deletions examples/app/lib/database/database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/encryption/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:

dependencies:
drift: ^2.0.2+1
sqlcipher_flutter_libs: ^0.5.1
sqlcipher_flutter_libs: ^0.6.0
flutter:
sdk: flutter
path_provider: ^2.0.11
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_web_worker_example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void main() {
}

class _DatabaseSampleState extends State<_DatabaseSample> {
List<Entrie> allItems = [];
List<Entry> allItems = [];
TextEditingController editController = TextEditingController();
final database = MyDatabase(Platform.createDatabaseConnection('sample'));

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 08241bf

Please sign in to comment.