Skip to content

Commit

Permalink
fix [@Prefix]
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypopp committed Apr 24, 2024
1 parent a9549b0 commit 2b4671b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
5 changes: 1 addition & 4 deletions lib/ppx_deriving_router.ml
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ let derive_mount td m =
Lwt.return [%e make_with_encode encode])
in
Ppx_deriving_router_runtime.prefix_route
[%e
match m.m_prefix with
| Some p -> [%expr Some [%e estring ~loc p]]
| None -> [%expr None]]
[%e elist ~loc (List.map m.m_prefix ~f:(estring ~loc))]
f route)
[%e routes]]
in
Expand Down
20 changes: 10 additions & 10 deletions lib/ppx_deriving_router_common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ and path_segment = Ppath of string | Pparam of string * core_type
type route = Leaf of leaf | Mount of mount

and mount = {
m_prefix : string option;
m_prefix : string list;
m_typ : longident loc;
m_typ_param : label option;
m_ctor : constructor_declaration;
Expand Down Expand Up @@ -221,15 +221,13 @@ let extract td =
| `mount (m_typ, m_typ_param) ->
let m_prefix =
match Attribute.get attr_prefix ctor with
| None -> Some ctor.pcd_name.txt
| Some path -> (
| None -> [ ctor.pcd_name.txt ]
| Some path ->
let path = path.txt in
let path =
match String.chop_prefix ~pre:"/" path with
| Some path -> path
| None -> path
in
match path with "" -> None | path -> Some path)
let path = String.split_on_char ~by:'/' path in
List.filter_map path ~f:(function
| "" -> None
| x -> Some x)
in
let m_response = extract_mount_response ctor.pcd_res in
Mount
Expand Down Expand Up @@ -451,7 +449,9 @@ module Derive_href = struct
->
let prefix =
estring ~loc
(match m_prefix with Some p -> "/" ^ p | None -> "")
(match m_prefix with
| [] -> ""
| p -> "/" ^ String.concat ~sep:"/" p)
in
let loc = m_ctor.pcd_loc in
let p, x = match_ctor m_ctor in
Expand Down
9 changes: 7 additions & 2 deletions runtime/ppx_deriving_router_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ type 'v route =

let prefix_route prefix f (Route (path, a, g)) =
match prefix with
| None -> Route (path, a, fun x -> f (g x))
| Some prefix -> Route (Routes.(s prefix /~ path), a, fun x -> f (g x))
| [] -> Route (path, a, fun x -> f (g x))
| prefix ->
let rec prefix_path p = function
| [] -> p
| x :: xs -> prefix_path Routes.(s x /~ p) xs
in
Route (prefix_path path (List.rev prefix), a, fun x -> f (g x))

let to_route (Route (path, a, f)) = Routes.(map f (route path a))

Expand Down
2 changes: 1 addition & 1 deletion runtime/ppx_deriving_router_runtime.mli
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ val encode : 'a encode -> 'a -> response Lwt.t
type 'v route =
| Route : ('a, 'v) Routes.path * 'a * ('v -> 'w) -> 'w route

val prefix_route : string option -> ('a -> 'b) -> 'a route -> 'b route
val prefix_route : string list -> ('a -> 'b) -> 'a route -> 'b route
val to_route : 'a route -> 'a Routes.route

(** ROUTER *)
Expand Down
2 changes: 1 addition & 1 deletion test/routing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ module All = struct
type _ t =
| Pages : Pages.t -> Ppx_deriving_router_runtime.response t
[@prefix "/"]
| Api : 'a Api.t -> 'a t [@prefix "/api"]
| Api : 'a Api.t -> 'a t [@prefix "/nested/api"]
[@@deriving router]
end
2 changes: 1 addition & 1 deletion test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ let test () =
print_endline "# TESTING ROUTE MATCHING GENERATION (ALL)";
test_req all_handler `GET "/hello/world";
test_req all_handler `GET "/";
test_req all_handler `GET "/api/121"
test_req all_handler `GET "/nested/api/121"

let () =
match Sys.argv.(1) with
Expand Down
4 changes: 2 additions & 2 deletions test/test.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/hello/world?modifier=uppercase
/121
/hello/world
/api/121
/nested/api/121
# TESTING ROUTE MATCHING GENERATION
## GET /
OK: HOME PAGE
Expand Down Expand Up @@ -44,5 +44,5 @@
OK: Hello, world!
## GET /
OK: HOME PAGE
## GET /api/121
## GET /nested/api/121
OK: {"id":121}

0 comments on commit 2b4671b

Please sign in to comment.