platform/packages/route/lib/src/route.dart

80 lines
2.1 KiB
Dart
Raw Normal View History

2016-11-22 03:31:09 +00:00
part of angel_route.src.router;
2016-10-11 18:53:32 +00:00
/// Represents a virtual location within an application.
2018-08-20 18:59:51 +00:00
class Route<T> {
2017-11-27 02:21:19 +00:00
final String method;
final String path;
2018-08-20 18:59:51 +00:00
final List<T> handlers;
2017-11-28 21:07:14 +00:00
final Map<String, Map<String, dynamic>> _cache = {};
final RouteDefinition _routeDefinition;
2017-11-27 02:21:19 +00:00
String name;
2019-02-03 16:44:44 +00:00
Parser<RouteResult> _parser;
2016-10-14 03:07:34 +00:00
2017-11-27 02:21:19 +00:00
Route(this.path, {@required this.method, @required this.handlers})
2017-11-28 21:07:14 +00:00
: _routeDefinition = RouteGrammar.routeDefinition
2019-11-28 17:40:32 +00:00
.parse(SpanScanner(path.replaceAll(_straySlashes, '')))
2017-11-27 02:21:19 +00:00
.value {
2019-11-28 17:40:32 +00:00
if (_routeDefinition?.segments?.isNotEmpty != true) {
2019-02-03 16:44:44 +00:00
_parser = match('').map((r) => RouteResult({}));
2019-11-28 17:40:32 +00:00
}
2016-10-14 03:07:34 +00:00
}
2018-08-20 18:59:51 +00:00
factory Route.join(Route<T> a, Route<T> b) {
2017-11-27 02:21:19 +00:00
var start = a.path.replaceAll(_straySlashes, '');
var end = b.path.replaceAll(_straySlashes, '');
2019-11-28 17:40:32 +00:00
return Route('$start/$end'.replaceAll(_straySlashes, ''),
2017-11-27 02:21:19 +00:00
method: b.method, handlers: b.handlers);
2016-10-21 03:13:13 +00:00
}
2019-02-03 16:44:44 +00:00
Parser<RouteResult> get parser => _parser ??= _routeDefinition.compile();
2016-10-11 18:53:32 +00:00
2017-11-27 02:21:19 +00:00
@override
String toString() {
return '$method $path => $handlers';
2016-10-11 18:53:32 +00:00
}
2018-08-20 18:59:51 +00:00
Route<T> clone() {
2019-11-28 17:40:32 +00:00
return Route<T>(path, method: method, handlers: handlers)
2017-11-27 02:21:19 +00:00
.._cache.addAll(_cache);
2016-11-23 18:58:34 +00:00
}
2017-11-27 02:21:19 +00:00
String makeUri(Map<String, dynamic> params) {
2019-11-28 17:40:32 +00:00
var b = StringBuffer();
2017-11-27 02:21:19 +00:00
int i = 0;
2017-11-27 02:21:19 +00:00
for (var seg in _routeDefinition.segments) {
if (i++ > 0) b.write('/');
2019-11-28 17:40:32 +00:00
if (seg is ConstantSegment) {
2017-11-27 02:21:19 +00:00
b.write(seg.text);
2019-11-28 17:40:32 +00:00
} else if (seg is ParameterSegment) {
if (!params.containsKey(seg.name)) {
throw ArgumentError('Missing parameter "${seg.name}".');
}
2017-11-27 02:21:19 +00:00
b.write(params[seg.name]);
2016-10-21 03:13:13 +00:00
}
}
2017-11-27 02:21:19 +00:00
return b.toString();
2016-10-11 18:53:32 +00:00
}
2017-10-08 22:44:11 +00:00
}
2019-02-03 16:44:44 +00:00
/// The result of matching an individual route.
class RouteResult {
/// The parsed route parameters.
final Map<String, dynamic> params;
/// Optional. An explicit "tail" value to set.
String get tail => _tail;
String _tail;
RouteResult(this.params, {String tail}) : _tail = tail;
void _setTail(String v) => _tail ??= v;
/// Adds parameters.
void addAll(Map<String, dynamic> map) {
params.addAll(map);
}
}