Skip to content

Support star pattern in URL router #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
support star pattern
  • Loading branch information
Archmonger committed Jan 29, 2024
commit 1c39865f3b42abe58a5c84694a1ac3154ca381ec
5 changes: 5 additions & 0 deletions src/reactpy_django/router/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def resolve(self, path: str) -> tuple[Any, dict[str, Any]] | None:

# TODO: Make reactpy_router's parse_path generic enough to where we don't have to define our own
def parse_path(path: str) -> tuple[re.Pattern[str], ConverterMapping]:
# Convert path to regex pattern, and make sure to interpret the registered converters (ex. <int:foo>)
pattern = "^"
last_match_end = 0
converters: ConverterMapping = {}
Expand All @@ -50,6 +51,10 @@ def parse_path(path: str) -> tuple[re.Pattern[str], ConverterMapping]:
converters[param_name] = param_conv["func"]
last_match_end = match.end()
pattern += f"{re.escape(path[last_match_end:])}$"

# Replace literal `*` with "match anything" regex pattern
pattern = pattern.replace("\*", ".*")

return re.compile(pattern), converters


Expand Down
13 changes: 13 additions & 0 deletions tests/test_app/router/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,17 @@ def main():
"/router/two/<int:value>/<str:value2>/",
display_params("Path 9", route_info),
),
route(
"/router/multi-tier-route/",
display_params("Path 10", route_info),
route("one/", display_params("Path 11", route_info)),
route("two/", display_params("Path 12", route_info)),
route("*", display_params("Path 13", route_info)),
),
route(
"/router/star-in-middle/*/",
display_params("Path 14", route_info),
route("one/", display_params("Path 15", route_info)),
route("two/", display_params("Path 16", route_info)),
),
)