From a064bb719dabd460f4a144cbf825a8d10cdbc0f8 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 1 Jul 2022 21:51:33 +0200 Subject: [PATCH] Migrate remaining examples to null-safety --- .../with_built_value/lib/database.drift.dart | 68 +++++++-------- examples/with_built_value/lib/database.g.dart | 14 +-- examples/with_built_value/pubspec.yaml | 6 +- extras/integration_tests/web/pubspec.yaml | 8 +- ...ves_after_migration_regression_test.g.dart | 86 +++++++++---------- 5 files changed, 85 insertions(+), 97 deletions(-) diff --git a/examples/with_built_value/lib/database.drift.dart b/examples/with_built_value/lib/database.drift.dart index 416a3df8..53c1c8d8 100644 --- a/examples/with_built_value/lib/database.drift.dart +++ b/examples/with_built_value/lib/database.drift.dart @@ -10,36 +10,33 @@ part of 'database.dart'; class User extends DataClass implements Insertable { final int id; final String name; - User({@required this.id, @required this.name}); - factory User.fromData(Map data, {String prefix}) { + User({required this.id, required this.name}); + factory User.fromData(Map data, {String? prefix}) { final effectivePrefix = prefix ?? ''; return User( - id: const IntType().mapFromDatabaseResponse(data['${effectivePrefix}id']), + id: const IntType() + .mapFromDatabaseResponse(data['${effectivePrefix}id'])!, name: const StringType() - .mapFromDatabaseResponse(data['${effectivePrefix}name']), + .mapFromDatabaseResponse(data['${effectivePrefix}name'])!, ); } @override Map toColumns(bool nullToAbsent) { final map = {}; - if (!nullToAbsent || id != null) { - map['id'] = Variable(id); - } - if (!nullToAbsent || name != null) { - map['name'] = Variable(name); - } + map['id'] = Variable(id); + map['name'] = Variable(name); return map; } UsersCompanion toCompanion(bool nullToAbsent) { return UsersCompanion( - id: id == null && nullToAbsent ? const Value.absent() : Value(id), - name: name == null && nullToAbsent ? const Value.absent() : Value(name), + id: Value(id), + name: Value(name), ); } factory User.fromJson(Map json, - {ValueSerializer serializer}) { + {ValueSerializer? serializer}) { serializer ??= driftRuntimeOptions.defaultSerializer; return User( id: serializer.fromJson(json['id']), @@ -47,7 +44,7 @@ class User extends DataClass implements Insertable { ); } @override - Map toJson({ValueSerializer serializer}) { + Map toJson({ValueSerializer? serializer}) { serializer ??= driftRuntimeOptions.defaultSerializer; return { 'id': serializer.toJson(id), @@ -55,7 +52,7 @@ class User extends DataClass implements Insertable { }; } - User copyWith({int id, String name}) => User( + User copyWith({int? id, String? name}) => User( id: id ?? this.id, name: name ?? this.name, ); @@ -85,11 +82,11 @@ class UsersCompanion extends UpdateCompanion { }); UsersCompanion.insert({ this.id = const Value.absent(), - @required String name, + required String name, }) : name = Value(name); static Insertable custom({ - Expression id, - Expression name, + Expression? id, + Expression? name, }) { return RawValuesInsertable({ if (id != null) 'id': id, @@ -97,7 +94,7 @@ class UsersCompanion extends UpdateCompanion { }); } - UsersCompanion copyWith({Value id, Value name}) { + UsersCompanion copyWith({Value? id, Value? name}) { return UsersCompanion( id: id ?? this.id, name: name ?? this.name, @@ -129,22 +126,20 @@ class UsersCompanion extends UpdateCompanion { class Users extends Table with TableInfo { @override final GeneratedDatabase attachedDatabase; - final String _alias; + final String? _alias; Users(this.attachedDatabase, [this._alias]); final VerificationMeta _idMeta = const VerificationMeta('id'); - GeneratedColumn _id; - GeneratedColumn get id => - _id ??= GeneratedColumn('id', aliasedName, false, - type: const IntType(), - requiredDuringInsert: false, - $customConstraints: 'NOT NULL PRIMARY KEY AUTOINCREMENT'); + late final GeneratedColumn id = GeneratedColumn( + 'id', aliasedName, false, + type: const IntType(), + requiredDuringInsert: false, + $customConstraints: 'NOT NULL PRIMARY KEY AUTOINCREMENT'); final VerificationMeta _nameMeta = const VerificationMeta('name'); - GeneratedColumn _name; - GeneratedColumn get name => - _name ??= GeneratedColumn('name', aliasedName, false, - type: const StringType(), - requiredDuringInsert: true, - $customConstraints: 'NOT NULL'); + late final GeneratedColumn name = GeneratedColumn( + 'name', aliasedName, false, + type: const StringType(), + requiredDuringInsert: true, + $customConstraints: 'NOT NULL'); @override List get $columns => [id, name]; @override @@ -157,11 +152,11 @@ class Users extends Table with TableInfo { final context = VerificationContext(); final data = instance.toColumns(true); if (data.containsKey('id')) { - context.handle(_idMeta, id.isAcceptableOrUnknown(data['id'], _idMeta)); + context.handle(_idMeta, id.isAcceptableOrUnknown(data['id']!, _idMeta)); } if (data.containsKey('name')) { context.handle( - _nameMeta, name.isAcceptableOrUnknown(data['name'], _nameMeta)); + _nameMeta, name.isAcceptableOrUnknown(data['name']!, _nameMeta)); } else if (isInserting) { context.missing(_nameMeta); } @@ -171,7 +166,7 @@ class Users extends Table with TableInfo { @override Set get $primaryKey => {id}; @override - User map(Map data, {String tablePrefix}) { + User map(Map data, {String? tablePrefix}) { return User.fromData(data, prefix: tablePrefix != null ? '$tablePrefix.' : null); } @@ -187,8 +182,7 @@ class Users extends Table with TableInfo { abstract class _$Database extends GeneratedDatabase { _$Database(QueryExecutor e) : super(SqlTypeSystem.defaultInstance, e); - Users _users; - Users get users => _users ??= Users(this); + late final Users users = Users(this); @override Iterable get allTables => allSchemaEntities.whereType(); @override diff --git a/examples/with_built_value/lib/database.g.dart b/examples/with_built_value/lib/database.g.dart index 0471981e..28427078 100644 --- a/examples/with_built_value/lib/database.g.dart +++ b/examples/with_built_value/lib/database.g.dart @@ -10,10 +10,10 @@ class _$Foo extends Foo { @override final User moorField; - factory _$Foo([void Function(FooBuilder) updates]) => + factory _$Foo([void Function(FooBuilder)? updates]) => (new FooBuilder()..update(updates))._build(); - _$Foo._({this.moorField}) : super._() { + _$Foo._({required this.moorField}) : super._() { BuiltValueNullFieldError.checkNotNull(moorField, r'Foo', 'moorField'); } @@ -43,11 +43,11 @@ class _$Foo extends Foo { } class FooBuilder implements Builder { - _$Foo _$v; + _$Foo? _$v; - User _moorField; - User get moorField => _$this._moorField; - set moorField(User moorField) => _$this._moorField = moorField; + User? _moorField; + User? get moorField => _$this._moorField; + set moorField(User? moorField) => _$this._moorField = moorField; FooBuilder(); @@ -67,7 +67,7 @@ class FooBuilder implements Builder { } @override - void update(void Function(FooBuilder) updates) { + void update(void Function(FooBuilder)? updates) { if (updates != null) updates(this); } diff --git a/examples/with_built_value/pubspec.yaml b/examples/with_built_value/pubspec.yaml index b3ee261a..8d476633 100644 --- a/examples/with_built_value/pubspec.yaml +++ b/examples/with_built_value/pubspec.yaml @@ -3,14 +3,14 @@ publish_to: none version: 1.0.0 environment: - sdk: '>=2.6.0 <3.0.0' + sdk: '>=2.12.0 <3.0.0' dependencies: - drift: ^1.0.0 + drift: ^2.0.0-dev built_value: ^8.0.0 dev_dependencies: - drift_dev: ^1.0.0 + drift_dev: ^2.0.0-dev built_value_generator: ^8.1.1 build_runner: ^2.0.0 diff --git a/extras/integration_tests/web/pubspec.yaml b/extras/integration_tests/web/pubspec.yaml index 834b41f6..cf4754ba 100644 --- a/extras/integration_tests/web/pubspec.yaml +++ b/extras/integration_tests/web/pubspec.yaml @@ -1,18 +1,18 @@ name: web description: Run integration tests for Moor on the web environment: - sdk: '>=2.6.0 <3.0.0' + sdk: '>=2.12.0 <3.0.0' dependencies: - drift: + drift: ^2.0.0-dev drift_testcases: path: ../drift_testcases dev_dependencies: - test: ^1.5.0 + test: ^1.18.0 build_runner: build_web_compilers: - drift_dev: ^1.0.0 + drift_dev: ^2.0.0-dev dependency_overrides: drift: diff --git a/extras/integration_tests/web/test/saves_after_migration_regression_test.g.dart b/extras/integration_tests/web/test/saves_after_migration_regression_test.g.dart index 899c60af..7af91134 100644 --- a/extras/integration_tests/web/test/saves_after_migration_regression_test.g.dart +++ b/extras/integration_tests/web/test/saves_after_migration_regression_test.g.dart @@ -9,44 +9,43 @@ part of 'saves_after_migration_regression_test.dart'; // ignore_for_file: type=lint class Foo extends DataClass implements Insertable { final int id; - Foo({@required this.id}); - factory Foo.fromData(Map data, {String prefix}) { + Foo({required this.id}); + factory Foo.fromData(Map data, {String? prefix}) { final effectivePrefix = prefix ?? ''; return Foo( - id: const IntType().mapFromDatabaseResponse(data['${effectivePrefix}id']), + id: const IntType() + .mapFromDatabaseResponse(data['${effectivePrefix}id'])!, ); } @override Map toColumns(bool nullToAbsent) { final map = {}; - if (!nullToAbsent || id != null) { - map['id'] = Variable(id); - } + map['id'] = Variable(id); return map; } FoosCompanion toCompanion(bool nullToAbsent) { return FoosCompanion( - id: id == null && nullToAbsent ? const Value.absent() : Value(id), + id: Value(id), ); } factory Foo.fromJson(Map json, - {ValueSerializer serializer}) { + {ValueSerializer? serializer}) { serializer ??= driftRuntimeOptions.defaultSerializer; return Foo( id: serializer.fromJson(json['id']), ); } @override - Map toJson({ValueSerializer serializer}) { + Map toJson({ValueSerializer? serializer}) { serializer ??= driftRuntimeOptions.defaultSerializer; return { 'id': serializer.toJson(id), }; } - Foo copyWith({int id}) => Foo( + Foo copyWith({int? id}) => Foo( id: id ?? this.id, ); @override @@ -73,14 +72,14 @@ class FoosCompanion extends UpdateCompanion { this.id = const Value.absent(), }); static Insertable custom({ - Expression id, + Expression? id, }) { return RawValuesInsertable({ if (id != null) 'id': id, }); } - FoosCompanion copyWith({Value id}) { + FoosCompanion copyWith({Value? id}) { return FoosCompanion( id: id ?? this.id, ); @@ -107,16 +106,15 @@ class FoosCompanion extends UpdateCompanion { class $FoosTable extends Foos with TableInfo<$FoosTable, Foo> { @override final GeneratedDatabase attachedDatabase; - final String _alias; + final String? _alias; $FoosTable(this.attachedDatabase, [this._alias]); final VerificationMeta _idMeta = const VerificationMeta('id'); - GeneratedColumn _id; @override - GeneratedColumn get id => - _id ??= GeneratedColumn('id', aliasedName, false, - type: const IntType(), - requiredDuringInsert: false, - defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); + late final GeneratedColumn id = GeneratedColumn( + 'id', aliasedName, false, + type: const IntType(), + requiredDuringInsert: false, + defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); @override List get $columns => [id]; @override @@ -129,7 +127,7 @@ class $FoosTable extends Foos with TableInfo<$FoosTable, Foo> { final context = VerificationContext(); final data = instance.toColumns(true); if (data.containsKey('id')) { - context.handle(_idMeta, id.isAcceptableOrUnknown(data['id'], _idMeta)); + context.handle(_idMeta, id.isAcceptableOrUnknown(data['id']!, _idMeta)); } return context; } @@ -137,7 +135,7 @@ class $FoosTable extends Foos with TableInfo<$FoosTable, Foo> { @override Set get $primaryKey => {id}; @override - Foo map(Map data, {String tablePrefix}) { + Foo map(Map data, {String? tablePrefix}) { return Foo.fromData(data, prefix: tablePrefix != null ? '$tablePrefix.' : null); } @@ -150,44 +148,43 @@ class $FoosTable extends Foos with TableInfo<$FoosTable, Foo> { class Bar extends DataClass implements Insertable { final int id; - Bar({@required this.id}); - factory Bar.fromData(Map data, {String prefix}) { + Bar({required this.id}); + factory Bar.fromData(Map data, {String? prefix}) { final effectivePrefix = prefix ?? ''; return Bar( - id: const IntType().mapFromDatabaseResponse(data['${effectivePrefix}id']), + id: const IntType() + .mapFromDatabaseResponse(data['${effectivePrefix}id'])!, ); } @override Map toColumns(bool nullToAbsent) { final map = {}; - if (!nullToAbsent || id != null) { - map['id'] = Variable(id); - } + map['id'] = Variable(id); return map; } BarsCompanion toCompanion(bool nullToAbsent) { return BarsCompanion( - id: id == null && nullToAbsent ? const Value.absent() : Value(id), + id: Value(id), ); } factory Bar.fromJson(Map json, - {ValueSerializer serializer}) { + {ValueSerializer? serializer}) { serializer ??= driftRuntimeOptions.defaultSerializer; return Bar( id: serializer.fromJson(json['id']), ); } @override - Map toJson({ValueSerializer serializer}) { + Map toJson({ValueSerializer? serializer}) { serializer ??= driftRuntimeOptions.defaultSerializer; return { 'id': serializer.toJson(id), }; } - Bar copyWith({int id}) => Bar( + Bar copyWith({int? id}) => Bar( id: id ?? this.id, ); @override @@ -214,14 +211,14 @@ class BarsCompanion extends UpdateCompanion { this.id = const Value.absent(), }); static Insertable custom({ - Expression id, + Expression? id, }) { return RawValuesInsertable({ if (id != null) 'id': id, }); } - BarsCompanion copyWith({Value id}) { + BarsCompanion copyWith({Value? id}) { return BarsCompanion( id: id ?? this.id, ); @@ -248,16 +245,15 @@ class BarsCompanion extends UpdateCompanion { class $BarsTable extends Bars with TableInfo<$BarsTable, Bar> { @override final GeneratedDatabase attachedDatabase; - final String _alias; + final String? _alias; $BarsTable(this.attachedDatabase, [this._alias]); final VerificationMeta _idMeta = const VerificationMeta('id'); - GeneratedColumn _id; @override - GeneratedColumn get id => - _id ??= GeneratedColumn('id', aliasedName, false, - type: const IntType(), - requiredDuringInsert: false, - defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); + late final GeneratedColumn id = GeneratedColumn( + 'id', aliasedName, false, + type: const IntType(), + requiredDuringInsert: false, + defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); @override List get $columns => [id]; @override @@ -270,7 +266,7 @@ class $BarsTable extends Bars with TableInfo<$BarsTable, Bar> { final context = VerificationContext(); final data = instance.toColumns(true); if (data.containsKey('id')) { - context.handle(_idMeta, id.isAcceptableOrUnknown(data['id'], _idMeta)); + context.handle(_idMeta, id.isAcceptableOrUnknown(data['id']!, _idMeta)); } return context; } @@ -278,7 +274,7 @@ class $BarsTable extends Bars with TableInfo<$BarsTable, Bar> { @override Set get $primaryKey => {id}; @override - Bar map(Map data, {String tablePrefix}) { + Bar map(Map data, {String? tablePrefix}) { return Bar.fromData(data, prefix: tablePrefix != null ? '$tablePrefix.' : null); } @@ -291,10 +287,8 @@ class $BarsTable extends Bars with TableInfo<$BarsTable, Bar> { abstract class _$_FakeDb extends GeneratedDatabase { _$_FakeDb(QueryExecutor e) : super(SqlTypeSystem.defaultInstance, e); - $FoosTable _foos; - $FoosTable get foos => _foos ??= $FoosTable(this); - $BarsTable _bars; - $BarsTable get bars => _bars ??= $BarsTable(this); + late final $FoosTable foos = $FoosTable(this); + late final $BarsTable bars = $BarsTable(this); @override Iterable get allTables => allSchemaEntities.whereType(); @override