Skip to content

feat: Allow a trie to be attached as a subtrie to another - #61

Merged
nielsenko merged 7 commits into
serverpod:mainfrom
nielsenko:trie-attach
May 9, 2025
Merged

nielsenko merged 7 commits into
serverpod:mainfrom
nielsenko:trie-attach

Conversation

@nielsenko

@nielsenko nielsenko commented May 8, 2025 •

Copy link
Copy Markdown
Collaborator

Description

Allows tries to be composed by attaching one below another.

Related Issues

Pre-Launch Checklist

Please ensure that your PR meets the following requirements before submitting:

  • This update focuses on a single feature or bug fix. (For multiple fixes, please submit separate PRs.)
  • I have read and followed the Dart Style Guide and formatted the code using dart format.
  • I have referenced at least one issue this PR fixes or is related to.
  • I have updated/added relevant documentation (doc comments with ///), ensuring consistency with existing project documentation.
  • I have added new tests to verify the changes.
  • All existing and new tests pass successfully.
  • I have documented any breaking changes below.

Breaking Changes

  • Includes breaking changes.
  • No breaking changes.

Summary by CodeRabbit

  • New Features

    • Added the ability to extract sub-paths, get path length, and detect parameters in path segments.
    • Introduced a method to attach one route trie to another at a specified subpath, enabling more flexible route management.
  • Bug Fixes

    • Improved error handling to prevent duplicate route registrations and invalid trie attachments.
  • Tests

    • Added comprehensive tests for attaching tries, including parameterized routes and error scenarios.

@coderabbitai

coderabbitai Bot commented May 8, 2025 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

The changes introduce new utility methods to the NormalizedPath class, refactor the PathTrie class to support attaching another trie at a subpath, and update the router to use the new hasParameters property. Comprehensive tests for the new attach functionality in PathTrie are also added.

Changes

File(s) Change Summary
lib/src/router/normalized_path.dart Added subPath, length, and hasParameters members to NormalizedPath; explicitly typed path as String.
lib/src/router/path_trie.dart Refactored add to use a new private _build method; added public attach method for attaching another PathTrie at a subpath.
lib/src/router/router.dart Removed private static _isDynamic method; updated to use NormalizedPath.hasParameters property for dynamic route detection.
test/router/path_trie_test.dart Added a new test group for the attach method, covering normal, error, parameterized, and nested parameterized attachment scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant ParentTrie as Parent PathTrie
    participant ChildTrie as Child PathTrie
    participant User as User/Test

    User->>ParentTrie: attach(subPath, ChildTrie)
    ParentTrie->>ParentTrie: _build(subPath prefix)
    ParentTrie->>ParentTrie: Attach ChildTrie.root at subPath.last
    User->>ParentTrie: lookup(fullPath)
    ParentTrie->>ChildTrie: lookup(remainingPath)
    ChildTrie-->>ParentTrie: Return value (with parameters if any)
    ParentTrie-->>User: Return value (with merged parameters if any)
Loading

Assessment against linked issues

Objective Addressed Explanation
Support attaching a trie to another (#58) ✅

Possibly related PRs

  • feat: Router class #52: Introduces the initial Router and NormalizedPath implementations, which are directly extended and modified in this PR.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov

codecov Bot commented May 8, 2025 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 88.88889% with 3 lines in your changes missing coverage. Please review.

Project coverage is 80.07%. Comparing base (e1e0bf7) to head (619415c).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/src/router/normalized_path.dart 40.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #61      +/-   ##
==========================================
+ Coverage   80.04%   80.07%   +0.03%     
==========================================
  Files          78       78              
  Lines        2260     2279      +19     
  Branches     1283     1289       +6     
==========================================
+ Hits         1809     1825      +16     
- Misses        451      454       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nielsenko

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented May 8, 2025

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nielsenko
nielsenko requested a review from SandPod May 8, 2025 09:26
@nielsenko nielsenko self-assigned this May 8, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
lib/src/router/normalized_path.dart (1)

60-61: Missing documentation for the subPath method.

While the length and hasParameters members have documentation, the subPath method lacks documentation explaining its purpose and parameters.

-  ///
+  /// Returns a new [NormalizedPath] representing a subpath of this path.
+  /// 
+  /// The [start] parameter specifies the starting segment index (inclusive).
+  /// The optional [end] parameter specifies the ending segment index (exclusive).
   NormalizedPath subPath(final int start, [final int? end]) =>
       NormalizedPath._(segments.sublist(start, end));
lib/src/router/path_trie.dart (1)

104-131: Robust implementation of the new attach method.

The attach method:

  • Has comprehensive documentation
  • Performs appropriate validation of the attachment path
  • Prevents invalid attachments at the root or existing paths
  • Efficiently reuses the _build method for node traversal

A small suggestion to enhance error messages:

- throw ArgumentError('Path already exists');
+ throw ArgumentError('Path $normalizedPath already exists');

This would provide more context in the error message, making debugging easier.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e1e0bf7 and 1b640dd.

📒 Files selected for processing (4)
  • lib/src/router/normalized_path.dart (1 hunks)
  • lib/src/router/path_trie.dart (2 hunks)
  • lib/src/router/router.dart (1 hunks)
  • test/router/path_trie_test.dart (1 hunks)
🔇 Additional comments (6)
lib/src/router/router.dart (1)

36-36: Great simplification using the new NormalizedPath property.

The use of the direct hasParameters property instead of a separate helper method makes the code cleaner and more maintainable. This change leverages the enhanced functionality from the NormalizedPath class appropriately.

test/router/path_trie_test.dart (1)

334-442: Well-structured and comprehensive tests for the new attach functionality.

The test group covers all essential aspects of the new feature:

  • Basic attachment functionality
  • Error cases (root path, existing path)
  • Dynamic updates to attached tries
  • Parameter handling in simple and complex scenarios

Good use of descriptive BDD-style test naming patterns that clearly explain the test scenarios.

lib/src/router/normalized_path.dart (2)

64-67: Good addition of utility properties.

The length and hasParameters properties provide useful utilities for path inspection and improve the expressiveness of code using normalized paths.


72-72: Appropriate type annotation for clarity.

Adding the explicit String type to the path field improves code readability and type safety.

lib/src/router/path_trie.dart (2)

51-63: Good refactoring of the add method.

The refactoring to use the new _build helper method improves code organization by separating node creation from value assignment. This makes the code more maintainable and reusable for both add and attach methods.


66-102: Well-implemented helper method for node traversal.

The _build method correctly handles both literal and parameterized segments, validates parameter names, and consistently applies error checking. This ensures path integrity while building the trie structure.

@nielsenko

Copy link
Copy Markdown
Collaborator Author

@coderabbitai Fixed nits

@nielsenko
nielsenko marked this pull request as ready for review May 8, 2025 11:18
@coderabbitai

coderabbitai Bot commented May 8, 2025

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

@nielsenko Thanks for fixing those nits! It's great to see your attention to detail in polishing the code. The PR looks good with those improvements. Let me know if you need any further assistance with this PR.

(._.)
( _(\ ~ Good job fixing those nits!
(>_/)


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of comments and questions.

Comment thread test/router/path_trie_test.dart Resolved
Comment thread test/router/path_trie_test.dart Resolved
Comment thread test/router/path_trie_test.dart Resolved
@nielsenko
nielsenko requested a review from SandPod May 8, 2025 14:55

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now!

Last minor thing to fix.

Comment thread test/router/path_trie_test.dart Resolved
Comment thread test/router/path_trie_test.dart Resolved
@nielsenko
nielsenko merged commit ed56e37 into serverpod:main May 9, 2025
@coderabbitai coderabbitai Bot mentioned this pull request May 9, 2025
8 of 9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support attaching a trie to another

2 participants