platform/core/route/lib/src/route.dart

100 lines
2.6 KiB
Dart
Raw Normal View History

2023-12-24 01:52:57 +00:00
part of 'router.dart';
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> {
2021-04-03 15:04:25 +00:00
final String method;
final String path;
2017-11-28 21:07:14 +00:00
final Map<String, Map<String, dynamic>> _cache = {};
2021-04-03 15:04:25 +00:00
final RouteDefinition? _routeDefinition;
final List<T> handlers;
String? name;
2021-04-03 05:50:52 +00:00
Parser<RouteResult>? _parser;
2021-04-03 15:04:25 +00:00
Route(this.path, {required this.method, required this.handlers})
: _routeDefinition = RouteGrammar.routeDefinition
.parse(SpanScanner(path.replaceAll(_straySlashes, '')))
.value {
if (_routeDefinition?.segments.isNotEmpty != true) {
_parser = match('').map((r) => RouteResult({}));
}
/*
2021-04-03 05:50:52 +00:00
var result = RouteGrammar.routeDefinition
.parse(SpanScanner(path.replaceAll(_straySlashes, '')));
if (result.value != null) {
2021-04-03 15:04:25 +00:00
2021-04-03 05:50:52 +00:00
//throw ArgumentError('[Route] Failed to create route for $path');
2021-04-03 15:04:25 +00:00
_routeDefinition = result.value;
2021-04-03 05:50:52 +00:00
if (_routeDefinition.segments.isEmpty) {
_parser = match('').map((r) => RouteResult({}));
}
} else {
2021-04-03 15:04:25 +00:00
_parser = match('').map((r) => RouteResult({}));
2019-11-28 17:40:32 +00:00
}
2021-04-03 15:04:25 +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
}
2021-04-03 05:50:52 +00:00
//List<T> get handlers => _handlers;
2021-04-03 15:04:25 +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();
2021-04-03 05:50:52 +00:00
var i = 0;
2021-04-03 15:04:25 +00:00
if (_routeDefinition != null) {
2024-05-21 16:32:59 +00:00
for (var seg in _routeDefinition.segments) {
2021-04-03 15:04:25 +00:00
if (i++ > 0) b.write('/');
if (seg is ConstantSegment) {
b.write(seg.text);
} else if (seg is ParameterSegment) {
if (!params.containsKey(seg.name)) {
throw ArgumentError('Missing parameter "${seg.name}".');
}
b.write(params[seg.name]);
2019-11-28 17:40:32 +00:00
}
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.
2021-04-03 05:50:52 +00:00
final Map<String, dynamic> params;
2019-02-03 16:44:44 +00:00
/// Optional. An explicit "tail" value to set.
2021-03-18 00:11:45 +00:00
String? get tail => _tail;
2019-02-03 16:44:44 +00:00
2021-03-18 00:11:45 +00:00
String? _tail;
2019-02-03 16:44:44 +00:00
2021-03-18 00:11:45 +00:00
RouteResult(this.params, {String? tail}) : _tail = tail;
2019-02-03 16:44:44 +00:00
2021-03-18 00:11:45 +00:00
void _setTail(String? v) => _tail ??= v;
2019-02-03 16:44:44 +00:00
/// Adds parameters.
2021-04-03 05:50:52 +00:00
void addAll(Map<String, dynamic> map) {
2019-02-03 16:44:44 +00:00
params.addAll(map);
}
}