From 02fccd3b25064b6daf03ec0d1703d55f08e9a45f Mon Sep 17 00:00:00 2001 From: Chima Precious Date: Sat, 27 Jul 2024 10:37:00 +0000 Subject: [PATCH] defer params map to later --- .../lib/src/parametric/definition.dart | 20 +++++++++--------- packages/spanner/lib/src/tree/tree.dart | 21 ++++++++++++++----- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/packages/spanner/lib/src/parametric/definition.dart b/packages/spanner/lib/src/parametric/definition.dart index 2833d6f..4e29145 100644 --- a/packages/spanner/lib/src/parametric/definition.dart +++ b/packages/spanner/lib/src/parametric/definition.dart @@ -2,6 +2,8 @@ import '../tree/node.dart'; import '../tree/tree.dart'; import 'utils.dart'; +typedef ParamAndValue = ({String name, String? value}); + SingleParameterDefn _singleParamDefn(RegExpMatch m, [String? nextPart]) => SingleParameterDefn._( m.group(2)!, @@ -47,7 +49,7 @@ abstract class ParameterDefinition implements HandlerStore { void resolveParams( String pattern, - Map collector, { + List collector, { bool caseSentive = false, }); } @@ -96,15 +98,13 @@ class SingleParameterDefn extends ParameterDefinition with HandlerStoreMixin { @override void resolveParams( final String pattern, - Map collector, { + List collector, { bool caseSentive = false, }) { - collector[name] = matchPattern( - pattern, - prefix ?? "", - suffix ?? "", - caseSentive, - ); + collector.add(( + name: name, + value: matchPattern(pattern, prefix ?? "", suffix ?? "", caseSentive) + )); } @override @@ -146,14 +146,14 @@ class CompositeParameterDefinition extends ParameterDefinition @override void resolveParams( String pattern, - Map collector, { + List collector, { bool caseSentive = false, }) { final match = _template.firstMatch(pattern); if (match == null) return; for (final key in match.groupNames) { - collector[key] = match.namedGroup(key); + collector.add((name: key, value: match.namedGroup(key))); } } diff --git a/packages/spanner/lib/src/tree/tree.dart b/packages/spanner/lib/src/tree/tree.dart index b27bb55..06c06b2 100644 --- a/packages/spanner/lib/src/tree/tree.dart +++ b/packages/spanner/lib/src/tree/tree.dart @@ -171,7 +171,7 @@ class Spanner { if (path.startsWith(BASE_PATH)) path = path.substring(1); if (path.endsWith(BASE_PATH)) path = path.substring(0, path.length - 1); - final resolvedParams = {}; + final resolvedParams = []; final resolvedHandlers = [...root.middlewares]; getResults(IndexedValue? handler) => @@ -190,7 +190,9 @@ class Spanner { /// incase we don't find the route we were looking for. var wildcardNode = rootNode.wildcardNode; - final routeSegments = route is Uri ? route.pathSegments : path.split('/'); + final routeSegments = route is Uri + ? route.pathSegments + : customSplit(path.toString(), '/').toList(); for (int i = 0; i < routeSegments.length; i++) { final currPart = routeSegments[i]; @@ -221,7 +223,10 @@ class Spanner { if ((childNode == null && parametricNode == null) || (childNode == null && definition == null)) { if (wildcardNode != null) { - resolvedParams['*'] = routeSegments.sublist(i).join('/'); + resolvedParams.add(( + name: WildcardNode.key, + value: routeSegments.sublist(i).join('/'), + )); rootNode = wildcardNode; } break; @@ -275,14 +280,14 @@ class Spanner { } class RouteResult { - final Map params; + final List _params; final List _values; /// this is either a Node or Parametric Definition @visibleForTesting final dynamic actual; - RouteResult(this.params, this._values, {this.actual}); + RouteResult(this._params, this._values, {this.actual}); Iterable? _cachedValues; Iterable get values { @@ -290,4 +295,10 @@ class RouteResult { _values.sort((a, b) => a.index.compareTo(b.index)); return _cachedValues = _values.map((e) => e.value); } + + Map? _cachedParams; + Map get params { + if (_cachedParams != null) return _cachedParams!; + return {for (final entry in _params) entry.name: entry.value}; + } }