Skip to content

Commit

Permalink
chore: *
Browse files Browse the repository at this point in the history
  • Loading branch information
Seven Du committed Jul 26, 2024
1 parent 9b7cfae commit 0e345eb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Publish to pub.dev

on:
public:
tags:
- "v[0-9]+.[0-9]+.[0-9]+*"

jobs:
publish:
permissions:
id-token: write
uses: dart-lang/setup-dart/.github/workflows/publish.yml@v1
18 changes: 10 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
name: test

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

jobs:
build:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -16,10 +18,10 @@ jobs:
run: dart format --set-exit-if-changed --output=none .
- name: Analyze project source
run: dart analyze
- name: Run tests
run: dart test --coverage coverage
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4.5.0
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: coverage
# - name: Run tests
# run: dart test --coverage coverage
# - name: Upload coverage reports to Codecov
# uses: codecov/codecov-action@v4.5.0
# with:
# token: ${{ secrets.CODECOV_TOKEN }}
# directory: coverage
2 changes: 2 additions & 0 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ void main() {

// `null`, No match.
print(router.find('get', '/'));

print(router.findAll('get', '/path/seven').map((e) => e.toMap()));
}
43 changes: 22 additions & 21 deletions lib/src/_router_mixins/find_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,61 @@ mixin FindAll<T> on Router<T> {
includeNonMethod = method == null ? false : includeNonMethod;

final segments = splitPath(path);
final matches = <MethodData<T>>[];

// match all method data.
_matchAll(context.root, normalizeHttpMethod(method), segments, 0,
includeNonMethod, matches);
final matches = _findAllRoutes(context.root, normalizeHttpMethod(method),
segments, 0, includeNonMethod);

return matches
.map((e) => MatchedRoute(e.data, toParams(e.params, segments)));
}

static void _matchAll<T>(
Node<T> node,
String? method,
Iterable<String> segments,
int index,
bool includeNonMethod,
List<MethodData<T>> matches) {
static Iterable<MethodData<T>> _findAllRoutes<T>(Node<T> node, String? method,
Iterable<String> segments, int index, bool includeNonMethod) {
final results = <MethodData<T>>[];

// Step 1, Wildcard
if (node.wildcard != null) {
final results = node.wildcard!.methods?[method];
if (results != null) matches.addAll(results);
final values = node.wildcard!.methods?[method];
if (values != null) results.addAll(values);
if (includeNonMethod) {
final include = node.wildcard!.methods?[null];
if (include != null) matches.addAll(include);
if (include != null) results.addAll(include);
}
}

// Step 2, Param
if (node.param != null) {
_matchAll(node, method, segments, index + 1, includeNonMethod, matches);
results.addAll(_findAllRoutes(
node.param!, method, segments, index + 1, includeNonMethod));

if (index == segments.length) {
final results = node.param!.methods?[method];
if (results != null) matches.addAll(matches);
final values = node.param!.methods?[method];
if (values != null) results.addAll(values);
if (includeNonMethod) {
final include = node.param!.methods?[null];
if (include != null) matches.addAll(include);
if (include != null) results.addAll(include);
}
}
}

// Step 3, Static
final static = node.static?[segments.elementAtOrNull(index)];
if (static != null) {
_matchAll(node, method, segments, index + 1, includeNonMethod, matches);
results.addAll(_findAllRoutes(
static, method, segments, index + 1, includeNonMethod));
}

// End of path.
if (index == segments.length) {
final results = node.methods?[method];
if (results != null) matches.addAll(results);
final values = node.methods?[method];
if (values != null) results.addAll(values);
if (includeNonMethod) {
final include = node.methods?[null];
if (include != null) matches.addAll(include);
if (include != null) results.addAll(include);
}
}

return results;
}
}

0 comments on commit 0e345eb

Please # to comment.