1.0.1
This commit is contained in:
parent
d575b0f75e
commit
3324193e15
5 changed files with 56 additions and 11 deletions
|
@ -1 +1,8 @@
|
|||
# 1.0.0
|
||||
# 1.0.1
|
||||
* Explicitly extend `Service<Id, T>`.
|
||||
* Override `readData`.
|
||||
* Use `Service<Id, Map<String, dynamic>>` for `inner`, instead of just
|
||||
`Service<Id, Map>`.
|
||||
|
||||
# 1.0.0
|
||||
* Initial version.
|
|
@ -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"
|
||||
}
|
||||
]
|
|
@ -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<String, Todo>(MapService());
|
||||
var fs = LocalFileSystem();
|
||||
var exampleDir = fs.file(Platform.script).parent;
|
||||
var dataJson = exampleDir.childFile('data.json');
|
||||
var service = TypedService<String, Todo>(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);
|
||||
}
|
||||
|
|
|
@ -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<Id, T> extends Service<Id, dynamic> {
|
||||
class TypedService<Id, T> extends Service<Id, T> {
|
||||
/// The inner service.
|
||||
final Service<Id, Map> inner;
|
||||
final Service<Id, Map<String, dynamic>> inner;
|
||||
|
||||
TypedService(this.inner) : super() {
|
||||
if (!reflectType(T).isAssignableTo(reflectType(Model)))
|
||||
|
@ -14,6 +14,13 @@ class TypedService<Id, T> extends Service<Id, dynamic> {
|
|||
"If you specify a type for TypedService, it must extend Model.");
|
||||
}
|
||||
|
||||
@override
|
||||
FutureOr<T> 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<Id, T> extends Service<Id, dynamic> {
|
|||
}
|
||||
|
||||
/// Serializes [x] into a [Map].
|
||||
Map serialize(x) {
|
||||
Map<String, dynamic> serialize(x) {
|
||||
if (x is Model)
|
||||
return god.serializeObject(x) as Map;
|
||||
return (god.serializeObject(x) as Map).cast<String, dynamic>();
|
||||
else if (x is Map)
|
||||
return x;
|
||||
return x.cast<String, dynamic>();
|
||||
else
|
||||
throw ArgumentError('Cannot serialize ${x.runtimeType}');
|
||||
}
|
||||
|
@ -77,11 +84,11 @@ class TypedService<Id, T> extends Service<Id, dynamic> {
|
|||
inner.read(id, params).then(deserialize);
|
||||
|
||||
@override
|
||||
Future<T> modify(Id id, data, [Map<String, dynamic> params]) =>
|
||||
Future<T> modify(Id id, T data, [Map<String, dynamic> params]) =>
|
||||
inner.modify(id, serialize(data), params).then(deserialize);
|
||||
|
||||
@override
|
||||
Future<T> update(Id id, data, [Map<String, dynamic> params]) =>
|
||||
Future<T> update(Id id, T data, [Map<String, dynamic> params]) =>
|
||||
inner.update(id, serialize(data), params).then(deserialize);
|
||||
|
||||
@override
|
||||
|
|
|
@ -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 <thosakwe@gmail.com>
|
||||
|
@ -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
|
Loading…
Reference in a new issue