Gen hashCode

This commit is contained in:
Tobe O 2018-11-03 03:36:59 -04:00
parent cbda457e30
commit f5d5f3660f
8 changed files with 75 additions and 1 deletions

View file

@ -1,3 +1,6 @@
# 2.1.1
* Generate `hashCode`.
# 2.1.0
* Removed dependency on `package:id`.
* Update dependencies for Dart2Stable.

View file

@ -52,6 +52,7 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
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<Serializable> {
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) {

View file

@ -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 <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/serialize

View file

@ -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<String, dynamic> 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<String, dynamic> 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<String, dynamic> toJson() {
return BookmarkSerializer.toMap(this);
}

View file

@ -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<String, dynamic> toJson() {
return BookSerializer.toMap(this);
}

View file

@ -36,6 +36,11 @@ class Gamepad extends _Gamepad {
.equals(other.dynamicMap, dynamicMap);
}
@override
int get hashCode {
return hashObjects([buttons, dynamicMap]);
}
Map<String, dynamic> toJson() {
return GamepadSerializer.toMap(this);
}

View file

@ -27,6 +27,11 @@ class GamepadButton implements _GamepadButton {
other.radius == radius;
}
@override
int get hashCode {
return hashObjects([name, radius]);
}
Map<String, dynamic> toJson() {
return GamepadButtonSerializer.toMap(this);
}

View file

@ -28,6 +28,11 @@ class WithEnum implements _WithEnum {
.equals(other.finalList, finalList);
}
@override
int get hashCode {
return hashObjects([type, finalList]);
}
Map<String, dynamic> toJson() {
return WithEnumSerializer.toMap(this);
}