Skip to content

Commit d93dda1

Browse files
committed
parse **
Signed-off-by: Rudi Grinberg <[email protected]>
1 parent 11e01a0 commit d93dda1

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

opium/src/router.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ module Route = struct
33

44
type t =
55
| Nil
6+
| Full_splat
67
| Literal of string * t
78
| Param of string option * t
89

910
let rec sexp_of_t (t : t) : Sexplib0.Sexp.t =
1011
match t with
1112
| Nil -> Atom "Nil"
13+
| Full_splat -> Atom "Full_splat"
1214
| Literal (x, y) -> List [ Atom x; sexp_of_t y ]
1315
| Param (x, y) ->
1416
let x : Sexplib0.Sexp.t =
@@ -23,6 +25,7 @@ module Route = struct
2325

2426
let rec parse_tokens params tokens =
2527
match tokens with
28+
| [ "**" ] -> Full_splat
2629
| [] | [ "" ] -> Nil
2730
| token :: tokens ->
2831
if token = ""
@@ -86,6 +89,7 @@ module Params = struct
8689
let create route captured =
8790
let rec loop acc (route : Route.t) captured =
8891
match route, captured with
92+
| Full_splat, _ -> assert false
8993
| Nil, [] -> acc
9094
| Literal (_, route), _ -> loop acc route captured
9195
| Param (None, route), p :: captured ->
@@ -159,6 +163,7 @@ let match_url t url =
159163
let match_route t route =
160164
let rec loop t (route : Route.t) =
161165
match route with
166+
| Full_splat -> assert false
162167
| Nil ->
163168
(match t.data with
164169
| None -> []
@@ -190,6 +195,7 @@ let match_route t route =
190195
let add_no_check t orig_route a =
191196
let rec loop t (route : Route.t) =
192197
match route with
198+
| Full_splat -> assert false
193199
| Nil -> { empty with data = Some (a, orig_route) }
194200
| Literal (lit, route) ->
195201
let literal =

opium/test/opium_router_tests.ml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ let%expect_test "duplicate paramters" =
3737
[%expect {| [FAIL] invalid route duplicate parameter "bar" |}]
3838
;;
3939

40+
let%expect_test "splat in the middle is wrong" =
41+
valid_route "/foo/**/foo";
42+
[%expect {| [FAIL] invalid route double splat allowed only in the end |}]
43+
;;
44+
45+
let%expect_test "splat at the end" =
46+
valid_route "/foo/**";
47+
[%expect {| [PASS] valid route |}]
48+
;;
49+
4050
let test_match_url router url =
4151
match Router.match_url router url with
4252
| None -> print_endline "no match"
@@ -93,7 +103,7 @@ let%expect_test "ambiguity in routes" =
93103
(Failure "duplicate routes")
94104
Raised at Stdlib.failwith in file "stdlib.ml", line 29, characters 17-33
95105
Called from Stdlib__list.fold_left in file "list.ml", line 121, characters 24-34
96-
Called from Opium_tests__Opium_router_tests.(fun) in file "opium/test/opium_router_tests.ml", line 85, characters 2-49
106+
Called from Opium_tests__Opium_router_tests.(fun) in file "opium/test/opium_router_tests.ml", line 95, characters 2-49
97107
Called from Expect_test_collector.Make.Instance.exec in file "collector/expect_test_collector.ml", line 244, characters 12-19 |}]
98108
;;
99109

@@ -109,7 +119,7 @@ let%expect_test "ambiguity in routes 2" =
109119
(Failure "duplicate routes")
110120
Raised at Stdlib.failwith in file "stdlib.ml", line 29, characters 17-33
111121
Called from Stdlib__list.fold_left in file "list.ml", line 121, characters 24-34
112-
Called from Opium_tests__Opium_router_tests.(fun) in file "opium/test/opium_router_tests.ml", line 101, characters 2-43
122+
Called from Opium_tests__Opium_router_tests.(fun) in file "opium/test/opium_router_tests.ml", line 111, characters 2-43
113123
Called from Expect_test_collector.Make.Instance.exec in file "collector/expect_test_collector.ml", line 244, characters 12-19 |}]
114124
;;
115125

0 commit comments

Comments
 (0)