mirror of https://github.com/AMT-Cheif/drift.git
Override toString in result classes (#676)
This commit is contained in:
parent
ac20d9b324
commit
2332c58742
|
@ -30,7 +30,8 @@ At the moment, moor supports these options:
|
|||
constructor that takes a `Map<String, dynamic>`.
|
||||
* `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`.
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> 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');
|
||||
}
|
Loading…
Reference in New Issue