diff --git a/moor_generator/CHANGELOG.md b/moor_generator/CHANGELOG.md index 8b31d97f..e87a68bb 100644 --- a/moor_generator/CHANGELOG.md +++ b/moor_generator/CHANGELOG.md @@ -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 diff --git a/moor_generator/test.yaml b/moor_generator/dart_test.yaml similarity index 100% rename from moor_generator/test.yaml rename to moor_generator/dart_test.yaml diff --git a/moor_generator/lib/src/analyzer/dart/table_parser.dart b/moor_generator/lib/src/analyzer/dart/table_parser.dart index bd149fa4..274366ef 100644 --- a/moor_generator/lib/src/analyzer/dart/table_parser.dart +++ b/moor_generator/lib/src/analyzer/dart/table_parser.dart @@ -110,7 +110,10 @@ class TableParser { } Future> _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 { diff --git a/moor_generator/lib/src/utils/type_utils.dart b/moor_generator/lib/src/utils/type_utils.dart index a8a789a8..e720a5a4 100644 --- a/moor_generator/lib/src/utils/type_utils.dart +++ b/moor_generator/lib/src/utils/type_utils.dart @@ -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'); } diff --git a/moor_generator/test/analyzer/dart/table_parser_test.dart b/moor_generator/test/analyzer/dart/table_parser_test.dart index df6b46c4..bfb99652 100644 --- a/moor_generator/test/analyzer/dart/table_parser_test.dart +++ b/moor_generator/test/analyzer/dart/table_parser_test.dart @@ -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'])); + }); }