Support inheritance in table definitions (#169)

This commit is contained in:
Simon Binder 2019-10-11 17:43:48 +02:00
parent 77fcf7a7ba
commit 337e260667
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
5 changed files with 30 additions and 2 deletions

View File

@ -1,3 +1,8 @@
## unreleased
- Accept inheritance in table definitions (e.g. if an abstract class declared as `IntColumn get foo => integer()()`,
tables inheriting from that class will also have a `foo` column)
## 2.0.1
- Escape `\r` characters in generated Dart literals

View File

@ -110,7 +110,10 @@ class TableParser {
}
Future<List<SpecifiedColumn>> _parseColumns(ClassElement element) {
final columns = element.fields
final columns = element.allSupertypes
.map((t) => t.element)
.followedBy([element])
.expand((e) => e.fields)
.where((field) => isColumn(field.type) && field.getter != null);
return Future.wait(columns.map((field) async {

View File

@ -6,5 +6,7 @@ bool isFromMoor(DartType type) {
}
bool isColumn(DartType type) {
return isFromMoor(type) && type.name.contains('Column');
return isFromMoor(type) &&
type.name.contains('Column') &&
!type.name.contains('Builder');
}

View File

@ -43,6 +43,18 @@ void main() {
String constructTableName() => 'my-table-name';
String get tableName => constructTableName();
}
mixin IntIdTable on Table {
IntColumn get id => integer().autoIncrement()();
}
abstract class HasNameTable extends Table {
TextColumn get name => text()();
}
class Foos extends HasNameTable with IntIdTable {
}
'''
});
});
@ -143,4 +155,10 @@ void main() {
expect(table.primaryKey, containsAll(table.columns));
expect(table.columns.any((column) => column.hasAI), isFalse);
});
test('handles inheritance in column definitions', () async {
final table = await parse('Foos');
expect(table.columns.map((c) => c.name.name), containsAll(['id', 'name']));
});
}