Skip to content

Commit

Permalink
Fix generated code with $ in identifier names
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Nov 30, 2023
1 parent 99bb9e0 commit 5740eb8
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 35 deletions.
11 changes: 11 additions & 0 deletions drift/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import 'package:drift/native.dart';

part 'main.g.dart';

class Todo extends Table {
TextColumn get id => text()();

TextColumn get listid => text().nullable()();

TextColumn get text$ => text().named('text').nullable()();

BoolColumn get completed => boolean()();
}

@DataClassName('TodoCategory')
class TodoCategories extends Table {
IntColumn get id => integer().autoIncrement()();
Expand Down Expand Up @@ -57,6 +67,7 @@ abstract class TodoItemWithCategoryNameView extends View {
}

@DriftDatabase(tables: [
Todo,
TodoItems,
TodoCategories,
], views: [
Expand Down
267 changes: 266 additions & 1 deletion drift/example/main.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions drift_dev/lib/src/writer/tables/data_class_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class DataClassWriter {

for (final column in columns) {
final getter = column.nameInDart;
final jsonKey = column.getJsonKey(scope.options);
final jsonKeyString = asDartLiteral(column.getJsonKey(scope.options));
String deserialized;

final typeConverter = column.typeConverter;
Expand All @@ -154,13 +154,14 @@ class DataClassWriter {
type = '$type?';
}

final fromConverter = "serializer.fromJson<$type>(json['$jsonKey'])";
final fromConverter =
"serializer.fromJson<$type>(json[$jsonKeyString])";
final converterField = _converter(column);
deserialized = '$converterField.fromJson($fromConverter)';
} else {
final type = _columnType(column);

deserialized = "serializer.fromJson<$type>(json['$jsonKey'])";
deserialized = "serializer.fromJson<$type>(json[$jsonKeyString])";
}

_buffer.write('$getter: $deserialized,');
Expand All @@ -186,7 +187,7 @@ class DataClassWriter {
'return <String, dynamic>{\n');

for (final column in columns) {
final name = column.getJsonKey(scope.options);
final nameLiteral = asDartLiteral(column.getJsonKey(scope.options));
final getter = column.nameInDart;
final needsThis = getter == 'serializer';
var value = needsThis ? 'this.$getter' : getter;
Expand All @@ -199,7 +200,7 @@ class DataClassWriter {
dartType = _jsonType(column);
}

_buffer.write("'$name': serializer.toJson<$dartType>($value),");
_buffer.write("$nameLiteral: serializer.toJson<$dartType>($value),");
}

_buffer.write('};}');
Expand Down
3 changes: 2 additions & 1 deletion drift_dev/lib/src/writer/tables/table_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,11 @@ class TableWriter extends TableOrViewWriter {
void _writeColumnVerificationMeta(DriftColumn column) {
if (!_skipVerification) {
final meta = emitter.drift('VerificationMeta');
final arg = asDartLiteral(column.nameInDart);

buffer
..write('static const $meta ${_fieldNameForColumnMeta(column)} = ')
..writeln("const $meta('${column.nameInDart}');");
..writeln("const $meta($arg);");
}
}

Expand Down
8 changes: 7 additions & 1 deletion drift_dev/lib/src/writer/utils/override_toString.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ void overrideToString(
for (var i = 0; i < properties.length; i++) {
final property = properties[i];

into.write("..write('$property: \$$property");
if (property.contains(r'$')) {
final asKey = property.replaceAll('\$', '\\\$');
into.write("..write('$asKey: \${$property}");
} else {
into.write("..write('$property: \$$property");
}

if (i != properties.length - 1) into.write(', ');

into.write("')");
Expand Down
Loading

0 comments on commit 5740eb8

Please sign in to comment.