diff --git a/.gitignore b/.gitignore index 28bbcb87..de511d3c 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 8478f23b..9e4ad398 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 7b0c5682..9f212e52 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 00000000..62711994 --- /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 00000000..a9c37b79 --- /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 00000000..1a6ce2f0 --- /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);