mirror of https://github.com/AMT-Cheif/drift.git
Avoid nested codes, add tests
This commit is contained in:
parent
a5f184857d
commit
00dbc2f886
|
@ -17,7 +17,8 @@ class AnnotatedDartCode {
|
|||
|
||||
final List<dynamic /* String|DartTopLevelSymbol */ > elements;
|
||||
|
||||
AnnotatedDartCode(this.elements);
|
||||
AnnotatedDartCode(this.elements)
|
||||
: assert(elements.every((e) => e is String || e is DartTopLevelSymbol));
|
||||
|
||||
factory AnnotatedDartCode.ast(AstNode node) {
|
||||
return AnnotatedDartCode.build(((builder) => builder.addAstNode(node)));
|
||||
|
|
|
@ -112,7 +112,7 @@ abstract class _NodeOrWriter {
|
|||
: converter.fieldName;
|
||||
|
||||
return AnnotatedDartCode([
|
||||
entityInfoType(converter.owningColumn.owner),
|
||||
...entityInfoType(converter.owningColumn.owner).elements,
|
||||
'.$fieldName',
|
||||
]);
|
||||
}
|
||||
|
@ -198,9 +198,7 @@ abstract class _NodeOrWriter {
|
|||
final buffer = StringBuffer();
|
||||
|
||||
for (final lexeme in code.elements) {
|
||||
if (lexeme is AnnotatedDartCode) {
|
||||
buffer.write(dartCode(lexeme));
|
||||
} else if (lexeme is DartTopLevelSymbol) {
|
||||
if (lexeme is DartTopLevelSymbol) {
|
||||
final uri = lexeme.importUri;
|
||||
|
||||
if (uri != null) {
|
||||
|
|
|
@ -171,4 +171,26 @@ class MyDatabase {}
|
|||
|
||||
checkOutputs({}, result.dartOutputs, result);
|
||||
});
|
||||
|
||||
test('generates custom result classes with modular generation', () async {
|
||||
final logger = Logger.detached('driftBuild');
|
||||
expect(logger.onRecord, neverEmits(anything));
|
||||
|
||||
final result = await emulateDriftBuild(
|
||||
inputs: {
|
||||
'a|lib/main.drift': '''
|
||||
firstQuery AS MyResultClass: SELECT 'foo' AS r1, 1 AS r2;
|
||||
secondQuery AS MyResultClass: SELECT 'bar' AS r1, 2 AS r2;
|
||||
''',
|
||||
},
|
||||
modularBuild: true,
|
||||
logger: logger,
|
||||
);
|
||||
|
||||
checkOutputs({
|
||||
'a|lib/main.drift.dart': decodedMatches(predicate((String generated) {
|
||||
return 'class MyResultClass'.allMatches(generated).length == 1;
|
||||
})),
|
||||
}, result.dartOutputs, result);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
import 'package:build_test/build_test.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../utils.dart';
|
||||
|
||||
void main() {
|
||||
group('modular writer', () {
|
||||
test('generates import for type converter', () async {
|
||||
final result = await emulateDriftBuild(
|
||||
inputs: {
|
||||
'a|lib/converter.dart': '''
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
TypeConverter<String, String> get myConverter => throw UnimplementedError();
|
||||
''',
|
||||
'a|lib/table.drift': '''
|
||||
import 'converter.dart';
|
||||
|
||||
CREATE TABLE my_table (
|
||||
foo TEXT MAPPED BY `myConverter`,
|
||||
bar INT
|
||||
);
|
||||
''',
|
||||
'a|lib/query.drift': '''
|
||||
import 'table.drift';
|
||||
|
||||
foo: SELECT foo FROM my_table;
|
||||
''',
|
||||
},
|
||||
modularBuild: true,
|
||||
);
|
||||
|
||||
checkOutputs({
|
||||
'a|lib/table.drift.dart': decodedMatches(
|
||||
allOf(
|
||||
contains("import 'package:a/converter.dart' as i2;"),
|
||||
contains(r'$converterfoo = i2.myConverter;'),
|
||||
),
|
||||
),
|
||||
'a|lib/query.drift.dart': decodedMatches(
|
||||
allOf(
|
||||
contains("import 'package:a/converter.dart';"),
|
||||
contains(r'i2.MyTable.$converterfoo'),
|
||||
),
|
||||
)
|
||||
}, result.dartOutputs, result);
|
||||
});
|
||||
});
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
// ignore_for_file: type=lint
|
||||
import 'package:drift/drift.dart' as i0;
|
||||
import 'package:drift/src/runtime/api/runtime_api.dart' as i1;
|
||||
import 'package:modular/database.dart' as i1;
|
||||
import 'package:modular/src/users.drift.dart' as i2;
|
||||
import 'package:drift/internal/modular.dart' as i3;
|
||||
import 'package:modular/src/user_queries.drift.dart' as i4;
|
||||
|
||||
mixin $MyAccessorMixin on i0.DatabaseAccessor<i1.GeneratedDatabase> {
|
||||
mixin $MyAccessorMixin on i0.DatabaseAccessor<i1.Database> {
|
||||
i2.Users get users =>
|
||||
i3.ReadDatabaseContainer(attachedDatabase).resultSet('users');
|
||||
i2.Follows get follows =>
|
||||
|
|
|
@ -4,7 +4,7 @@ import 'package:modular/src/users.drift.dart' as i1;
|
|||
import 'package:modular/src/posts.drift.dart' as i2;
|
||||
import 'package:modular/src/search.drift.dart' as i3;
|
||||
import 'package:modular/accessor.dart' as i4;
|
||||
import 'package:drift/src/runtime/api/runtime_api.dart' as i5;
|
||||
import 'package:modular/database.dart' as i5;
|
||||
import 'package:modular/src/user_queries.drift.dart' as i6;
|
||||
import 'package:drift/internal/modular.dart' as i7;
|
||||
|
||||
|
@ -16,8 +16,7 @@ abstract class $Database extends i0.GeneratedDatabase {
|
|||
late final i2.Likes likes = i2.Likes(this);
|
||||
late final i1.Follows follows = i1.Follows(this);
|
||||
late final i1.PopularUsers popularUsers = i1.PopularUsers(this);
|
||||
late final i4.MyAccessor myAccessor =
|
||||
i4.MyAccessor(this as i5.GeneratedDatabase);
|
||||
late final i4.MyAccessor myAccessor = i4.MyAccessor(this as i5.Database);
|
||||
i6.UserQueriesDrift get userQueriesDrift => i7.ReadDatabaseContainer(this)
|
||||
.accessor<i6.UserQueriesDrift>(i6.UserQueriesDrift.new);
|
||||
i3.SearchDrift get searchDrift => i7.ReadDatabaseContainer(this)
|
||||
|
|
|
@ -19,7 +19,7 @@ class User extends i0.DataClass implements i0.Insertable<i1.User> {
|
|||
map['biography'] = i0.Variable<String>(biography);
|
||||
}
|
||||
if (!nullToAbsent || preferences != null) {
|
||||
final converter = Users.$converterpreferencesn;
|
||||
final converter = i1.Users.$converterpreferencesn;
|
||||
map['preferences'] = i0.Variable<String>(converter.toSql(preferences));
|
||||
}
|
||||
return map;
|
||||
|
@ -45,7 +45,7 @@ class User extends i0.DataClass implements i0.Insertable<i1.User> {
|
|||
id: serializer.fromJson<int>(json['id']),
|
||||
name: serializer.fromJson<String>(json['name']),
|
||||
biography: serializer.fromJson<String?>(json['biography']),
|
||||
preferences: Users.$converterpreferencesn.fromJson(
|
||||
preferences: i1.Users.$converterpreferencesn.fromJson(
|
||||
serializer.fromJson<Map<String, Object?>?>(json['preferences'])),
|
||||
);
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ class User extends i0.DataClass implements i0.Insertable<i1.User> {
|
|||
'name': serializer.toJson<String>(name),
|
||||
'biography': serializer.toJson<String?>(biography),
|
||||
'preferences': serializer.toJson<Map<String, Object?>?>(
|
||||
Users.$converterpreferencesn.toJson(preferences)),
|
||||
i1.Users.$converterpreferencesn.toJson(preferences)),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -152,7 +152,7 @@ class UsersCompanion extends i0.UpdateCompanion<i1.User> {
|
|||
map['biography'] = i0.Variable<String>(biography.value);
|
||||
}
|
||||
if (preferences.present) {
|
||||
final converter = Users.$converterpreferencesn;
|
||||
final converter = i1.Users.$converterpreferencesn;
|
||||
map['preferences'] =
|
||||
i0.Variable<String>(converter.toSql(preferences.value));
|
||||
}
|
||||
|
@ -203,7 +203,7 @@ class Users extends i0.Table with i0.TableInfo<Users, i1.User> {
|
|||
type: i0.DriftSqlType.string,
|
||||
requiredDuringInsert: false,
|
||||
$customConstraints: '')
|
||||
.withConverter<i2.Preferences?>(Users.$converterpreferencesn);
|
||||
.withConverter<i2.Preferences?>(i1.Users.$converterpreferencesn);
|
||||
@override
|
||||
List<i0.GeneratedColumn> get $columns => [id, name, biography, preferences];
|
||||
@override
|
||||
|
@ -244,7 +244,7 @@ class Users extends i0.Table with i0.TableInfo<Users, i1.User> {
|
|||
.read(i0.DriftSqlType.string, data['${effectivePrefix}name'])!,
|
||||
biography: attachedDatabase.typeMapping
|
||||
.read(i0.DriftSqlType.string, data['${effectivePrefix}biography']),
|
||||
preferences: Users.$converterpreferencesn.fromSql(attachedDatabase
|
||||
preferences: i1.Users.$converterpreferencesn.fromSql(attachedDatabase
|
||||
.typeMapping
|
||||
.read(i0.DriftSqlType.string, data['${effectivePrefix}preferences'])),
|
||||
);
|
||||
|
@ -464,7 +464,7 @@ class PopularUser extends i0.DataClass {
|
|||
id: serializer.fromJson<int>(json['id']),
|
||||
name: serializer.fromJson<String>(json['name']),
|
||||
biography: serializer.fromJson<String?>(json['biography']),
|
||||
preferences: Users.$converterpreferencesn.fromJson(
|
||||
preferences: i1.Users.$converterpreferencesn.fromJson(
|
||||
serializer.fromJson<Map<String, Object?>?>(json['preferences'])),
|
||||
);
|
||||
}
|
||||
|
@ -476,7 +476,7 @@ class PopularUser extends i0.DataClass {
|
|||
'name': serializer.toJson<String>(name),
|
||||
'biography': serializer.toJson<String?>(biography),
|
||||
'preferences': serializer.toJson<Map<String, Object?>?>(
|
||||
Users.$converterpreferencesn.toJson(preferences)),
|
||||
i1.Users.$converterpreferencesn.toJson(preferences)),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -541,7 +541,7 @@ class PopularUsers extends i0.ViewInfo<i1.PopularUsers, i1.PopularUser>
|
|||
.read(i0.DriftSqlType.string, data['${effectivePrefix}name'])!,
|
||||
biography: attachedDatabase.typeMapping
|
||||
.read(i0.DriftSqlType.string, data['${effectivePrefix}biography']),
|
||||
preferences: Users.$converterpreferencesn.fromSql(attachedDatabase
|
||||
preferences: i1.Users.$converterpreferencesn.fromSql(attachedDatabase
|
||||
.typeMapping
|
||||
.read(i0.DriftSqlType.string, data['${effectivePrefix}preferences'])),
|
||||
);
|
||||
|
@ -559,7 +559,7 @@ class PopularUsers extends i0.ViewInfo<i1.PopularUsers, i1.PopularUser>
|
|||
late final i0.GeneratedColumnWithTypeConverter<i2.Preferences?, String>
|
||||
preferences = i0.GeneratedColumn<String>('preferences', aliasedName, true,
|
||||
type: i0.DriftSqlType.string)
|
||||
.withConverter<i2.Preferences?>(Users.$converterpreferencesn);
|
||||
.withConverter<i2.Preferences?>(i1.Users.$converterpreferencesn);
|
||||
@override
|
||||
PopularUsers createAlias(String alias) {
|
||||
return PopularUsers(attachedDatabase, alias);
|
||||
|
|
Loading…
Reference in New Issue