From 0e345eb539fadf1166c7249a5fcc1fb37b5e22d4 Mon Sep 17 00:00:00 2001 From: Seven Du Date: Sat, 27 Jul 2024 06:35:36 +0800 Subject: [PATCH] chore: * --- .github/workflows/publish.yml | 12 ++++++++ .github/workflows/test.yml | 18 ++++++------ example/main.dart | 2 ++ lib/src/_router_mixins/find_all.dart | 43 ++++++++++++++-------------- 4 files changed, 46 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..d47ed6e --- /dev/null +++ b/.github/workflows/publish.yml @@ -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 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fef01c7..d28eb49 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 @@ -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 diff --git a/example/main.dart b/example/main.dart index a639e25..daf988e 100644 --- a/example/main.dart +++ b/example/main.dart @@ -24,4 +24,6 @@ void main() { // `null`, No match. print(router.find('get', '/')); + + print(router.findAll('get', '/path/seven').map((e) => e.toMap())); } diff --git a/lib/src/_router_mixins/find_all.dart b/lib/src/_router_mixins/find_all.dart index 8de31c5..c74e56b 100644 --- a/lib/src/_router_mixins/find_all.dart +++ b/lib/src/_router_mixins/find_all.dart @@ -9,42 +9,40 @@ mixin FindAll on Router { includeNonMethod = method == null ? false : includeNonMethod; final segments = splitPath(path); - final matches = >[]; // 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( - Node node, - String? method, - Iterable segments, - int index, - bool includeNonMethod, - List> matches) { + static Iterable> _findAllRoutes(Node node, String? method, + Iterable segments, int index, bool includeNonMethod) { + final results = >[]; + // 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); } } } @@ -52,17 +50,20 @@ mixin FindAll on Router { // 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; } }