From 835e31c8eddf0b9ed55c6b9e89848a3e48c8a5e7 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Wed, 3 Jun 2020 19:07:53 +0200 Subject: [PATCH] Add `this.` prefix to avoid name clashes (#613) --- moor/test/data/tables/custom_tables.g.dart | 78 ++++++++++--------- moor/test/data/tables/tables.moor | 2 +- .../moor_files_integration_test.dart | 2 +- .../lib/src/writer/tables/table_writer.dart | 4 +- .../tables/update_companion_writer.dart | 10 ++- moor_generator/lib/src/writer/writer.dart | 12 +++ 6 files changed, 67 insertions(+), 41 deletions(-) diff --git a/moor/test/data/tables/custom_tables.g.dart b/moor/test/data/tables/custom_tables.g.dart index 36448310..6cf31109 100644 --- a/moor/test/data/tables/custom_tables.g.dart +++ b/moor/test/data/tables/custom_tables.g.dart @@ -876,10 +876,10 @@ class WithConstraints extends Table class MytableData extends DataClass implements Insertable { final int someid; final String sometext; - final bool somebool; + final bool isInserting; final DateTime somedate; MytableData( - {@required this.someid, this.sometext, this.somebool, this.somedate}); + {@required this.someid, this.sometext, this.isInserting, this.somedate}); factory MytableData.fromData(Map data, GeneratedDatabase db, {String prefix}) { final effectivePrefix = prefix ?? ''; @@ -891,8 +891,8 @@ class MytableData extends DataClass implements Insertable { someid: intType.mapFromDatabaseResponse(data['${effectivePrefix}someid']), sometext: stringType .mapFromDatabaseResponse(data['${effectivePrefix}sometext']), - somebool: - boolType.mapFromDatabaseResponse(data['${effectivePrefix}somebool']), + isInserting: boolType + .mapFromDatabaseResponse(data['${effectivePrefix}is_inserting']), somedate: dateTimeType .mapFromDatabaseResponse(data['${effectivePrefix}somedate']), ); @@ -906,8 +906,8 @@ class MytableData extends DataClass implements Insertable { if (!nullToAbsent || sometext != null) { map['sometext'] = Variable(sometext); } - if (!nullToAbsent || somebool != null) { - map['somebool'] = Variable(somebool); + if (!nullToAbsent || isInserting != null) { + map['is_inserting'] = Variable(isInserting); } if (!nullToAbsent || somedate != null) { map['somedate'] = Variable(somedate); @@ -922,9 +922,9 @@ class MytableData extends DataClass implements Insertable { sometext: sometext == null && nullToAbsent ? const Value.absent() : Value(sometext), - somebool: somebool == null && nullToAbsent + isInserting: isInserting == null && nullToAbsent ? const Value.absent() - : Value(somebool), + : Value(isInserting), somedate: somedate == null && nullToAbsent ? const Value.absent() : Value(somedate), @@ -937,7 +937,7 @@ class MytableData extends DataClass implements Insertable { return MytableData( someid: serializer.fromJson(json['someid']), sometext: serializer.fromJson(json['sometext']), - somebool: serializer.fromJson(json['somebool']), + isInserting: serializer.fromJson(json['is_inserting']), somedate: serializer.fromJson(json['somedate']), ); } @@ -952,17 +952,17 @@ class MytableData extends DataClass implements Insertable { return { 'someid': serializer.toJson(someid), 'sometext': serializer.toJson(sometext), - 'somebool': serializer.toJson(somebool), + 'is_inserting': serializer.toJson(isInserting), 'somedate': serializer.toJson(somedate), }; } MytableData copyWith( - {int someid, String sometext, bool somebool, DateTime somedate}) => + {int someid, String sometext, bool isInserting, DateTime somedate}) => MytableData( someid: someid ?? this.someid, sometext: sometext ?? this.sometext, - somebool: somebool ?? this.somebool, + isInserting: isInserting ?? this.isInserting, somedate: somedate ?? this.somedate, ); @override @@ -970,52 +970,54 @@ class MytableData extends DataClass implements Insertable { return (StringBuffer('MytableData(') ..write('someid: $someid, ') ..write('sometext: $sometext, ') - ..write('somebool: $somebool, ') + ..write('isInserting: $isInserting, ') ..write('somedate: $somedate') ..write(')')) .toString(); } @override - int get hashCode => $mrjf($mrjc(someid.hashCode, - $mrjc(sometext.hashCode, $mrjc(somebool.hashCode, somedate.hashCode)))); + int get hashCode => $mrjf($mrjc( + someid.hashCode, + $mrjc( + sometext.hashCode, $mrjc(isInserting.hashCode, somedate.hashCode)))); @override bool operator ==(dynamic other) => identical(this, other) || (other is MytableData && other.someid == this.someid && other.sometext == this.sometext && - other.somebool == this.somebool && + other.isInserting == this.isInserting && other.somedate == this.somedate); } class MytableCompanion extends UpdateCompanion { final Value someid; final Value sometext; - final Value somebool; + final Value isInserting; final Value somedate; const MytableCompanion({ this.someid = const Value.absent(), this.sometext = const Value.absent(), - this.somebool = const Value.absent(), + this.isInserting = const Value.absent(), this.somedate = const Value.absent(), }); MytableCompanion.insert({ this.someid = const Value.absent(), this.sometext = const Value.absent(), - this.somebool = const Value.absent(), + this.isInserting = const Value.absent(), this.somedate = const Value.absent(), }); static Insertable custom({ Expression someid, Expression sometext, - Expression somebool, + Expression isInserting, Expression somedate, }) { return RawValuesInsertable({ if (someid != null) 'someid': someid, if (sometext != null) 'sometext': sometext, - if (somebool != null) 'somebool': somebool, + if (isInserting != null) 'is_inserting': isInserting, if (somedate != null) 'somedate': somedate, }); } @@ -1023,12 +1025,12 @@ class MytableCompanion extends UpdateCompanion { MytableCompanion copyWith( {Value someid, Value sometext, - Value somebool, + Value isInserting, Value somedate}) { return MytableCompanion( someid: someid ?? this.someid, sometext: sometext ?? this.sometext, - somebool: somebool ?? this.somebool, + isInserting: isInserting ?? this.isInserting, somedate: somedate ?? this.somedate, ); } @@ -1042,8 +1044,8 @@ class MytableCompanion extends UpdateCompanion { if (sometext.present) { map['sometext'] = Variable(sometext.value); } - if (somebool.present) { - map['somebool'] = Variable(somebool.value); + if (isInserting.present) { + map['is_inserting'] = Variable(isInserting.value); } if (somedate.present) { map['somedate'] = Variable(somedate.value); @@ -1056,7 +1058,7 @@ class MytableCompanion extends UpdateCompanion { return (StringBuffer('MytableCompanion(') ..write('someid: $someid, ') ..write('sometext: $sometext, ') - ..write('somebool: $somebool, ') + ..write('isInserting: $isInserting, ') ..write('somedate: $somedate') ..write(')')) .toString(); @@ -1083,11 +1085,13 @@ class Mytable extends Table with TableInfo { $customConstraints: ''); } - final VerificationMeta _someboolMeta = const VerificationMeta('somebool'); - GeneratedBoolColumn _somebool; - GeneratedBoolColumn get somebool => _somebool ??= _constructSomebool(); - GeneratedBoolColumn _constructSomebool() { - return GeneratedBoolColumn('somebool', $tableName, true, + final VerificationMeta _isInsertingMeta = + const VerificationMeta('isInserting'); + GeneratedBoolColumn _isInserting; + GeneratedBoolColumn get isInserting => + _isInserting ??= _constructIsInserting(); + GeneratedBoolColumn _constructIsInserting() { + return GeneratedBoolColumn('is_inserting', $tableName, true, $customConstraints: ''); } @@ -1100,7 +1104,8 @@ class Mytable extends Table with TableInfo { } @override - List get $columns => [someid, sometext, somebool, somedate]; + List get $columns => + [someid, sometext, isInserting, somedate]; @override Mytable get asDslTable => this; @override @@ -1120,9 +1125,12 @@ class Mytable extends Table with TableInfo { context.handle(_sometextMeta, sometext.isAcceptableOrUnknown(data['sometext'], _sometextMeta)); } - if (data.containsKey('somebool')) { - context.handle(_someboolMeta, - somebool.isAcceptableOrUnknown(data['somebool'], _someboolMeta)); + if (data.containsKey('is_inserting')) { + context.handle( + _isInsertingMeta, + this + .isInserting + .isAcceptableOrUnknown(data['is_inserting'], _isInsertingMeta)); } if (data.containsKey('somedate')) { context.handle(_somedateMeta, diff --git a/moor/test/data/tables/tables.moor b/moor/test/data/tables/tables.moor index 3a2a6078..43a4a996 100644 --- a/moor/test/data/tables/tables.moor +++ b/moor/test/data/tables/tables.moor @@ -29,7 +29,7 @@ CREATE INDEX IF NOT EXISTS value_idx ON config (config_value); CREATE TABLE mytable ( someid INTEGER NOT NULL PRIMARY KEY, sometext TEXT, - somebool BOOLEAN, + is_inserting BOOLEAN, somedate DATETIME ); diff --git a/moor/test/parsed_sql/moor_files_integration_test.dart b/moor/test/parsed_sql/moor_files_integration_test.dart index f3bef569..47e23eaa 100644 --- a/moor/test/parsed_sql/moor_files_integration_test.dart +++ b/moor/test/parsed_sql/moor_files_integration_test.dart @@ -26,7 +26,7 @@ const _createConfig = 'CREATE TABLE IF NOT EXISTS config (' const _createMyTable = 'CREATE TABLE IF NOT EXISTS mytable (' 'someid INTEGER NOT NULL PRIMARY KEY, ' 'sometext VARCHAR, ' - 'somebool INTEGER, ' + 'is_inserting INTEGER, ' 'somedate INTEGER);'; const _createEmail = 'CREATE VIRTUAL TABLE IF NOT EXISTS email USING ' diff --git a/moor_generator/lib/src/writer/tables/table_writer.dart b/moor_generator/lib/src/writer/tables/table_writer.dart index 162e06a4..0d4b7f71 100644 --- a/moor_generator/lib/src/writer/tables/table_writer.dart +++ b/moor_generator/lib/src/writer/tables/table_writer.dart @@ -182,8 +182,10 @@ class TableWriter { ..write('final context = VerificationContext();\n') ..write('final data = instance.toColumns(true);\n'); + const locals = {'instance', 'isInserting', 'context', 'data'}; + for (final column in table.columns) { - final getterName = column.dartGetterName; + final getterName = column.thisIfNeeded(locals); final metaName = _fieldNameForColumnMeta(column); if (column.typeConverter != null) { diff --git a/moor_generator/lib/src/writer/tables/update_companion_writer.dart b/moor_generator/lib/src/writer/tables/update_companion_writer.dart index be39ec51..f743c4e4 100644 --- a/moor_generator/lib/src/writer/tables/update_companion_writer.dart +++ b/moor_generator/lib/src/writer/tables/update_companion_writer.dart @@ -154,8 +154,12 @@ class UpdateCompanionWriter { '(bool nullToAbsent) {\n') ..write('final map = {};'); + const locals = {'map', 'nullToAbsent'}; + for (final column in table.columns) { - _buffer.write('if (${column.dartGetterName}.present) {'); + final getterName = column.thisIfNeeded(locals); + + _buffer.write('if ($getterName.present) {'); final mapSetter = 'map[${asDartLiteral(column.name.name)}] = ' 'Variable<${column.variableTypeName}>'; @@ -166,13 +170,13 @@ class UpdateCompanionWriter { _buffer ..write('final converter = $fieldName;\n') ..write(mapSetter) - ..write('(converter.mapToSql(${column.dartGetterName}.value));'); + ..write('(converter.mapToSql($getterName.value));'); } else { // no type converter. Write variable directly _buffer ..write(mapSetter) ..write('(') - ..write('${column.dartGetterName}.value') + ..write('$getterName.value') ..write(');'); } diff --git a/moor_generator/lib/src/writer/writer.dart b/moor_generator/lib/src/writer/writer.dart index c8fca8d4..343388b9 100644 --- a/moor_generator/lib/src/writer/writer.dart +++ b/moor_generator/lib/src/writer/writer.dart @@ -1,4 +1,5 @@ import 'package:meta/meta.dart'; +import 'package:moor_generator/moor_generator.dart'; import 'package:moor_generator/src/analyzer/options.dart'; /// Manages a tree structure which we use to generate code. @@ -125,3 +126,14 @@ class DartScope { extension WriterUtilsForOptions on MoorOptions { String get fieldModifier => generateMutableClasses ? '' : 'final'; } + +extension WriterUtilsForColumns on MoorColumn { + /// Adds an `this.` prefix is the [dartGetterName] is in [locals]. + String thisIfNeeded(Set locals) { + if (locals.contains(dartGetterName)) { + return 'this.$dartGetterName'; + } + + return dartGetterName; + } +}