From c3cc7aec0fe245a0b2f9292810a13ef8ee24ed86 Mon Sep 17 00:00:00 2001 From: Seven Du Date: Mon, 9 Sep 2024 12:20:28 +0800 Subject: [PATCH] feat: Support nullable method. --- CHANGELOG.md | 4 ++++ lib/src/context.dart | 8 +------- lib/src/operations/add_route.dart | 4 ++-- lib/src/operations/find_all_routes.dart | 10 +++++----- lib/src/operations/find_route.dart | 14 +++++++------- lib/src/types.dart | 3 --- pubspec.yaml | 2 +- 7 files changed, 20 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b27b81..96db38e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v4.1.0 + +- **feat**: Support nullable method. + ## v4.0.0 [compare changes](https://github.com/medz/routingkit/compare/v3.0.3...v4.0.0) diff --git a/lib/src/context.dart b/lib/src/context.dart index 9001034..b7b8799 100644 --- a/lib/src/context.dart +++ b/lib/src/context.dart @@ -2,18 +2,12 @@ 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({String allMethodMark = '*'}) => - _ContextImpl(allMethodMark); +RouterContext createRouter() => _ContextImpl(); diff --git a/lib/src/operations/add_route.dart b/lib/src/operations/add_route.dart index ffd644e..7d887e7 100644 --- a/lib/src/operations/add_route.dart +++ b/lib/src/operations/add_route.dart @@ -3,7 +3,7 @@ import '../_internal/utils.dart'; import '../types.dart'; /// Adds a route to the router context. -void addRoute(RouterContext ctx, String method, String path, T data) { +void addRoute(RouterContext ctx, String? method, String path, T data) { final segments = toPathSegments(path); final ParamsMetadata params = []; @@ -44,7 +44,7 @@ void addRoute(RouterContext ctx, String method, String path, T data) { } // Assign params and data to the node. - (node.methods ??= {}).putIfAbsent(method, () => []).add(MethodData( + (node.methods ??= {}).putIfAbsent(method ?? '', () => []).add(MethodData( data: data, params: params.isNotEmpty ? params : null, )); diff --git a/lib/src/operations/find_all_routes.dart b/lib/src/operations/find_all_routes.dart index 3632211..b78dc60 100644 --- a/lib/src/operations/find_all_routes.dart +++ b/lib/src/operations/find_all_routes.dart @@ -4,7 +4,7 @@ import '../types.dart'; /// Find all route patterns that match the given [path]. Iterable> findAllRoutes( RouterContext ctx, - String method, + String? method, String path, { bool params = true, }) { @@ -22,7 +22,7 @@ Iterable> findAllRoutes( Iterable> _findAllMethodData( RouterContext ctx, Node node, - String method, + String? method, Iterable segments, int index, ) { @@ -31,7 +31,7 @@ Iterable> _findAllMethodData( // 0. wildcard if (node.wildcard?.methods case final Map>> methodMap) { - final values = methodMap[method] ?? methodMap[ctx.allMethodMark]; + final values = methodMap[method] ?? methodMap['']; if (values != null && values.isNotEmpty) { results.addAll(values); } @@ -42,7 +42,7 @@ 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[ctx.allMethodMark]; + final values = methodMap[method] ?? methodMap['']; final optionalValues = values?.where((e) => e.params?.any((e) => e.optional) == true); if (optionalValues != null) { @@ -59,7 +59,7 @@ Iterable> _findAllMethodData( // 3. ends. if (node.methods case final Map>> methodMap when index == segments.length) { - final values = methodMap[method] ?? methodMap[ctx.allMethodMark]; + final values = methodMap[method] ?? methodMap['']; 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 29dc881..b729207 100644 --- a/lib/src/operations/find_route.dart +++ b/lib/src/operations/find_route.dart @@ -4,7 +4,7 @@ import '../types.dart'; /// Find a route. MatchedRoute? findRoute( RouterContext ctx, - String method, + String? method, String path, { bool params = true, }) { @@ -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[ctx.allMethodMark]; + final values = methodMap[method] ?? methodMap['']; if (values?.firstOrNull?.data case final data when data is T) { return MatchedRoute(data: data); } @@ -33,21 +33,21 @@ MatchedRoute? findRoute( Iterable>? _lookupTree( RouterContext ctx, Node node, - String method, + String? method, Iterable segments, int index, ) { // 0. Ends if (index == segments.length) { if (node.methods != null) { - final values = node.methods?[method] ?? node.methods?[ctx.allMethodMark]; + final values = node.methods?[method] ?? node.methods?['']; 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[ctx.allMethodMark]; + final values = methodMap[method] ?? methodMap['']; // The reason for only checking first here is that findRoute only returns the first match. if (values != null && @@ -58,7 +58,7 @@ Iterable>? _lookupTree( if (node.wildcard case Node(methods: final methodMap) when methodMap != null) { - final values = methodMap[method] ?? methodMap[ctx.allMethodMark]; + final values = methodMap[method] ?? methodMap['']; // The reason for only checking first here is that findRoute only returns the first match. if (values != null && @@ -87,7 +87,7 @@ Iterable>? _lookupTree( // 3. wildcard if (node.wildcard case Node(methods: final methodMap) when methodMap != null) { - return methodMap[method] ?? methodMap[ctx.allMethodMark]; + return methodMap[method] ?? methodMap['']; } // No match diff --git a/lib/src/types.dart b/lib/src/types.dart index 79c50ea..7227013 100644 --- a/lib/src/types.dart +++ b/lib/src/types.dart @@ -5,9 +5,6 @@ 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. diff --git a/pubspec.yaml b/pubspec.yaml index 2885da1..2d74bca 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ description: >- Routing Kit - Lightweight and fast router. - A composable pure function routing kit. -version: 4.0.0 +version: 4.1.0 repository: https://github.com/medz/routingkit funding: