diff --git a/angel_serialize_generator/CHANGELOG.md b/angel_serialize_generator/CHANGELOG.md index dd895869..7b086e1f 100644 --- a/angel_serialize_generator/CHANGELOG.md +++ b/angel_serialize_generator/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.1.1 +* Generate `hashCode`. + # 2.1.0 * Removed dependency on `package:id`. * Update dependencies for Dart2Stable. diff --git a/angel_serialize_generator/lib/model.dart b/angel_serialize_generator/lib/model.dart index 7ec76a53..dd9567f7 100644 --- a/angel_serialize_generator/lib/model.dart +++ b/angel_serialize_generator/lib/model.dart @@ -52,6 +52,7 @@ class JsonModelGenerator extends GeneratorForAnnotation { generateConstructor(ctx, clazz, file); generateCopyWithMethod(ctx, clazz, file); generateEqualsOperator(ctx, clazz, file); + generateHashCode(ctx, clazz); // Generate toJson() method if necessary var serializers = annotation.peek('serializers')?.listValue ?? []; @@ -204,6 +205,21 @@ class JsonModelGenerator extends GeneratorForAnnotation { return (a, b) => '$eq.equals($a, $b)'; } + void generateHashCode(BuildContext ctx, ClassBuilder clazz) { + clazz + ..methods.add(new Method((method) { + method + ..name = 'hashCode' + ..type = MethodType.getter + ..returns = refer('int') + ..annotations.add(refer('override')) + ..body = refer('hashObjects') + .call([literalList(ctx.fields.map((f) => refer(f.name)))]) + .returned + .statement; + })); + } + void generateEqualsOperator( BuildContext ctx, ClassBuilder clazz, LibraryBuilder file) { clazz.methods.add(new Method((method) { diff --git a/angel_serialize_generator/pubspec.yaml b/angel_serialize_generator/pubspec.yaml index 248f5b54..02332c04 100644 --- a/angel_serialize_generator/pubspec.yaml +++ b/angel_serialize_generator/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_serialize_generator -version: 2.1.0 +version: 2.1.1 description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling. author: Tobe O homepage: https://github.com/angel-dart/serialize diff --git a/angel_serialize_generator/test/models/author.g.dart b/angel_serialize_generator/test/models/author.g.dart index 98234677..caab3506 100644 --- a/angel_serialize_generator/test/models/author.g.dart +++ b/angel_serialize_generator/test/models/author.g.dart @@ -83,6 +83,21 @@ class Author extends _Author { other.updatedAt == updatedAt; } + @override + int get hashCode { + return hashObjects([ + id, + name, + age, + books, + newestBook, + secret, + obscured, + createdAt, + updatedAt + ]); + } + Map toJson() { return AuthorSerializer.toMap(this); } @@ -129,6 +144,11 @@ class Library extends _Library { other.updatedAt == updatedAt; } + @override + int get hashCode { + return hashObjects([id, collection, createdAt, updatedAt]); + } + Map toJson() { return LibrarySerializer.toMap(this); } @@ -191,6 +211,11 @@ class Bookmark extends _Bookmark { other.updatedAt == updatedAt; } + @override + int get hashCode { + return hashObjects([id, history, page, comment, createdAt, updatedAt]); + } + Map toJson() { return BookmarkSerializer.toMap(this); } diff --git a/angel_serialize_generator/test/models/book.g.dart b/angel_serialize_generator/test/models/book.g.dart index 3f3be261..5de4cb2b 100644 --- a/angel_serialize_generator/test/models/book.g.dart +++ b/angel_serialize_generator/test/models/book.g.dart @@ -83,6 +83,21 @@ class Book extends _Book { other.updatedAt == updatedAt; } + @override + int get hashCode { + return hashObjects([ + id, + author, + title, + description, + pageCount, + notModels, + camelCaseString, + createdAt, + updatedAt + ]); + } + Map toJson() { return BookSerializer.toMap(this); } diff --git a/angel_serialize_generator/test/models/game_pad.g.dart b/angel_serialize_generator/test/models/game_pad.g.dart index f1797746..85fd895f 100644 --- a/angel_serialize_generator/test/models/game_pad.g.dart +++ b/angel_serialize_generator/test/models/game_pad.g.dart @@ -36,6 +36,11 @@ class Gamepad extends _Gamepad { .equals(other.dynamicMap, dynamicMap); } + @override + int get hashCode { + return hashObjects([buttons, dynamicMap]); + } + Map toJson() { return GamepadSerializer.toMap(this); } diff --git a/angel_serialize_generator/test/models/game_pad_button.g.dart b/angel_serialize_generator/test/models/game_pad_button.g.dart index 1c1ce0d1..262146df 100644 --- a/angel_serialize_generator/test/models/game_pad_button.g.dart +++ b/angel_serialize_generator/test/models/game_pad_button.g.dart @@ -27,6 +27,11 @@ class GamepadButton implements _GamepadButton { other.radius == radius; } + @override + int get hashCode { + return hashObjects([name, radius]); + } + Map toJson() { return GamepadButtonSerializer.toMap(this); } diff --git a/angel_serialize_generator/test/models/with_enum.g.dart b/angel_serialize_generator/test/models/with_enum.g.dart index 632f919d..2ffdbd00 100644 --- a/angel_serialize_generator/test/models/with_enum.g.dart +++ b/angel_serialize_generator/test/models/with_enum.g.dart @@ -28,6 +28,11 @@ class WithEnum implements _WithEnum { .equals(other.finalList, finalList); } + @override + int get hashCode { + return hashObjects([type, finalList]); + } + Map toJson() { return WithEnumSerializer.toMap(this); }