From 6a8aff777aa0687bc004fd8e899e10943d51de29 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Sun, 27 Dec 2020 16:45:24 -0800 Subject: [PATCH] Add full splat to splat Signed-off-by: Rudi Grinberg --- opium/src/router.ml | 5 +++-- opium/test/opium_router_tests.ml | 17 +++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/opium/src/router.ml b/opium/src/router.ml index 9f5bcdb..fd16fa7 100644 --- a/opium/src/router.ml +++ b/opium/src/router.ml @@ -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 @@ -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 | [ "" ] | [] -> @@ -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 ] diff --git a/opium/test/opium_router_tests.ml b/opium/test/opium_router_tests.ml index c7e56b3..ef1a7b6 100644 --- a/opium/test/opium_router_tests.ml +++ b/opium/test/opium_router_tests.ml @@ -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" = @@ -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))) |}] ;;