Skip to content

Commit

Permalink
Add full splat to splat
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg authored and anuragsoni committed Nov 20, 2021
1 parent c29bb9b commit 9dad5ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
5 changes: 3 additions & 2 deletions opium/src/router.ml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module Params = struct
| Param (Some name, route), p :: captured ->
let acc = { acc with named = (name, p) :: acc.named } in
loop acc route captured
| Full_splat, _ :: _ -> assert false
| Full_splat, rest -> { acc with unnamed = List.rev_append rest acc.unnamed }
| Param (_, _), [] -> assert false
| Nil, _ :: _ -> assert false
in
Expand Down Expand Up @@ -167,7 +167,7 @@ let match_url t url =
in
let rec loop t captured tokens =
match t with
| Accept (a, route) -> accept a route captured
| Accept (a, route) -> accept a route (List.rev_append tokens captured)
| Node t ->
(match tokens with
| [ "" ] | [] ->
Expand Down Expand Up @@ -205,6 +205,7 @@ let match_route t route =
in
let by_param = by_param t.param route in
let by_literal =
(* TODO remove duplication with [Param] case *)
Smap.fold (fun _ node acc -> loop node route :: acc) t.literal [] |> List.concat
in
List.concat [ here; by_param; by_literal ]
Expand Down
17 changes: 11 additions & 6 deletions opium/test/opium_router_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ let%expect_test "full splat node matches" =
test "/foo/";
[%expect
{|
matched with params: ((named ()) (unnamed ()))
matched with params: ((named ()) (unnamed ()))
matched with params: ((named ()) (unnamed ())) |}]
matched with params: ((named ()) (unnamed (bar)))
matched with params: ((named ()) (unnamed (bar foo)))
matched with params: ((named ()) (unnamed (""))) |}]
;;
let%expect_test "full splat + collision checking" =
Expand Down Expand Up @@ -190,10 +190,15 @@ let%expect_test "full splat" =
let router = of_routes' [ "/**" ] in
let test = test_match_url router in
test "/test";
test "/test/";
test "/";
test "";
test "/user/123/foo/bar";
[%expect{|
matched with params: ((named ()) (unnamed ()))
[%expect
{|
matched with params: ((named ()) (unnamed (test)))
matched with params: ((named ()) (unnamed (test "")))
matched with params: ((named ()) (unnamed ("")))
matched with params: ((named ()) (unnamed ()))
matched with params: ((named ()) (unnamed ())) |}]
matched with params: ((named ()) (unnamed (user 123 foo bar))) |}]
;;

0 comments on commit 9dad5ad

Please sign in to comment.