Skip to content

Commit

Permalink
feat: Support nullable method.
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Sep 9, 2024
1 parent 89a1772 commit c3cc7ae
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 1 addition & 7 deletions lib/src/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ 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>({String allMethodMark = '*'}) =>
_ContextImpl<T>(allMethodMark);
RouterContext<T> createRouter<T>() => _ContextImpl<T>();
4 changes: 2 additions & 2 deletions lib/src/operations/add_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import '../_internal/utils.dart';
import '../types.dart';

/// Adds a route to the router context.
void addRoute<T>(RouterContext<T> ctx, String method, String path, T data) {
void addRoute<T>(RouterContext<T> ctx, String? method, String path, T data) {
final segments = toPathSegments(path);
final ParamsMetadata params = [];

Expand Down Expand Up @@ -44,7 +44,7 @@ void addRoute<T>(RouterContext<T> 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,
));
Expand Down
10 changes: 5 additions & 5 deletions lib/src/operations/find_all_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '../types.dart';
/// Find all route patterns that match the given [path].
Iterable<MatchedRoute<T>> findAllRoutes<T>(
RouterContext<T> ctx,
String method,
String? method,
String path, {
bool params = true,
}) {
Expand All @@ -22,7 +22,7 @@ Iterable<MatchedRoute<T>> findAllRoutes<T>(
Iterable<MethodData<T>> _findAllMethodData<T>(
RouterContext<T> ctx,
Node<T> node,
String method,
String? method,
Iterable<String> segments,
int index,
) {
Expand All @@ -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[ctx.allMethodMark];
final values = methodMap[method] ?? methodMap[''];
if (values != null && values.isNotEmpty) {
results.addAll(values);
}
Expand All @@ -42,7 +42,7 @@ 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[ctx.allMethodMark];
final values = methodMap[method] ?? methodMap[''];
final optionalValues =
values?.where((e) => e.params?.any((e) => e.optional) == true);
if (optionalValues != null) {
Expand All @@ -59,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[ctx.allMethodMark];
final values = methodMap[method] ?? methodMap[''];
if (values != null && values.isNotEmpty) {
results.addAll(values);
}
Expand Down
14 changes: 7 additions & 7 deletions lib/src/operations/find_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '../types.dart';
/// Find a route.
MatchedRoute<T>? findRoute<T>(
RouterContext<T> ctx,
String method,
String? method,
String path, {
bool params = true,
}) {
Expand All @@ -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[ctx.allMethodMark];
final values = methodMap[method] ?? methodMap[''];
if (values?.firstOrNull?.data case final data when data is T) {
return MatchedRoute(data: data);
}
Expand All @@ -33,21 +33,21 @@ MatchedRoute<T>? findRoute<T>(
Iterable<MethodData<T>>? _lookupTree<T>(
RouterContext<T> ctx,
Node<T> node,
String method,
String? method,
Iterable<String> 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<T>(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 &&
Expand All @@ -58,7 +58,7 @@ Iterable<MethodData<T>>? _lookupTree<T>(

if (node.wildcard case Node<T>(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 &&
Expand Down Expand Up @@ -87,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[ctx.allMethodMark];
return methodMap[method] ?? methodMap[''];
}

// No match
Expand Down
3 changes: 0 additions & 3 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ 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
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c3cc7ae

Please sign in to comment.