Skip to content

Commit

Permalink
feat: support custom all method mark
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Sep 9, 2024
1 parent 1f3df61 commit 89a1772
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
8 changes: 7 additions & 1 deletion lib/src/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import '_internal/node_impl.dart';
import 'types.dart';

final class _ContextImpl<T> implements RouterContext<T> {
_ContextImpl(this.allMethodMark);

@override
final Node<T> root = NodeImpl("<root>");

@override
late final Map<String, Node<T>> static = {};

@override
final String allMethodMark;
}

/// Creates a new router context.
RouterContext<T> createRouter<T>() => _ContextImpl<T>();
RouterContext<T> createRouter<T>({String allMethodMark = '*'}) =>
_ContextImpl<T>(allMethodMark);
13 changes: 7 additions & 6 deletions lib/src/operations/find_all_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Iterable<MethodData<T>> _findAllMethodData<T>(
// 0. wildcard
if (node.wildcard?.methods
case final Map<String, List<MethodData<T>>> methodMap) {
final values = methodMap[method] ?? methodMap[''];
final values = methodMap[method] ?? methodMap[ctx.allMethodMark];
if (values != null && values.isNotEmpty) {
results.addAll(values);
}
Expand All @@ -42,10 +42,11 @@ Iterable<MethodData<T>> _findAllMethodData<T>(
results.addAll(_findAllMethodData(ctx, node, method, segments, index + 1));
if (node.methods case final Map<String, List<MethodData<T>>> methodMap
when index == segments.length) {
final values = methodMap[method] ?? methodMap[''];
if (values != null &&
values.firstOrNull?.params?.lastOrNull?.optional == true) {
results.addAll(values);
final values = methodMap[method] ?? methodMap[ctx.allMethodMark];
final optionalValues =
values?.where((e) => e.params?.any((e) => e.optional) == true);
if (optionalValues != null) {
results.addAll(optionalValues);
}
}
}
Expand All @@ -58,7 +59,7 @@ Iterable<MethodData<T>> _findAllMethodData<T>(
// 3. ends.
if (node.methods case final Map<String, List<MethodData<T>>> methodMap
when index == segments.length) {
final values = methodMap[method] ?? methodMap[''];
final values = methodMap[method] ?? methodMap[ctx.allMethodMark];
if (values != null && values.isNotEmpty) {
results.addAll(values);
}
Expand Down
14 changes: 9 additions & 5 deletions lib/src/operations/find_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ MatchedRoute<T>? findRoute<T>(
// 0. global static matched.
if (ctx.static[normalizedPath]
case Node<T>(methods: final Map<String, List<MethodData<T>>> methodMap)) {
final values = methodMap[method] ?? methodMap[''];
final values = methodMap[method] ?? methodMap[ctx.allMethodMark];
if (values?.firstOrNull?.data case final data when data is T) {
return MatchedRoute(data: data);
}
Expand All @@ -40,14 +40,16 @@ Iterable<MethodData<T>>? _lookupTree<T>(
// 0. Ends
if (index == segments.length) {
if (node.methods != null) {
final values = node.methods?[method] ?? node.methods?[''];
final values = node.methods?[method] ?? node.methods?[ctx.allMethodMark];
if (values != null) return values;
}

// Fallback to dynamic for last child (/test and /test/ matches /test/*)
if (node.param case Node<T>(methods: final methodMap)
when methodMap != null) {
final values = methodMap[method] ?? methodMap[''];
final values = methodMap[method] ?? methodMap[ctx.allMethodMark];

// The reason for only checking first here is that findRoute only returns the first match.
if (values != null &&
values.firstOrNull?.params?.lastOrNull?.optional == true) {
return values;
Expand All @@ -56,7 +58,9 @@ Iterable<MethodData<T>>? _lookupTree<T>(

if (node.wildcard case Node<T>(methods: final methodMap)
when methodMap != null) {
final values = methodMap[method] ?? methodMap[''];
final values = methodMap[method] ?? methodMap[ctx.allMethodMark];

// The reason for only checking first here is that findRoute only returns the first match.
if (values != null &&
values.firstOrNull?.params?.lastOrNull?.optional == true) {
return values;
Expand All @@ -83,7 +87,7 @@ Iterable<MethodData<T>>? _lookupTree<T>(
// 3. wildcard
if (node.wildcard case Node<T>(methods: final methodMap)
when methodMap != null) {
return methodMap[method] ?? methodMap[''];
return methodMap[method] ?? methodMap[ctx.allMethodMark];
}

// No match
Expand Down
3 changes: 3 additions & 0 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ abstract interface class RouterContext<T> {

/// all path segment is statice nodes.
Map<String, Node<T>> get static;

/// Defined all http method mark.
String get allMethodMark;
}

/// Method data params metadata.
Expand Down

0 comments on commit 89a1772

Please sign in to comment.