Finish this tomorrow
This commit is contained in:
parent
ceb66dd520
commit
b718b0bddb
3 changed files with 49 additions and 8 deletions
|
@ -48,6 +48,7 @@ class Route {
|
||||||
final List<Route> _children = [];
|
final List<Route> _children = [];
|
||||||
final List _handlers = [];
|
final List _handlers = [];
|
||||||
RegExp _matcher;
|
RegExp _matcher;
|
||||||
|
String _method;
|
||||||
String _name;
|
String _name;
|
||||||
Route _parent;
|
Route _parent;
|
||||||
String _path;
|
String _path;
|
||||||
|
@ -56,7 +57,7 @@ class Route {
|
||||||
List<Route> get children => new List.unmodifiable(_children);
|
List<Route> get children => new List.unmodifiable(_children);
|
||||||
List get handlers => new List.unmodifiable(_handlers);
|
List get handlers => new List.unmodifiable(_handlers);
|
||||||
RegExp get matcher => _matcher;
|
RegExp get matcher => _matcher;
|
||||||
final String method;
|
String get method => _method;
|
||||||
String get name => _name;
|
String get name => _name;
|
||||||
Route get parent => _parent;
|
Route get parent => _parent;
|
||||||
String get path => _path;
|
String get path => _path;
|
||||||
|
@ -94,10 +95,11 @@ class Route {
|
||||||
Route(Pattern path,
|
Route(Pattern path,
|
||||||
{Iterable<Route> children: const [],
|
{Iterable<Route> children: const [],
|
||||||
Iterable handlers: const [],
|
Iterable handlers: const [],
|
||||||
this.method: "GET",
|
method: "GET",
|
||||||
String name: null}) {
|
String name: null}) {
|
||||||
if (children != null) _children.addAll(children);
|
if (children != null) _children.addAll(children);
|
||||||
if (handlers != null) _handlers.addAll(handlers);
|
if (handlers != null) _handlers.addAll(handlers);
|
||||||
|
_method = method;
|
||||||
_name = name;
|
_name = name;
|
||||||
|
|
||||||
if (path is RegExp) {
|
if (path is RegExp) {
|
||||||
|
@ -113,6 +115,43 @@ class Route {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Splits a route path into a list of segments, and then
|
||||||
|
/// builds a hierarchy of off that.
|
||||||
|
///
|
||||||
|
/// This should generally be used instead of the original
|
||||||
|
/// Route constructor.
|
||||||
|
///
|
||||||
|
/// All children and handlers, as well as the method, will be
|
||||||
|
/// assigned to the last child route created.
|
||||||
|
///
|
||||||
|
/// The final child route is returned.
|
||||||
|
factory Route.build(Pattern path,
|
||||||
|
{Iterable<Route> children: const [],
|
||||||
|
Iterable handlers: const [],
|
||||||
|
method: "GET",
|
||||||
|
String name: null}) {
|
||||||
|
final segments = path.toString().split('/').where((str) => str.isNotEmpty);
|
||||||
|
print('Seg: $segments');
|
||||||
|
Route result;
|
||||||
|
|
||||||
|
for (String segment in segments) {
|
||||||
|
print('SEGGG: $segment');
|
||||||
|
if (result == null)
|
||||||
|
result = new Route(segment);
|
||||||
|
else
|
||||||
|
result = result.child(segment);
|
||||||
|
}
|
||||||
|
|
||||||
|
print('result: ${result.path}');
|
||||||
|
|
||||||
|
result._children.addAll(children);
|
||||||
|
result._handlers.addAll(handlers);
|
||||||
|
result._method = method;
|
||||||
|
result._name = name;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
factory Route.join(Route parent, Route child) {
|
factory Route.join(Route parent, Route child) {
|
||||||
final String path1 = parent.path
|
final String path1 = parent.path
|
||||||
.replaceAll(_rgxStart, '')
|
.replaceAll(_rgxStart, '')
|
||||||
|
@ -137,9 +176,11 @@ class Route {
|
||||||
|
|
||||||
String separator = (pattern1.isEmpty || pattern1 == '^') ? '' : '\\/';
|
String separator = (pattern1.isEmpty || pattern1 == '^') ? '' : '\\/';
|
||||||
|
|
||||||
return route
|
parent._children.add(route
|
||||||
.._matcher = new RegExp('$pattern1$separator$pattern2')
|
.._matcher = new RegExp('$pattern1$separator$pattern2')
|
||||||
.._parent = parent;
|
.._parent = parent);
|
||||||
|
|
||||||
|
return route;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Route> addAll(Iterable<Route> routes, {bool join: true}) {
|
List<Route> addAll(Iterable<Route> routes, {bool join: true}) {
|
||||||
|
@ -160,7 +201,7 @@ class Route {
|
||||||
Iterable handlers: const [],
|
Iterable handlers: const [],
|
||||||
String method: "GET",
|
String method: "GET",
|
||||||
String name: null}) {
|
String name: null}) {
|
||||||
final route = new Route(path,
|
final route = new Route.build(path,
|
||||||
children: children, handlers: handlers, method: method, name: name);
|
children: children, handlers: handlers, method: method, name: name);
|
||||||
return addChild(route);
|
return addChild(route);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ import 'package:angel_route/angel_route.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
final foo = new Route('/foo', handlers: ['bar']);
|
final foo = new Route.build('/foo', handlers: ['bar']);
|
||||||
final bar = foo.child('/bar');
|
final bar = foo.child('/bar');
|
||||||
final baz = bar.child('//////baz//////', handlers: ['hello', 'world']);
|
final baz = bar.child('//////baz//////', handlers: ['hello', 'world']);
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,10 @@ import 'package:angel_route/angel_route.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
final base = new Route('foo');
|
final foo = new Route.build('/foo/:id([0-9]+)', handlers: ['bar']);
|
||||||
final foo = base.child(':id([0-9]+)', handlers: ['bar']);
|
|
||||||
final bar = foo.child('/bar');
|
final bar = foo.child('/bar');
|
||||||
final baz = bar.child('//////baz//////', handlers: ['hello', 'world']);
|
final baz = bar.child('//////baz//////', handlers: ['hello', 'world']);
|
||||||
|
new Router(foo).dumpTree();
|
||||||
|
|
||||||
test('matching', () {
|
test('matching', () {
|
||||||
expect(foo.children.length, equals(1));
|
expect(foo.children.length, equals(1));
|
||||||
|
|
Loading…
Reference in a new issue