From 3324193e15e82fe8ec049a92b0c1011690d0a38f Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 26 Apr 2019 09:52:48 -0400 Subject: [PATCH] 1.0.1 --- CHANGELOG.md | 9 ++++++++- example/data.json | 23 ++++++++++++++++++++++- example/main.dart | 10 +++++++++- lib/angel_typed_service.dart | 21 ++++++++++++++------- pubspec.yaml | 4 +++- 5 files changed, 56 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f96ab092..d29d8063 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,8 @@ -# 1.0.0 \ No newline at end of file +# 1.0.1 +* Explicitly extend `Service`. +* Override `readData`. +* Use `Service>` for `inner`, instead of just +`Service`. + +# 1.0.0 +* Initial version. \ No newline at end of file diff --git a/example/data.json b/example/data.json index f1312318..e7a12b94 100644 --- a/example/data.json +++ b/example/data.json @@ -1 +1,22 @@ -{ "id": 3, "completed": false, "text": "clean your room" } +[ + { + "id": "0", + "createdAt": null, + "updatedAt": null, + "idAsInt": null, + "text": "Yes", + "completed": false, + "created_at": "2019-04-26T09:51:27.494884", + "updated_at": "2019-04-26T09:51:27.494884" + }, + { + "id": "1", + "createdAt": null, + "updatedAt": null, + "idAsInt": null, + "text": "nOPE", + "completed": false, + "created_at": "2019-04-26T09:51:37.847741", + "updated_at": "2019-04-26T09:51:37.847741" + } +] \ No newline at end of file diff --git a/example/main.dart b/example/main.dart index e12491ca..6e26e700 100644 --- a/example/main.dart +++ b/example/main.dart @@ -1,13 +1,19 @@ +import 'dart:io'; +import 'package:angel_file_service/angel_file_service.dart'; import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/http.dart'; import 'package:angel_typed_service/angel_typed_service.dart'; +import 'package:file/local.dart'; import 'package:json_god/json_god.dart' as god; import 'package:logging/logging.dart'; main() async { var app = Angel(); var http = AngelHttp(app); - var service = TypedService(MapService()); + var fs = LocalFileSystem(); + var exampleDir = fs.file(Platform.script).parent; + var dataJson = exampleDir.childFile('data.json'); + var service = TypedService(JsonFileService(dataJson)); hierarchicalLoggingEnabled = true; app.use('/api/todos', service); @@ -27,8 +33,10 @@ main() async { class Todo extends Model { String text; bool completed; + @override DateTime createdAt, updatedAt; + Todo({String id, this.text, this.completed, this.createdAt, this.updatedAt}) : super(id: id); } diff --git a/lib/angel_typed_service.dart b/lib/angel_typed_service.dart index 46e59657..c7a2185f 100644 --- a/lib/angel_typed_service.dart +++ b/lib/angel_typed_service.dart @@ -4,9 +4,9 @@ import 'package:angel_framework/angel_framework.dart'; import 'package:json_god/json_god.dart' as god; /// An Angel service that uses reflection to (de)serialize Dart objects. -class TypedService extends Service { +class TypedService extends Service { /// The inner service. - final Service inner; + final Service> inner; TypedService(this.inner) : super() { if (!reflectType(T).isAssignableTo(reflectType(Model))) @@ -14,6 +14,13 @@ class TypedService extends Service { "If you specify a type for TypedService, it must extend Model."); } + @override + FutureOr Function(RequestContext, ResponseContext) get readData => + _readData; + + T _readData(RequestContext req, ResponseContext res) => + deserialize(req.bodyAsMap); + /// Attempts to deserialize [x] into an instance of [T]. T deserialize(x) { // print('DESERIALIZE: $x (${x.runtimeType})'); @@ -55,11 +62,11 @@ class TypedService extends Service { } /// Serializes [x] into a [Map]. - Map serialize(x) { + Map serialize(x) { if (x is Model) - return god.serializeObject(x) as Map; + return (god.serializeObject(x) as Map).cast(); else if (x is Map) - return x; + return x.cast(); else throw ArgumentError('Cannot serialize ${x.runtimeType}'); } @@ -77,11 +84,11 @@ class TypedService extends Service { inner.read(id, params).then(deserialize); @override - Future modify(Id id, data, [Map params]) => + Future modify(Id id, T data, [Map params]) => inner.modify(id, serialize(data), params).then(deserialize); @override - Future update(Id id, data, [Map params]) => + Future update(Id id, T data, [Map params]) => inner.update(id, serialize(data), params).then(deserialize); @override diff --git a/pubspec.yaml b/pubspec.yaml index 3eb3f83f..0317a10b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_typed_service -version: 1.0.0 +version: 1.0.1 description: Angel services that use reflection to (de)serialize Dart objects. homepage: https://github.com/angel-dart/typed_service author: Tobe O @@ -9,6 +9,8 @@ dependencies: angel_framework: ^2.0.0-alpha json_god: ^2.0.0-beta dev_dependencies: + angel_file_service: ^2.0.0 + file: ^5.0.0 logging: ^0.11.0 pedantic: ^1.0.0 test: ^1.0.0 \ No newline at end of file