Skip to content

Commit

Permalink
Add import references to generated insertable
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Aug 13, 2023
1 parent f863546 commit 9c21e17
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 19 deletions.
2 changes: 2 additions & 0 deletions drift_dev/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

## 2.11.0

Expand Down
46 changes: 32 additions & 14 deletions drift_dev/lib/src/writer/tables/update_companion_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class UpdateCompanionWriter {

void _writeToString() {
overrideToString(
_emitter.dartCode(_emitter.companionType(table)),
_emitter.companionType(table).toString(),
[for (final column in columns) column.nameInDart],
_buffer,
);
Expand All @@ -235,17 +235,31 @@ class UpdateCompanionWriter {
final info = table.existingRowClass;
if (info == null) return;

final rowClass = _emitter.rowClass(table).toString();
final rowType = _emitter.dartCode(_emitter.rowType(table));
final rowClass = _emitter.rowClass(table);
final insertableClass = '_\$${rowClass}Insertable';

_buffer.write('class $insertableClass implements '
'Insertable<$rowType> {\n'
'$rowType _object;\n\n'
'$insertableClass(this._object);\n\n'
'@override\n'
'Map<String, Expression> toColumns(bool nullToAbsent) {\n'
'return $_companionType(\n');
_emitter
// Class _$RowInsertable implements Insertable<RowClass> {
..write('class $insertableClass implements ')
..writeDriftRef('Insertable')
..write('<')
..writeDart(rowClass)
..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);
for (final columnName in columns) {
Expand All @@ -254,14 +268,18 @@ class UpdateCompanionWriter {

if (column != null && !column.isGenerated) {
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('extension ${rowClass}ToInsertable '
'on $rowType {')
..write('extension ${rowClass}ToInsertable on ')
..writeDart(rowClass)
..writeln('{')
..write('$insertableClass toInsertable() {\n')
..write('return $insertableClass(this);\n')
..write('}\n}\n');
Expand Down
55 changes: 55 additions & 0 deletions drift_dev/test/writer/data_class_writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,61 @@ class MyTable extends Table {
}''')),
}, 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 {
Expand Down
4 changes: 2 additions & 2 deletions examples/modular/lib/src/posts.drift.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class PostsCompanion extends i0.UpdateCompanion<i1.Post> {

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

@override
String toString() {
return (StringBuffer('i1.LikesCompanion(')
return (StringBuffer('LikesCompanion(')
..write('post: $post, ')
..write('likedBy: $likedBy, ')
..write('rowid: $rowid')
Expand Down
2 changes: 1 addition & 1 deletion examples/modular/lib/src/search.drift.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class SearchInPostsCompanion extends i0.UpdateCompanion<i1.SearchInPost> {

@override
String toString() {
return (StringBuffer('i1.SearchInPostsCompanion(')
return (StringBuffer('SearchInPostsCompanion(')
..write('author: $author, ')
..write('content: $content, ')
..write('rowid: $rowid')
Expand Down
4 changes: 2 additions & 2 deletions examples/modular/lib/src/users.drift.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class UsersCompanion extends i0.UpdateCompanion<i1.User> {

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

@override
String toString() {
return (StringBuffer('i1.FollowsCompanion(')
return (StringBuffer('FollowsCompanion(')
..write('followed: $followed, ')
..write('follower: $follower, ')
..write('rowid: $rowid')
Expand Down

0 comments on commit 9c21e17

Please sign in to comment.