Add import references to generated insertable

This commit is contained in:
Simon Binder 2023-08-13 18:59:31 +02:00
parent f8635469d4
commit 9c21e17d91
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
6 changed files with 94 additions and 19 deletions

View File

@ -2,6 +2,8 @@
- Reduce the amount of assets read by drift, improving build performance and enabling faster - Reduce the amount of assets read by drift, improving build performance and enabling faster
incremental rebuilds. incremental rebuilds.
- Fix missing import references around `@UseRowClass` with `generateInsertable: true` when
modular code generation is enabled.
## 2.11.0 ## 2.11.0

View File

@ -223,7 +223,7 @@ class UpdateCompanionWriter {
void _writeToString() { void _writeToString() {
overrideToString( overrideToString(
_emitter.dartCode(_emitter.companionType(table)), _emitter.companionType(table).toString(),
[for (final column in columns) column.nameInDart], [for (final column in columns) column.nameInDart],
_buffer, _buffer,
); );
@ -235,17 +235,31 @@ class UpdateCompanionWriter {
final info = table.existingRowClass; final info = table.existingRowClass;
if (info == null) return; if (info == null) return;
final rowClass = _emitter.rowClass(table).toString(); final rowClass = _emitter.rowClass(table);
final rowType = _emitter.dartCode(_emitter.rowType(table));
final insertableClass = '_\$${rowClass}Insertable'; final insertableClass = '_\$${rowClass}Insertable';
_buffer.write('class $insertableClass implements ' _emitter
'Insertable<$rowType> {\n' // Class _$RowInsertable implements Insertable<RowClass> {
'$rowType _object;\n\n' ..write('class $insertableClass implements ')
'$insertableClass(this._object);\n\n' ..writeDriftRef('Insertable')
'@override\n' ..write('<')
'Map<String, Expression> toColumns(bool nullToAbsent) {\n' ..writeDart(rowClass)
'return $_companionType(\n'); ..writeln('> {')
// Field to RowClass and constructor
..writeDart(rowClass)
..writeln(' _object;')
..writeln('$insertableClass(this._object);')
// Map<String, Expression> toColumns(bool nullToAbsent) {
..writeln('@override')
..writeUriRef(AnnotatedDartCode.dartCore, 'Map')
..write('<')
..writeUriRef(AnnotatedDartCode.dartCore, 'String')
..write(', ')
..writeUriRef(AnnotatedDartCode.drift, 'Expression')
..write('> toColumns(')
..writeUriRef(AnnotatedDartCode.dartCore, 'bool')
..writeln(' nullToAbsent) {')
..writeln('return $_companionType(');
final columns = info.positionalColumns.followedBy(info.namedColumns.values); final columns = info.positionalColumns.followedBy(info.namedColumns.values);
for (final columnName in columns) { for (final columnName in columns) {
@ -254,14 +268,18 @@ class UpdateCompanionWriter {
if (column != null && !column.isGenerated) { if (column != null && !column.isGenerated) {
final dartName = column.nameInDart; final dartName = column.nameInDart;
_buffer.write('$dartName: Value (_object.$dartName),\n'); _emitter
..write('$dartName: ')
..writeDriftRef('Value')
..writeln('(_object.$dartName),');
} }
} }
_buffer _emitter
..write(').toColumns(false);\n}\n}\n\n') ..write(').toColumns(false);\n}\n}\n\n')
..write('extension ${rowClass}ToInsertable ' ..write('extension ${rowClass}ToInsertable on ')
'on $rowType {') ..writeDart(rowClass)
..writeln('{')
..write('$insertableClass toInsertable() {\n') ..write('$insertableClass toInsertable() {\n')
..write('return $insertableClass(this);\n') ..write('return $insertableClass(this);\n')
..write('}\n}\n'); ..write('}\n}\n');

View File

@ -212,6 +212,61 @@ class MyTable extends Table {
}''')), }''')),
}, result.dartOutputs, result.writer); }, result.dartOutputs, result.writer);
}); });
test('generates correct companions for modular row classes', () async {
final result = await emulateDriftBuild(
inputs: {
'a|lib/a.dart': '''
import 'package:drift/drift.dart';
@UseRowClass(Item, generateInsertable: true)
class ItemTable extends Table {
IntColumn get id => integer().autoIncrement()();
}
class Item {
final int id;
Item(this.id);
}
''',
},
modularBuild: true,
);
checkOutputs(
{
'a|lib/a.drift.dart': decodedMatches(
allOf(
// The toString() definition for companions was broken and included
// the import prefix of the companion.
contains("StringBuffer('ItemTableCompanion(')"),
// The extension should also reference the row class correctly
contains(r'''
class _$ItemInsertable implements i0.Insertable<i1.Item> {
i1.Item _object;
_$ItemInsertable(this._object);
@override
Map<String, i0.Expression> toColumns(bool nullToAbsent) {
return i2.ItemTableCompanion(
id: i0.Value(_object.id),
).toColumns(false);
}
}
extension ItemToInsertable on i1.Item {
_$ItemInsertable toInsertable() {
return _$ItemInsertable(this);
}
}
'''),
),
),
},
result.dartOutputs,
result.writer,
);
});
} }
class _GeneratesConstDataClasses extends Matcher { class _GeneratesConstDataClasses extends Matcher {

View File

@ -205,7 +205,7 @@ class PostsCompanion extends i0.UpdateCompanion<i1.Post> {
@override @override
String toString() { String toString() {
return (StringBuffer('i1.PostsCompanion(') return (StringBuffer('PostsCompanion(')
..write('id: $id, ') ..write('id: $id, ')
..write('author: $author, ') ..write('author: $author, ')
..write('content: $content') ..write('content: $content')
@ -393,7 +393,7 @@ class LikesCompanion extends i0.UpdateCompanion<i1.Like> {
@override @override
String toString() { String toString() {
return (StringBuffer('i1.LikesCompanion(') return (StringBuffer('LikesCompanion(')
..write('post: $post, ') ..write('post: $post, ')
..write('likedBy: $likedBy, ') ..write('likedBy: $likedBy, ')
..write('rowid: $rowid') ..write('rowid: $rowid')

View File

@ -194,7 +194,7 @@ class SearchInPostsCompanion extends i0.UpdateCompanion<i1.SearchInPost> {
@override @override
String toString() { String toString() {
return (StringBuffer('i1.SearchInPostsCompanion(') return (StringBuffer('SearchInPostsCompanion(')
..write('author: $author, ') ..write('author: $author, ')
..write('content: $content, ') ..write('content: $content, ')
..write('rowid: $rowid') ..write('rowid: $rowid')

View File

@ -302,7 +302,7 @@ class UsersCompanion extends i0.UpdateCompanion<i1.User> {
@override @override
String toString() { String toString() {
return (StringBuffer('i1.UsersCompanion(') return (StringBuffer('UsersCompanion(')
..write('id: $id, ') ..write('id: $id, ')
..write('name: $name, ') ..write('name: $name, ')
..write('biography: $biography, ') ..write('biography: $biography, ')
@ -500,7 +500,7 @@ class FollowsCompanion extends i0.UpdateCompanion<i1.Follow> {
@override @override
String toString() { String toString() {
return (StringBuffer('i1.FollowsCompanion(') return (StringBuffer('FollowsCompanion(')
..write('followed: $followed, ') ..write('followed: $followed, ')
..write('follower: $follower, ') ..write('follower: $follower, ')
..write('rowid: $rowid') ..write('rowid: $rowid')