Skip to content

Commit

Permalink
defer params map to later
Browse files Browse the repository at this point in the history
  • Loading branch information
codekeyz committed Jul 27, 2024
1 parent 34ffc09 commit 02fccd3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
20 changes: 10 additions & 10 deletions packages/spanner/lib/src/parametric/definition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)!,
Expand Down Expand Up @@ -47,7 +49,7 @@ abstract class ParameterDefinition implements HandlerStore {

void resolveParams(
String pattern,
Map<String, dynamic> collector, {
List<ParamAndValue> collector, {
bool caseSentive = false,
});
}
Expand Down Expand Up @@ -96,15 +98,13 @@ class SingleParameterDefn extends ParameterDefinition with HandlerStoreMixin {
@override
void resolveParams(
final String pattern,
Map<String, dynamic> collector, {
List<ParamAndValue> collector, {
bool caseSentive = false,
}) {
collector[name] = matchPattern(
pattern,
prefix ?? "",
suffix ?? "",
caseSentive,
);
collector.add((
name: name,
value: matchPattern(pattern, prefix ?? "", suffix ?? "", caseSentive)
));
}

@override
Expand Down Expand Up @@ -146,14 +146,14 @@ class CompositeParameterDefinition extends ParameterDefinition
@override
void resolveParams(
String pattern,
Map<String, dynamic> collector, {
List<ParamAndValue> 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)));
}
}

Expand Down
21 changes: 16 additions & 5 deletions packages/spanner/lib/src/tree/tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <String, dynamic>{};
final resolvedParams = <ParamAndValue>[];
final resolvedHandlers = <IndexedValue>[...root.middlewares];

getResults(IndexedValue? handler) =>
Expand All @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -275,19 +280,25 @@ class Spanner {
}

class RouteResult {
final Map<String, dynamic> params;
final List<ParamAndValue> _params;
final List<IndexedValue> _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<dynamic>? _cachedValues;
Iterable<dynamic> get values {
if (_cachedValues != null) return _cachedValues!;
_values.sort((a, b) => a.index.compareTo(b.index));
return _cachedValues = _values.map((e) => e.value);
}

Map<String, dynamic>? _cachedParams;
Map<String, dynamic> get params {
if (_cachedParams != null) return _cachedParams!;
return {for (final entry in _params) entry.name: entry.value};
}
}

0 comments on commit 02fccd3

Please sign in to comment.