Strip out TypedService

This commit is contained in:
Tobe O 2018-08-19 22:37:48 -04:00
parent b69b6f3fa5
commit 4fce6594e2
7 changed files with 84 additions and 247 deletions

View file

@ -3,12 +3,7 @@
<component name="ChangeListManager">
<list default="true" id="7b89ff1e-1260-4dcf-9c3d-345de0471ea1" name="Default" comment="">
<change beforePath="$PROJECT_DIR$/CHANGELOG.md" beforeDir="false" afterPath="$PROJECT_DIR$/CHANGELOG.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/lib/src/core/response_context.dart" beforeDir="false" afterPath="$PROJECT_DIR$/lib/src/core/response_context.dart" afterDir="false" />
<change beforePath="$PROJECT_DIR$/lib/src/core/server.dart" beforeDir="false" afterPath="$PROJECT_DIR$/lib/src/core/server.dart" afterDir="false" />
<change beforePath="$PROJECT_DIR$/lib/src/core/typed_service.dart" beforeDir="false" afterPath="$PROJECT_DIR$/lib/src/core/typed_service.dart" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test/hooked_test.dart" beforeDir="false" afterPath="$PROJECT_DIR$/test/hooked_test.dart" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test/serialize_test.dart" beforeDir="false" afterPath="$PROJECT_DIR$/test/serialize_test.dart" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test/services_test.dart" beforeDir="false" afterPath="$PROJECT_DIR$/test/services_test.dart" afterDir="false" />
</list>
<ignored path="$PROJECT_DIR$/.tmp/" />
<ignored path="$PROJECT_DIR$/temp/" />
@ -64,11 +59,11 @@
</provider>
</entry>
</file>
<file leaf-file-name="typed_service.dart" pinned="false" current-in-tab="true">
<file leaf-file-name="typed_service.dart" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/lib/src/core/typed_service.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-485">
<caret line="13" column="3" lean-forward="true" selection-start-line="13" selection-start-column="3" selection-end-line="13" selection-end-column="3" />
<state relative-caret-position="304">
<caret line="19" column="40" lean-forward="true" selection-start-line="19" selection-start-column="40" selection-end-line="19" selection-end-column="40" />
<folding>
<element signature="e#0#20#0" expanded="true" />
</folding>
@ -88,6 +83,18 @@
</provider>
</entry>
</file>
<file leaf-file-name="CHANGELOG.md" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/CHANGELOG.md">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor relative-caret-position="176">
<caret line="11" column="58" selection-start-line="11" selection-start-column="58" selection-end-line="11" selection-end-column="58" />
</first_editor>
<second_editor />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="templating.dart" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/example/templating.dart">
<provider selected="true" editor-type-id="text-editor">
@ -100,14 +107,11 @@
</provider>
</entry>
</file>
<file leaf-file-name="hooks.dart" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/lib/hooks.dart">
<file leaf-file-name="streaming_test.dart" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/test/streaming_test.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-350">
<caret line="120" column="15" selection-start-line="120" selection-start-column="15" selection-end-line="120" selection-end-column="15" />
<folding>
<element signature="e#55#75#0" expanded="true" />
</folding>
<state relative-caret-position="16">
<caret line="10" lean-forward="true" selection-start-line="10" selection-end-line="10" />
</state>
</provider>
</entry>
@ -273,14 +277,14 @@
<option value="$PROJECT_DIR$/lib/src/core/angel_base.dart" />
<option value="$PROJECT_DIR$/pubspec.yaml" />
<option value="$PROJECT_DIR$/lib/src/http/angel_http.dart" />
<option value="$PROJECT_DIR$/lib/hooks.dart" />
<option value="$PROJECT_DIR$/CHANGELOG.md" />
<option value="$PROJECT_DIR$/lib/src/core/response_context.dart" />
<option value="$PROJECT_DIR$/test/services_test.dart" />
<option value="$PROJECT_DIR$/test/hooked_test.dart" />
<option value="$PROJECT_DIR$/lib/src/core/server.dart" />
<option value="$PROJECT_DIR$/test/serialize_test.dart" />
<option value="$PROJECT_DIR$/lib/hooks.dart" />
<option value="$PROJECT_DIR$/lib/src/core/typed_service.dart" />
<option value="$PROJECT_DIR$/CHANGELOG.md" />
</list>
</option>
</component>
@ -699,28 +703,7 @@
<workItem from="1533353660185" duration="262000" />
<workItem from="1534009494986" duration="594000" />
<workItem from="1534546784803" duration="353000" />
<workItem from="1534547885788" duration="3528000" />
</task>
<task id="LOCAL-00065" summary="Removed `HttpHeaders` in `ResponseContext`">
<created>1517980935063</created>
<option name="number" value="00065" />
<option name="presentableId" value="LOCAL-00065" />
<option name="project" value="LOCAL" />
<updated>1517980935063</updated>
</task>
<task id="LOCAL-00066" summary="Deprecated AngelMetrics">
<created>1517981171771</created>
<option name="number" value="00066" />
<option name="presentableId" value="LOCAL-00066" />
<option name="project" value="LOCAL" />
<updated>1517981171771</updated>
</task>
<task id="LOCAL-00067" summary="Export HTTP Req+Res context">
<created>1517981193409</created>
<option name="number" value="00067" />
<option name="presentableId" value="LOCAL-00067" />
<option name="project" value="LOCAL" />
<updated>1517981193409</updated>
<workItem from="1534547885788" duration="3873000" />
</task>
<task id="LOCAL-00068" summary="HTTP ResponseContext works">
<created>1517981784675</created>
@ -1044,7 +1027,28 @@
<option name="project" value="LOCAL" />
<updated>1534693773968</updated>
</task>
<option name="localTasksCounter" value="114" />
<task id="LOCAL-00114" summary="Remove traces of json_god">
<created>1534732268467</created>
<option name="number" value="00114" />
<option name="presentableId" value="LOCAL-00114" />
<option name="project" value="LOCAL" />
<updated>1534732268467</updated>
</task>
<task id="LOCAL-00115" summary="Remove lib/hooks.dart">
<created>1534732365272</created>
<option name="number" value="00115" />
<option name="presentableId" value="LOCAL-00115" />
<option name="project" value="LOCAL" />
<updated>1534732365272</updated>
</task>
<task id="LOCAL-00116" summary="Remove lib/common.dart">
<created>1534732527424</created>
<option name="number" value="00116" />
<option name="presentableId" value="LOCAL-00116" />
<option name="project" value="LOCAL" />
<updated>1534732527424</updated>
</task>
<option name="localTasksCounter" value="117" />
<servers />
</component>
<component name="TestHistory">
@ -1080,7 +1084,7 @@
</history-entry>
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="165827000" />
<option name="totallyTimeSpent" value="166172000" />
</component>
<component name="TodoView">
<todo-panel id="selected-file">
@ -1100,7 +1104,7 @@
<window_info anchor="bottom" id="Event Log" order="7" sideWeight="0.5035524" side_tool="true" weight="0.34188035" />
<window_info anchor="right" id="Maven Projects" order="3" />
<window_info anchor="bottom" id="Database Changes" order="8" show_stripe_button="false" />
<window_info active="true" anchor="bottom" id="Dart Analysis" order="7" visible="true" weight="0.31004366" />
<window_info anchor="bottom" id="Dart Analysis" order="7" weight="0.31004366" />
<window_info anchor="right" id="Flutter Outline" order="4" />
<window_info id="Capture Tool" order="2" />
<window_info id="Designer" order="2" />
@ -1118,7 +1122,7 @@
<window_info anchor="bottom" id="Run" order="2" sideWeight="0.4964476" weight="0.3973799" />
<window_info anchor="bottom" id="Version Control" order="7" weight="0.32905984" />
<window_info anchor="bottom" id="Terminal" order="7" weight="0.44978166" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.27920645" />
<window_info content_ui="combo" id="Project" order="0" weight="0.27920645" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32905984" />
<window_info anchor="right" id="SciView" order="4" />
<window_info anchor="right" id="Theme Preview" order="3" />
@ -1143,9 +1147,6 @@
</component>
<component name="VcsManagerConfiguration">
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
<MESSAGE value="Fix hooking bug" />
<MESSAGE value="+5" />
<MESSAGE value="+6" />
<MESSAGE value="+7" />
<MESSAGE value="Provide custom IsInstanceOf for testing" />
<MESSAGE value="Tracked down the rogue &quot;null&quot; print" />
@ -1168,7 +1169,10 @@
<MESSAGE value="Change signature of ResponseContext.jsonp" />
<MESSAGE value="Changed view engine to use `Map&lt;String, dynamic&gt;`" />
<MESSAGE value="Strip out json god" />
<option name="LAST_COMMIT_MESSAGE" value="Strip out json god" />
<MESSAGE value="Remove traces of json_god" />
<MESSAGE value="Remove lib/hooks.dart" />
<MESSAGE value="Remove lib/common.dart" />
<option name="LAST_COMMIT_MESSAGE" value="Remove lib/common.dart" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
@ -1176,13 +1180,6 @@
</breakpoint-manager>
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/lib/src/core/request_context.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="366">
<caret line="38" lean-forward="true" selection-start-line="38" selection-end-line="38" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/test/typed_service_test.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="448">
@ -1350,13 +1347,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/test/streaming_test.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="112">
<caret line="16" column="20" selection-start-line="16" selection-start-column="20" selection-end-line="16" selection-end-column="20" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/test/view_generator_test.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="96">
@ -1473,26 +1463,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/CHANGELOG.md">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor relative-caret-position="144">
<caret line="9" column="63" selection-start-line="9" selection-start-column="63" selection-end-line="9" selection-end-column="63" />
</first_editor>
<second_editor />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/example/templating.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="240">
<caret line="15" column="22" selection-start-line="15" selection-start-column="22" selection-end-line="15" selection-end-column="22" />
<folding>
<element signature="e#0#17#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/lib/src/http/http.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="192">
@ -1561,26 +1531,49 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/lib/hooks.dart">
<entry file="file://$PROJECT_DIR$/example/templating.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-350">
<caret line="120" column="15" selection-start-line="120" selection-start-column="15" selection-end-line="120" selection-end-column="15" />
<state relative-caret-position="240">
<caret line="15" column="22" selection-start-line="15" selection-start-column="22" selection-end-line="15" selection-end-column="22" />
<folding>
<element signature="e#55#75#0" expanded="true" />
<element signature="e#0#17#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/lib/hooks.dart">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/test/streaming_test.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="16">
<caret line="10" lean-forward="true" selection-start-line="10" selection-end-line="10" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/lib/common.dart">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/lib/src/core/typed_service.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-485">
<caret line="13" column="3" lean-forward="true" selection-start-line="13" selection-start-column="3" selection-end-line="13" selection-end-column="3" />
<state relative-caret-position="304">
<caret line="19" column="40" lean-forward="true" selection-start-line="19" selection-start-column="40" selection-end-line="19" selection-end-column="40" />
<folding>
<element signature="e#0#20#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/CHANGELOG.md">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor relative-caret-position="176">
<caret line="11" column="58" selection-start-line="11" selection-start-column="58" selection-end-line="11" selection-end-column="58" />
</first_editor>
<second_editor />
</state>
</provider>
</entry>
</component>
<component name="masterDetails">
<states>

View file

@ -8,6 +8,8 @@
* Remove dependency on `package:json_god` by default.
* Remove dependency on `package:dart2_constant`.
* Remove `contentType` argument in `ResponseContext.serialize`.
* Moved `lib/hooks.dart` into `package:angel_hooks`.
* Moved `TypedService` into `package:angel_typed_service`.
# 1.1.5+1
* Patched annoying error that prevented MapServices from working,

View file

@ -1,90 +0,0 @@
import 'dart:async';
import 'dart:mirrors';
import '../../common.dart';
import 'service.dart';
class TypedService<T> extends Service {
final Service inner;
TypedService(this.inner) : super() {
if (!reflectType(T).isAssignableTo(reflectType(Model)))
throw new Exception(
"If you specify a type for TypedService, it must extend Model.");
}
deserialize(x) {
// print('DESERIALIZE: $x (${x.runtimeType})');
if (x is Type || x is T)
return x;
else if (x is Iterable)
return x.map(deserialize).toList();
else if (x is Map) {
Map data = x.keys.fold({}, (map, key) {
var value = x[key];
if ((key == 'createdAt' ||
key == 'updatedAt' ||
key == 'created_at' ||
key == 'updated_at') &&
value is String) {
return map..[key] = DateTime.parse(value);
} else {
return map..[key] = value;
}
});
Model result = god.deserializeDatum(data, outputType: T);
if (data['createdAt'] is DateTime) {
result.createdAt = data['createdAt'] as DateTime;
} else if (data['created_at'] is DateTime) {
result.createdAt = data['created_at'] as DateTime;
}
if (data['updatedAt'] is DateTime) {
result.updatedAt = data['updatedAt'] as DateTime;
} else if (data['updated_at'] is DateTime) {
result.updatedAt = data['updated_at'] as DateTime;
}
// print('x: $x\nresult: $result');
return result;
} else
return x;
}
serialize(x) {
// TODO: Custom serializer, i.e. json_god?
// if (x is Model)
// return json.encodeObject(x);
// else
if (x is Map)
return x;
else if (x is Iterable)
return x.map(serialize).toList();
else
throw new ArgumentError('Cannot serialize ${x.runtimeType}');
}
@override
Future index([Map params]) => inner.index(params).then(deserialize);
@override
Future create(data, [Map params]) =>
inner.create(serialize(data), params).then(deserialize);
@override
Future read(id, [Map params]) => inner.read(id, params).then(deserialize);
@override
Future modify(id, data, [Map params]) =>
inner.modify(id, serialize(data), params).then(deserialize);
@override
Future update(id, data, [Map params]) =>
inner.update(id, serialize(data), params).then(deserialize);
@override
Future remove(id, [Map params]) => inner.remove(id, params).then(deserialize);
}

