Skip to content

Commit 207cb8f

Browse files
author
Olivier Auverlot
committed
If a segment in the pattern is ''%d'', it will match any non-empty segment that is an integer. If a segment in the pattern is ''%s'', it will match any non-empty segment in the path
1 parent 05fff46 commit 207cb8f

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

examples/official-site/sqlpage/migrations/08_functions.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,21 +549,21 @@ VALUES (
549549
550550
```sql
551551
select ''text'' AS component;
552-
select sqlpage.is_path_matching(sqlpage.path(),''/api/%/%/%'') AS contents;
552+
select sqlpage.is_path_matching(sqlpage.path(),''/api/v1/user/%d/%s'') AS contents;
553553
```
554554
555555
#### Result
556556
557-
`/api/v1/user/42`
557+
`/api/v1/user/42/name`
558558
559559
#### Notes
560560
561561
- The pattern is a list of segments separated by ''/''.
562562
- If the path is NULL, or the pattern is NULL, it will return an empty string.
563563
- If the path and pattern have different numbers of segments, it will return an empty string.
564564
- If the path and pattern have the same number of segments, it will compare them segment by segment.
565-
- If a segment in the pattern is ''%'', it will match any non-empty segment in the path.
566-
- If a segment in the pattern is a string, it will match the corresponding segment in the path if they are equal.
565+
- If a segment in the pattern is ''%d'', it will match any non-empty segment that is an integer.
566+
- If a segment in the pattern is ''%s'', it will match any non-empty segment in the path.
567567
- If all segments match, it will return the path.
568568
- Otherwise, it will return an empty string.
569569
'

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,15 @@ async fn is_path_matching<'a>(
829829
}
830830

831831
for (ps, pat_s) in path_segments.iter().zip(pattern_segments.iter()) {
832-
if *pat_s == "%" {
832+
if *pat_s == "%s" {
833833
if ps.is_empty() {
834834
return Some(Cow::Borrowed(""));
835835
}
836+
} else if *pat_s == "%d" {
837+
let ps_decoded = percent_encoding::percent_decode_str(ps).decode_utf8_lossy();
838+
if ps_decoded.is_empty() || ps_decoded.parse::<i64>().is_err() {
839+
return Some(Cow::Borrowed(""));
840+
}
836841
} else {
837842
let ps_decoded = percent_encoding::percent_decode_str(ps).decode_utf8_lossy();
838843
let pat_s_decoded = percent_encoding::percent_decode_str(pat_s).decode_utf8_lossy();

0 commit comments

Comments
 (0)