From 2332c58742ec0de304385548e4900878643f8b29 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Thu, 2 Jul 2020 19:59:41 +0200 Subject: [PATCH] Override toString in result classes (#676) --- .../docs/Advanced Features/builder_options.md | 3 +- moor/example/example.g.dart | 8 +++++ moor/test/data/tables/custom_tables.g.dart | 28 +++++++++++++++ moor/test/data/tables/todos.g.dart | 13 +++++++ moor_generator/lib/src/analyzer/options.dart | 7 ++-- .../src/writer/queries/result_set_writer.dart | 2 ++ .../src/writer/tables/data_class_writer.dart | 32 ++++------------- .../tables/update_companion_writer.dart | 32 ++++------------- .../src/writer/utils/override_toString.dart | 36 +++++++++++++++++++ 9 files changed, 106 insertions(+), 55 deletions(-) create mode 100644 moor_generator/lib/src/writer/utils/override_toString.dart diff --git a/docs/content/en/docs/Advanced Features/builder_options.md b/docs/content/en/docs/Advanced Features/builder_options.md index b9cb28c1..1369bf87 100644 --- a/docs/content/en/docs/Advanced Features/builder_options.md +++ b/docs/content/en/docs/Advanced Features/builder_options.md @@ -30,7 +30,8 @@ At the moment, moor supports these options: constructor that takes a `Map`. * `override_hash_and_equals_in_result_sets`: boolean. When moor generates another class to hold the result of generated select queries, this flag controls whether moor should - override `operator ==` and `hashCode` in those classes. + override `operator ==` and `hashCode` in those classes. In recent versions, it will also + override `toString` if this option is enabled. * `compact_query_methods` (defaults to `true`): For queries declared on a `@UseMoor` or `@UseDao` annotation, moor used to generate three methods: A base method returning a `Selectable` and then two helper methods returning a `Stream` or a `Future`. diff --git a/moor/example/example.g.dart b/moor/example/example.g.dart index 628b2514..26bafe5e 100644 --- a/moor/example/example.g.dart +++ b/moor/example/example.g.dart @@ -1041,4 +1041,12 @@ class TotalWeightResult { (other is TotalWeightResult && other.title == this.title && other.totalWeight == this.totalWeight); + @override + String toString() { + return (StringBuffer('TotalWeightResult(') + ..write('title: $title, ') + ..write('totalWeight: $totalWeight') + ..write(')')) + .toString(); + } } diff --git a/moor/test/data/tables/custom_tables.g.dart b/moor/test/data/tables/custom_tables.g.dart index 6cf31109..37a2d43e 100644 --- a/moor/test/data/tables/custom_tables.g.dart +++ b/moor/test/data/tables/custom_tables.g.dart @@ -1577,6 +1577,14 @@ class JsonResult { (other is JsonResult && other.key == this.key && other.value == this.value); + @override + String toString() { + return (StringBuffer('JsonResult(') + ..write('key: $key, ') + ..write('value: $value') + ..write(')')) + .toString(); + } } class MultipleResult { @@ -1597,6 +1605,15 @@ class MultipleResult { other.a == this.a && other.b == this.b && other.c == this.c); + @override + String toString() { + return (StringBuffer('MultipleResult(') + ..write('a: $a, ') + ..write('b: $b, ') + ..write('c: $c') + ..write(')')) + .toString(); + } } class ReadRowIdResult { @@ -1628,4 +1645,15 @@ class ReadRowIdResult { other.configValue == this.configValue && other.syncState == this.syncState && other.syncStateImplicit == this.syncStateImplicit); + @override + String toString() { + return (StringBuffer('ReadRowIdResult(') + ..write('rowid: $rowid, ') + ..write('configKey: $configKey, ') + ..write('configValue: $configValue, ') + ..write('syncState: $syncState, ') + ..write('syncStateImplicit: $syncStateImplicit') + ..write(')')) + .toString(); + } } diff --git a/moor/test/data/tables/todos.g.dart b/moor/test/data/tables/todos.g.dart index ba937eaf..1f4f8a13 100644 --- a/moor/test/data/tables/todos.g.dart +++ b/moor/test/data/tables/todos.g.dart @@ -1667,6 +1667,19 @@ class AllTodosWithCategoryResult { other.category == this.category && other.catId == this.catId && other.catDesc == this.catDesc); + @override + String toString() { + return (StringBuffer('AllTodosWithCategoryResult(') + ..write('id: $id, ') + ..write('title: $title, ') + ..write('content: $content, ') + ..write('targetDate: $targetDate, ') + ..write('category: $category, ') + ..write('catId: $catId, ') + ..write('catDesc: $catDesc') + ..write(')')) + .toString(); + } } // ************************************************************************** diff --git a/moor_generator/lib/src/analyzer/options.dart b/moor_generator/lib/src/analyzer/options.dart index d9b20b4d..e5bda756 100644 --- a/moor_generator/lib/src/analyzer/options.dart +++ b/moor_generator/lib/src/analyzer/options.dart @@ -21,8 +21,11 @@ class MoorOptions { @JsonKey(name: 'write_from_json_string_constructor', defaultValue: false) final bool generateFromJsonStringConstructor; - /// Overrides [Object.hashCode] and [Object.==] in classes generated for - /// custom queries. + /// Overrides [Object.hashCode], [Object.==] and [Object.toString] in classes + /// generated for custom queries. + /// + /// The `toString` override was added in a later version, we kept the original + /// name for backwards compatibility. @JsonKey(name: 'override_hash_and_equals_in_result_sets', defaultValue: false) final bool overrideHashAndEqualsInResultSets; diff --git a/moor_generator/lib/src/writer/queries/result_set_writer.dart b/moor_generator/lib/src/writer/queries/result_set_writer.dart index 058f4cca..ac5d3fdd 100644 --- a/moor_generator/lib/src/writer/queries/result_set_writer.dart +++ b/moor_generator/lib/src/writer/queries/result_set_writer.dart @@ -1,4 +1,5 @@ import 'package:moor_generator/src/model/sql_query.dart'; +import 'package:moor_generator/src/writer/utils/override_toString.dart'; import 'package:moor_generator/writer.dart'; /// Writes a class holding the result of an sql query into Dart. @@ -49,6 +50,7 @@ class ResultSetWriter { into.write(';\n'); overrideEquals(fieldNames, className, into); + overrideToString(className, fieldNames, into); } into.write('}\n'); diff --git a/moor_generator/lib/src/writer/tables/data_class_writer.dart b/moor_generator/lib/src/writer/tables/data_class_writer.dart index a0c1ce13..e5af51bf 100644 --- a/moor_generator/lib/src/writer/tables/data_class_writer.dart +++ b/moor_generator/lib/src/writer/tables/data_class_writer.dart @@ -1,5 +1,6 @@ import 'package:moor_generator/moor_generator.dart'; import 'package:moor_generator/src/utils/string_escaper.dart'; +import 'package:moor_generator/src/writer/utils/override_toString.dart'; import 'package:moor_generator/writer.dart'; import 'package:recase/recase.dart'; @@ -243,32 +244,11 @@ class DataClassWriter { } void _writeToString() { - /* - @override - String toString() { - return (StringBuffer('User(') - ..write('id: $id,') - ..write('name: $name,') - ..write('isAwesome: $isAwesome') - ..write(')')).toString(); - } - */ - - _buffer - ..write('@override\nString toString() {') - ..write("return (StringBuffer('${table.dartTypeName}(')"); - - for (var i = 0; i < table.columns.length; i++) { - final column = table.columns[i]; - final getterName = column.dartGetterName; - - _buffer.write("..write('$getterName: \$$getterName"); - if (i != table.columns.length - 1) _buffer.write(', '); - - _buffer.write("')"); - } - - _buffer..write("..write(')')).toString();")..write('\}\n'); + overrideToString( + table.dartTypeName, + [for (final column in table.columns) column.dartGetterName], + _buffer, + ); } void _writeHashCode() { 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 f743c4e4..f4f66b6e 100644 --- a/moor_generator/lib/src/writer/tables/update_companion_writer.dart +++ b/moor_generator/lib/src/writer/tables/update_companion_writer.dart @@ -1,5 +1,6 @@ import 'package:moor_generator/moor_generator.dart'; import 'package:moor_generator/src/utils/string_escaper.dart'; +import 'package:moor_generator/src/writer/utils/override_toString.dart'; import 'package:moor_generator/writer.dart'; class UpdateCompanionWriter { @@ -187,31 +188,10 @@ class UpdateCompanionWriter { } void _writeToString() { - /* - @override - String toString() { - return (StringBuffer('Category(') - ..write('id: $id, ') - ..write('description: $description') - ..write(')')) - .toString(); - */ - - _buffer - ..write('@override\nString toString() {\n') - ..write('return (StringBuffer(' - "'${table.getNameForCompanionClass(scope.options)}(')"); - - for (var i = 0; i < table.columns.length; i++) { - final column = table.columns[i]; - final dartGetterName = column.dartGetterName; - - _buffer.write("..write('$dartGetterName: \$$dartGetterName"); - if (i != table.columns.length - 1) _buffer.write(', '); - - _buffer.write("')"); - } - - _buffer..write("..write(')')).toString();")..write('\}\n'); + overrideToString( + table.getNameForCompanionClass(scope.options), + [for (final column in table.columns) column.dartGetterName], + _buffer, + ); } } diff --git a/moor_generator/lib/src/writer/utils/override_toString.dart b/moor_generator/lib/src/writer/utils/override_toString.dart new file mode 100644 index 00000000..a97ad3fe --- /dev/null +++ b/moor_generator/lib/src/writer/utils/override_toString.dart @@ -0,0 +1,36 @@ +// ignore_for_file: file_names + +/// Generates a `toString` override into the [into] buffer. +/// +/// The override looks like this: +/// +/// ```dart +/// @override +/// String toString() { +/// return (StringBuffer('ClassName(') +/// ..write('property1: $property1') +/// ..write('property2: $property2') +/// ..write(')') +/// ).toString(); +/// } +/// ``` +void overrideToString( + String className, + List properties, + StringBuffer into, +) { + into + ..write('@override\nString toString() {') + ..write("return (StringBuffer('$className(')"); + + for (var i = 0; i < properties.length; i++) { + final property = properties[i]; + + into.write("..write('$property: \$$property"); + if (i != properties.length - 1) into.write(', '); + + into.write("')"); + } + + into..write("..write(')')).toString();")..write('\}\n'); +}