View file

@ -21,7 +21,6 @@ import 'serialize_test.dart' as serialize;
import 'server_test.dart' as server;
import 'services_test.dart' as services;
import 'streaming_test.dart' as streaming;
import 'typed_service_test.dart' as typed_service;
import 'view_generator_test.dart' as view_generator;
import 'package:test/test.dart';
@ -47,6 +46,5 @@ main() {
group('server', server.main);
group('services', services.main);
group('streaming', streaming.main);
group('typed_service', typed_service.main);
group('view generator', view_generator.main);
}

View file

@ -21,7 +21,7 @@ main() {
setUp(() async {
app = new Angel(MirrorsReflector());
client = new http.Client();
app.use('/todos', new TypedService<Todo>(new MapService()));
app.use('/todos', new MapService());
app.use('/books', new BookService());
Todos = app.service("todos") as HookedService;

View file

@ -21,7 +21,7 @@ main() {
setUp(() async {
app = new Angel(MirrorsReflector())
..use('/todos', new TypedService<Todo>(new MapService()))
..use('/todos', new MapService())
..errorHandler = (e, req, res) {
print('Whoops: ${e.error}');
if (e.stackTrace != null) print(new Chain.forTrace(e.stackTrace).terse);

View file

@ -1,66 +0,0 @@
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/common.dart';
import 'package:test/test.dart';
import 'common.dart';
main() {
var svc = new TypedService<Todo>(new MapService());
test('force model', () {
expect(() => new TypedService<String>(null), throwsException);
});
test('serialize', () {
expect(svc.serialize({'foo': 'bar'}), {'foo': 'bar'});
expect(
svc.serialize([
{'foo': 'bar'}
]),
[
{'foo': 'bar'}
]);
expect(() => svc.serialize(2), throwsArgumentError);
var now = new DateTime.now();
var t =
new Todo(text: 'a', completed: false, createdAt: now, updatedAt: now);
var m = svc.serialize(t);
print(m);
expect(m..remove('_identityHashCode'), {
'id': null,
'createdAt': now.toIso8601String(),
'updatedAt': now.toIso8601String(),
'text': 'a',
'completed': false
});
});
test('deserialize date', () {
var now = new DateTime.now();
var m = svc.deserialize({
'createdAt': now.toIso8601String(),
'updatedAt': now.toIso8601String()
});
expect(m, const IsInstanceOf<Todo>());
var t = m as Todo;
expect(t.createdAt.millisecondsSinceEpoch, now.millisecondsSinceEpoch);
});
test('deserialize date w/ underscore', () {
var now = new DateTime.now();
var m = svc.deserialize({
'created_at': now.toIso8601String(),
'updated_at': now.toIso8601String()
});
expect(m, const IsInstanceOf<Todo>());
var t = m as Todo;
expect(t.createdAt.millisecondsSinceEpoch, now.millisecondsSinceEpoch);
});
}
class Todo extends Model {
String text;
bool completed;
@override
DateTime createdAt, updatedAt;
Todo({this.text, this.completed, this.createdAt, this.updatedAt});
}