Skip to content

Commit a31396c

Browse files
committed
Add full splat to splat
Signed-off-by: Rudi Grinberg <[email protected]>
1 parent 27a6131 commit a31396c

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

opium/src/router.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ module Params = struct
119119
| Param (Some name, route), p :: captured ->
120120
let acc = { acc with named = (name, p) :: acc.named } in
121121
loop acc route captured
122-
| Full_splat, _ :: _ -> assert false
122+
| Full_splat, rest -> { acc with unnamed = List.rev_append rest acc.unnamed }
123123
| Param (_, _), [] -> assert false
124124
| Nil, _ :: _ -> assert false
125125
in
@@ -167,7 +167,7 @@ let match_url t url =
167167
in
168168
let rec loop t captured tokens =
169169
match t with
170-
| Accept (a, route) -> accept a route captured
170+
| Accept (a, route) -> accept a route (List.rev_append tokens captured)
171171
| Node t ->
172172
(match tokens with
173173
| [ "" ] | [] ->
@@ -205,6 +205,7 @@ let match_route t route =
205205
in
206206
let by_param = by_param t.param route in
207207
let by_literal =
208+
(* TODO remove duplication with [Param] case *)
208209
Smap.fold (fun _ node acc -> loop node route :: acc) t.literal [] |> List.concat
209210
in
210211
List.concat [ here; by_param; by_literal ]

opium/test/opium_router_tests.ml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ let%expect_test "full splat node matches" =
155155
test "/foo/";
156156
[%expect
157157
{|
158-
matched with params: ((named ()) (unnamed ()))
159-
matched with params: ((named ()) (unnamed ()))
160-
matched with params: ((named ()) (unnamed ())) |}]
158+
matched with params: ((named ()) (unnamed (bar)))
159+
matched with params: ((named ()) (unnamed (bar foo)))
160+
matched with params: ((named ()) (unnamed (""))) |}]
161161
;;
162162
163163
let%expect_test "full splat + collision checking" =
@@ -190,10 +190,13 @@ let%expect_test "full splat" =
190190
let router = of_routes' [ "/**" ] in
191191
let test = test_match_url router in
192192
test "/test";
193+
test "/test/";
193194
test "/";
194195
test "/user/123/foo/bar";
195-
[%expect{|
196-
matched with params: ((named ()) (unnamed ()))
197-
matched with params: ((named ()) (unnamed ()))
198-
matched with params: ((named ()) (unnamed ())) |}]
196+
[%expect
197+
{|
198+
matched with params: ((named ()) (unnamed (test)))
199+
matched with params: ((named ()) (unnamed (test "")))
200+
matched with params: ((named ()) (unnamed ("")))
201+
matched with params: ((named ()) (unnamed (user 123 foo bar))) |}]
199202
;;

0 commit comments

Comments
 (0)