Skip to content

Commit

Permalink
address pr comment
Browse files Browse the repository at this point in the history
  • Loading branch information
ElijahAhianyo committed May 2, 2024
1 parent 209e10c commit 5ccf111
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
42 changes: 3 additions & 39 deletions reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import multiprocessing
import os
import platform
import re
from typing import (
Any,
AsyncIterator,
Expand Down Expand Up @@ -65,6 +64,7 @@
)
from reflex.route import (
get_route_args,
replace_brackets_with_keywords,
verify_route_validity,
)
from reflex.state import (
Expand Down Expand Up @@ -556,45 +556,9 @@ def _check_routes_conflict(self, new_route: str):
Args:
new_route: the route being newly added.
"""

def replace_brackets_with_keywords(input_string):
# /posts -> /post
# /posts/[slug] -> /posts/__SINGLE_SEGMENT__
# /posts/[slug]/comments -> /posts/__SINGLE_SEGMENT__/comments
# /posts/[[slug]] -> /posts/__DOUBLE_SEGMENT__
# / posts/[[...slug2]]-> /posts/__DOUBLE_CATCHALL_SEGMENT__
# /posts/[...slug3]-> /posts/__SINGLE_CATCHALL_SEGMENT__

# Replace [[...<slug>]] with __DOUBLE_CATCHALL_SEGMENT__
output_string = re.sub(
r"\[\[\.\.\..+?\]\]",
constants.RouteRegex.DOUBLE_CATCHALL_SEGMENT,
input_string,
)
# Replace [...<slug>] with __SINGLE_CATCHALL_SEGMENT__
output_string = re.sub(
r"\[\.\.\..+?\]",
constants.RouteRegex.SINGLE_CATCHALL_SEGMENT,
output_string,
)
# Replace [[<slug>]] with __DOUBLE_SEGMENT__
output_string = re.sub(
r"\[\[.+?\]\]", constants.RouteRegex.DOUBLE_SEGMENT, output_string
)
# Replace [<slug>] with __SINGLE_SEGMENT__
output_string = re.sub(
r"\[.+?\]", constants.RouteRegex.SINGLE_SEGMENT, output_string
)
return output_string

new_replaced_route = replace_brackets_with_keywords(new_route)
if (
constants.RouteRegex.SINGLE_SEGMENT not in new_replaced_route
and constants.RouteRegex.DOUBLE_SEGMENT not in new_replaced_route
and constants.RouteRegex.SINGLE_CATCHALL_SEGMENT not in new_replaced_route
and constants.RouteRegex.DOUBLE_CATCHALL_SEGMENT not in new_replaced_route
):
if "[" not in new_route:
return

segments = (
constants.RouteRegex.SINGLE_SEGMENT,
constants.RouteRegex.DOUBLE_SEGMENT,
Expand Down
39 changes: 39 additions & 0 deletions reflex/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,42 @@ def catchall_prefix(route: str) -> str:
"""
pattern = catchall_in_route(route)
return route.replace(pattern, "") if pattern else ""


def replace_brackets_with_keywords(input_string):
"""Replace brackets and everything inside it in a string with a keyword.
Args:
input_string: String to replace.
Returns:
new string containing keywords.
"""
# /posts -> /post
# /posts/[slug] -> /posts/__SINGLE_SEGMENT__
# /posts/[slug]/comments -> /posts/__SINGLE_SEGMENT__/comments
# /posts/[[slug]] -> /posts/__DOUBLE_SEGMENT__
# / posts/[[...slug2]]-> /posts/__DOUBLE_CATCHALL_SEGMENT__
# /posts/[...slug3]-> /posts/__SINGLE_CATCHALL_SEGMENT__

# Replace [[...<slug>]] with __DOUBLE_CATCHALL_SEGMENT__
output_string = re.sub(
r"\[\[\.\.\..+?\]\]",
constants.RouteRegex.DOUBLE_CATCHALL_SEGMENT,
input_string,
)
# Replace [...<slug>] with __SINGLE_CATCHALL_SEGMENT__
output_string = re.sub(
r"\[\.\.\..+?\]",
constants.RouteRegex.SINGLE_CATCHALL_SEGMENT,
output_string,
)
# Replace [[<slug>]] with __DOUBLE_SEGMENT__
output_string = re.sub(
r"\[\[.+?\]\]", constants.RouteRegex.DOUBLE_SEGMENT, output_string
)
# Replace [<slug>] with __SINGLE_SEGMENT__
output_string = re.sub(
r"\[.+?\]", constants.RouteRegex.SINGLE_SEGMENT, output_string
)
return output_string

0 comments on commit 5ccf111

Please sign in to comment.