From fe8611c5f49029650922832b5e50bbaed14eb962 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 27 Jan 2023 16:30:05 +0100 Subject: [PATCH] Avoid unecessary async in lambda --- drift_dev/CHANGELOG.md | 3 +- .../lib/src/writer/queries/query_writer.dart | 11 ++-- .../writer/queries/query_writer_test.dart | 59 +++++++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-) diff --git a/drift_dev/CHANGELOG.md b/drift_dev/CHANGELOG.md index 60dc3318..696a6324 100644 --- a/drift_dev/CHANGELOG.md +++ b/drift_dev/CHANGELOG.md @@ -7,7 +7,8 @@ - Support two different queries using `LIST()` columns having the same result class name. - Fix table classes not extending defining Dart classes with modular generation. - Fix `@UseDataClass` with `extending` not working with modular generation. -- Fix `drift_dev` generating invalid code when using a type converter with a nullable JSON type. +- Fix generating invalid code when using a type converter with a nullable JSON type. +- Avoid unecessary `async` modifier when mapping queries to existing row classes. ## 2.4.1 diff --git a/drift_dev/lib/src/writer/queries/query_writer.dart b/drift_dev/lib/src/writer/queries/query_writer.dart index 04401a0b..d3bacf0f 100644 --- a/drift_dev/lib/src/writer/queries/query_writer.dart +++ b/drift_dev/lib/src/writer/queries/query_writer.dart @@ -78,9 +78,10 @@ class QueryWriter { final resultSet = query.resultSet!; final queryRow = _emitter.drift('QueryRow'); final existingRowType = resultSet.existingRowType; + final asyncModifier = query.needsAsyncMapping ? 'async' : ''; if (existingRowType != null) { - _emitter.write('($queryRow row) async => '); + _emitter.write('($queryRow row) $asyncModifier => '); _writeArgumentExpression(existingRowType, resultSet); } else if (resultSet.singleColumn) { final column = resultSet.scalarColumns.single; @@ -97,11 +98,9 @@ class QueryWriter { _writeArgumentExpression(match, resultSet); } } else { - _buffer.write('($queryRow row) '); - if (query.needsAsyncMapping) { - _buffer.write('async '); - } - _buffer.write('{ return ${query.resultClassName}('); + _buffer + ..writeln('($queryRow row) $asyncModifier {') + ..write('return ${query.resultClassName}('); if (options.rawResultSetData) { _buffer.write('row: row,\n'); diff --git a/drift_dev/test/writer/queries/query_writer_test.dart b/drift_dev/test/writer/queries/query_writer_test.dart index 1d837b7d..688bcd22 100644 --- a/drift_dev/test/writer/queries/query_writer_test.dart +++ b/drift_dev/test/writer/queries/query_writer_test.dart @@ -228,4 +228,63 @@ class MyRow { result, ); }); + + test('can map to existing row class synchronously', () async { + // Regression test for https://github.com/simolus3/drift/issues/2282 + final result = await emulateDriftBuild( + inputs: { + 'a|lib/row.dart': ''' +class TestCustom { + final int testId; + final String testOneText; + final String testTwoText; + TestCustom({ + required this.testId, + required this.testOneText, + required this.testTwoText, + }); +} +''', + 'a|lib/a.drift': ''' +import 'row.dart'; + +CREATE TABLE TestOne ( + test_id INT NOT NULL, + test_one_text TEXT NOT NULL +); + +CREATE TABLE TestTwo ( + test_id INT NOT NULL, + test_two_text TEXT NOT NULL +); + +getTest WITH TestCustom: + SELECT + one.*, + two.test_two_text + FROM TestOne one + INNER JOIN TestTwo two + ON one.test_id = two.test_id; +''', + }, + modularBuild: true, + ); + + checkOutputs({ + 'a|lib/a.drift.dart': decodedMatches(contains( + ' i0.Selectable getTest() {\n' + ' return customSelect(\n' + ' \'SELECT one.*, two.test_two_text FROM TestOne AS one INNER JOIN TestTwo AS two ON one.test_id = two.test_id\',\n' + ' variables: [],\n' + ' readsFrom: {\n' + ' testTwo,\n' + ' testOne,\n' + ' }).map((i0.QueryRow row) => i3.TestCustom(\n' + ' testId: row.read(\'test_id\'),\n' + ' testOneText: row.read(\'test_one_text\'),\n' + ' testTwoText: row.read(\'test_two_text\'),\n' + ' ));\n' + ' }')), + }, result.dartOutputs, result); + }); }