From 89a17720eaa7d29818bf064895317d2aa5fcdd9e Mon Sep 17 00:00:00 2001 From: Seven Du Date: Mon, 9 Sep 2024 11:50:56 +0800 Subject: [PATCH] feat: support custom all method mark --- lib/src/context.dart | 8 +++++++- lib/src/operations/find_all_routes.dart | 13 +++++++------ lib/src/operations/find_route.dart | 14 +++++++++----- lib/src/types.dart | 3 +++ 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/src/context.dart b/lib/src/context.dart index b7b8799..9001034 100644 --- a/lib/src/context.dart +++ b/lib/src/context.dart @@ -2,12 +2,18 @@ import '_internal/node_impl.dart'; import 'types.dart'; final class _ContextImpl implements RouterContext { + _ContextImpl(this.allMethodMark); + @override final Node root = NodeImpl(""); @override late final Map> static = {}; + + @override + final String allMethodMark; } /// Creates a new router context. -RouterContext createRouter() => _ContextImpl(); +RouterContext createRouter({String allMethodMark = '*'}) => + _ContextImpl(allMethodMark); diff --git a/lib/src/operations/find_all_routes.dart b/lib/src/operations/find_all_routes.dart index d94321f..3632211 100644 --- a/lib/src/operations/find_all_routes.dart +++ b/lib/src/operations/find_all_routes.dart @@ -31,7 +31,7 @@ Iterable> _findAllMethodData( // 0. wildcard if (node.wildcard?.methods case final Map>> methodMap) { - final values = methodMap[method] ?? methodMap['']; + final values = methodMap[method] ?? methodMap[ctx.allMethodMark]; if (values != null && values.isNotEmpty) { results.addAll(values); } @@ -42,10 +42,11 @@ Iterable> _findAllMethodData( results.addAll(_findAllMethodData(ctx, node, method, segments, index + 1)); if (node.methods case final Map>> 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); } } } @@ -58,7 +59,7 @@ Iterable> _findAllMethodData( // 3. ends. if (node.methods case final Map>> 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); } diff --git a/lib/src/operations/find_route.dart b/lib/src/operations/find_route.dart index 1e77032..29dc881 100644 --- a/lib/src/operations/find_route.dart +++ b/lib/src/operations/find_route.dart @@ -14,7 +14,7 @@ MatchedRoute? findRoute( // 0. global static matched. if (ctx.static[normalizedPath] case Node(methods: final Map>> 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); } @@ -40,14 +40,16 @@ Iterable>? _lookupTree( // 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(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; @@ -56,7 +58,9 @@ Iterable>? _lookupTree( if (node.wildcard case Node(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; @@ -83,7 +87,7 @@ Iterable>? _lookupTree( // 3. wildcard if (node.wildcard case Node(methods: final methodMap) when methodMap != null) { - return methodMap[method] ?? methodMap['']; + return methodMap[method] ?? methodMap[ctx.allMethodMark]; } // No match diff --git a/lib/src/types.dart b/lib/src/types.dart index 7227013..79c50ea 100644 --- a/lib/src/types.dart +++ b/lib/src/types.dart @@ -5,6 +5,9 @@ abstract interface class RouterContext { /// all path segment is statice nodes. Map> get static; + + /// Defined all http method mark. + String get allMethodMark; } /// Method data params metadata.