Skip to content

fix: handle leading forward slash in pattern matching path function #116

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 4 commits into from
Jul 17, 2023
Merged
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
improve implementation
  • Loading branch information
russellwheatley committed Jul 10, 2023
commit 9bca662a528d17d6166cb5db7f24e14d1455dfdd
16 changes: 6 additions & 10 deletions src/firebase_functions/private/path_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ class PathPattern:
segments: list[PathSegment]

def __init__(self, raw_path: str):
self.raw = raw_path
normalized_path = raw_path.strip("/")
self.raw = normalized_path
self.segments = []
self.init_path_segments(raw_path)
self.init_path_segments(normalized_path)

def init_path_segments(self, raw: str):
parts = raw.split("/")
Expand Down Expand Up @@ -164,19 +165,14 @@ def has_captures(self) -> bool:
for segment in self.segments)

def extract_matches(self, path: str) -> dict[str, str]:
segments = self.segments
if self.segments[0].value == "":
# if leading slash, there will be an empty segment which increases
# the path index, we pop to remove it
segments.pop(0)
matches: dict[str, str] = {}
if not self.has_captures:
return matches
path_segments = path_parts(path)
path_ndx = 0
for segment_ndx in range(len(segments)):
segment = segments[segment_ndx]
remaining_segments = len(segments) - 1 - segment_ndx
for segment_ndx in range(len(self.segments)):
segment = self.segments[segment_ndx]
remaining_segments = len(self.segments) - 1 - segment_ndx
next_path_ndx = len(path_segments) - remaining_segments
if segment.name == SegmentName.SINGLE_CAPTURE:
matches[segment.trimmed] = path_segments[path_ndx]
Expand Down