From 3c8cab050e0b199602914e25508187d81e9f9432 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 31 Dec 2016 11:17:19 -0500 Subject: [PATCH] source_gen --- .gitignore | 3 +++ lib/src/models/user.dart | 37 +++++++++++++++---------------------- pubspec.yaml | 2 ++ tool/build.dart | 4 ++++ tool/phases.dart | 12 ++++++++++++ tool/watch.dart | 4 ++++ 6 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 tool/build.dart create mode 100644 tool/phases.dart create mode 100644 tool/watch.dart diff --git a/.gitignore b/.gitignore index 28bbcb8..de511d3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ ### Dart template # See https://www.dartlang.org/tools/private-files.html +# source_gen +*.g.dart + # Files and directories created by pub .buildlog .packages diff --git a/lib/src/models/user.dart b/lib/src/models/user.dart index 8478f23..9e4ad39 100644 --- a/lib/src/models/user.dart +++ b/lib/src/models/user.dart @@ -1,14 +1,26 @@ library angel.models.user; -import 'dart:convert'; import 'package:angel_mongo/model.dart'; +import 'package:source_gen/generators/json_serializable.dart'; -class User extends Model { +part 'user.g.dart'; + +@JsonSerializable() +class User extends Model with _$UserSerializerMixin { + @JsonKey('email') String email; + + @JsonKey('username') String username; + + @JsonKey('password') String password; + + @JsonKey('roles') final List roles = []; + factory User.fromJson(Map json) => _$UserFromJson(json); + User( {String id, this.email, @@ -16,28 +28,9 @@ class User extends Model { this.password, List roles: const []}) { this.id = id; - + if (roles != null) { this.roles.addAll(roles); } } - - factory User.fromJson(String json) => new User.fromMap(JSON.decode(json)); - - factory User.fromMap(Map data) => new User( - id: data['id'], - email: data['email'], - username: data['username'], - password: data['password'], - roles: data['roles']); - - Map toJson() { - return { - 'id': id, - 'email': email, - 'username': username, - 'password': password, - 'roles': roles - }; - } } diff --git a/pubspec.yaml b/pubspec.yaml index 7b0c568..9f212e5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,8 +11,10 @@ dependencies: mailer: ^1.1.0+4 validate: ^1.5.2 dev_dependencies: + build_runner: ^0.1.1 grinder: ^0.8.0+2 http: ^0.11.3 + source_gen: ^0.5.2 test: ^0.12.13 transformers: - angel_configuration diff --git a/tool/build.dart b/tool/build.dart new file mode 100644 index 0000000..6271199 --- /dev/null +++ b/tool/build.dart @@ -0,0 +1,4 @@ +import 'package:build_runner/build_runner.dart'; +import 'phases.dart'; + +main() => build(PHASES, deleteFilesByDefault: true); diff --git a/tool/phases.dart b/tool/phases.dart new file mode 100644 index 0000000..a9c37b7 --- /dev/null +++ b/tool/phases.dart @@ -0,0 +1,12 @@ +import 'package:build_runner/build_runner.dart'; +import 'package:source_gen/builder.dart'; +import 'package:source_gen/generators/json_serializable_generator.dart'; + +final PhaseGroup PHASES = new PhaseGroup.singleAction( + new GeneratorBuilder(const [const JsonSerializableGenerator()]), + new InputSet('angel', const [ + 'bin/**/*.dart', + 'lib/**/*.dart', + 'views/**/*.dart', + 'web/**/*.dart' + ])); diff --git a/tool/watch.dart b/tool/watch.dart new file mode 100644 index 0000000..1a6ce2f --- /dev/null +++ b/tool/watch.dart @@ -0,0 +1,4 @@ +import 'package:build_runner/build_runner.dart'; +import 'phases.dart'; + +main() => watch(PHASES, deleteFilesByDefault: true);