Let data classes override toString()

This commit is contained in:
Simon Binder 2019-03-10 12:54:50 +01:00
parent 09d047a868
commit cbd217317a
No known key found for this signature in database
GPG Key ID: B807FDF954BA00CF
6 changed files with 120 additions and 0 deletions

View File

@ -1,2 +1,5 @@
## 1.1.0
- Transactions
## 1.0.0
- Initial version of the Moor library

View File

@ -22,6 +22,15 @@ class Category {
id: id ?? this.id,
description: description ?? this.description,
);
@override
String toString() {
return (StringBuffer('Category(')
..write('id: $id, ')
..write('description: $description')
..write(')'))
.toString();
}
@override
int get hashCode => (id.hashCode) * 31 + description.hashCode;
@override
@ -95,6 +104,17 @@ class Recipe {
instructions: instructions ?? this.instructions,
category: category ?? this.category,
);
@override
String toString() {
return (StringBuffer('Recipe(')
..write('id: $id, ')
..write('title: $title, ')
..write('instructions: $instructions, ')
..write('category: $category')
..write(')'))
.toString();
}
@override
int get hashCode =>
(((id.hashCode) * 31 + title.hashCode) * 31 + instructions.hashCode) *
@ -186,6 +206,16 @@ class Ingredient {
name: name ?? this.name,
caloriesPer100g: caloriesPer100g ?? this.caloriesPer100g,
);
@override
String toString() {
return (StringBuffer('Ingredient(')
..write('id: $id, ')
..write('name: $name, ')
..write('caloriesPer100g: $caloriesPer100g')
..write(')'))
.toString();
}
@override
int get hashCode =>
((id.hashCode) * 31 + name.hashCode) * 31 + caloriesPer100g.hashCode;
@ -270,6 +300,16 @@ class IngredientInRecipe {
ingredient: ingredient ?? this.ingredient,
amountInGrams: amountInGrams ?? this.amountInGrams,
);
@override
String toString() {
return (StringBuffer('IngredientInRecipe(')
..write('recipe: $recipe, ')
..write('ingredient: $ingredient, ')
..write('amountInGrams: $amountInGrams')
..write(')'))
.toString();
}
@override
int get hashCode =>
((recipe.hashCode) * 31 + ingredient.hashCode) * 31 +

View File

@ -39,6 +39,18 @@ class TodoEntry {
targetDate: targetDate ?? this.targetDate,
category: category ?? this.category,
);
@override
String toString() {
return (StringBuffer('TodoEntry(')
..write('id: $id, ')
..write('title: $title, ')
..write('content: $content, ')
..write('targetDate: $targetDate, ')
..write('category: $category')
..write(')'))
.toString();
}
@override
int get hashCode =>
((((id.hashCode) * 31 + title.hashCode) * 31 + content.hashCode) * 31 +
@ -140,6 +152,15 @@ class Category {
id: id ?? this.id,
description: description ?? this.description,
);
@override
String toString() {
return (StringBuffer('Category(')
..write('id: $id, ')
..write('description: $description')
..write(')'))
.toString();
}
@override
int get hashCode => (id.hashCode) * 31 + description.hashCode;
@override
@ -210,6 +231,16 @@ class User {
name: name ?? this.name,
isAwesome: isAwesome ?? this.isAwesome,
);
@override
String toString() {
return (StringBuffer('User(')
..write('id: $id, ')
..write('name: $name, ')
..write('isAwesome: $isAwesome')
..write(')'))
.toString();
}
@override
int get hashCode =>
((id.hashCode) * 31 + name.hashCode) * 31 + isAwesome.hashCode;
@ -285,6 +316,15 @@ class SharedTodo {
todo: todo ?? this.todo,
user: user ?? this.user,
);
@override
String toString() {
return (StringBuffer('SharedTodo(')
..write('todo: $todo, ')
..write('user: $user')
..write(')'))
.toString();
}
@override
int get hashCode => (todo.hashCode) * 31 + user.hashCode;
@override

View File

@ -1,2 +1,5 @@
## 1.1.0
- Transactions
## 1.0.0
- Initial release

View File

@ -1,2 +1,5 @@
## 1.1.0
- The generated data classes now implement `toString()`
## 1.0.0
- Initial version of the Moor generator

View File

@ -29,6 +29,8 @@ class DataClassWriter {
// And a convenience method to copy data from this class.
_writeCopyWith(buffer);
_writeToString(buffer);
buffer.write('@override\n int get hashCode => ');
if (table.columns.isEmpty) {
@ -118,6 +120,35 @@ class DataClassWriter {
buffer.write(');');
}
void _writeToString(StringBuffer buffer) {
/*
@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');
}
/// Recursively creates the implementation for hashCode of the data class,
/// assuming it has at least one field. When it has one field, we just return
/// the hash code of that field. Otherwise, we multiply it with 31 and add