From 400cbbf71bc96f86d4d1533cadcda5dceb18ff47 Mon Sep 17 00:00:00 2001 From: Andrey Popp <8mayday@gmail.com> Date: Thu, 25 Apr 2024 11:40:32 +0400 Subject: [PATCH] reorg --- README.md | 6 +-- browser/runtime/dune | 5 ++- .../runtime/ppx_deriving_router_runtime.ml | 39 +----------------- .../runtime/ppx_deriving_router_runtime.mli | 24 +---------- .../runtime/ppx_deriving_router_primitives.ml | 29 +++++++++++++ native/runtime/ppx_deriving_router_runtime.ml | 41 ++----------------- .../runtime/ppx_deriving_router_runtime.mli | 34 ++++----------- ...ness.ml => ppx_deriving_router_witness.ml} | 0 ...ss.mli => ppx_deriving_router_witness.mli} | 0 native/test/routing.ml | 4 +- 10 files changed, 52 insertions(+), 130 deletions(-) create mode 100644 native/runtime/ppx_deriving_router_primitives.ml rename native/runtime/{witness.ml => ppx_deriving_router_witness.ml} (100%) rename native/runtime/{witness.mli => ppx_deriving_router_witness.mli} (100%) diff --git a/README.md b/README.md index 3ac3da3..119118a 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Add preprocessing configuration in `dune`: Define routes: ```ocaml module Pages = struct - open Ppx_deriving_router_runtime.Types + open Ppx_deriving_router_runtime.Primitives type t = | Home [@GET "/"] @@ -111,7 +111,7 @@ val T_of_url_query : string list -> T option val T_to_url_query : T -> string list ``` -The default encoders/decoders are provided in `Ppx_deriving_router_runtime.Types` module +The default encoders/decoders are provided in `Ppx_deriving_router_runtime.Primitives` module (this is why we need to `open` the module when defining routes). To provide custom encoders/decoders for a custom type, we can define own @@ -152,7 +152,7 @@ response type: ```ocaml module Api = struct - open Ppx_deriving_router_runtime.Types + open Ppx_deriving_router_runtime.Primitives open Ppx_deriving_json_runtime.Primitives type user = { id : int } [@@deriving json] diff --git a/browser/runtime/dune b/browser/runtime/dune index 4d42c28..085a880 100644 --- a/browser/runtime/dune +++ b/browser/runtime/dune @@ -6,4 +6,7 @@ (libraries melange-fetch)) (copy_files# - (files ../../native/runtime/witness.ml*)) + (files ../../native/runtime/ppx_deriving_router_witness.ml*)) + +(copy_files# + (files ../../native/runtime/ppx_deriving_router_primitives.ml*)) diff --git a/browser/runtime/ppx_deriving_router_runtime.ml b/browser/runtime/ppx_deriving_router_runtime.ml index e73ee20..e7773ad 100644 --- a/browser/runtime/ppx_deriving_router_runtime.ml +++ b/browser/runtime/ppx_deriving_router_runtime.ml @@ -3,43 +3,8 @@ type 'a url_path_decoder = string -> 'a option type 'a url_query_encoder = 'a -> string list type 'a url_query_decoder = string list -> 'a option -module Witness = Witness - -module Types = struct - let string_to_url_path x = x - let string_of_url_path x = Some x - let int_to_url_path x = string_of_int x - let int_of_url_path x = int_of_string_opt x - let bool_to_url_path x = if x then "true" else "false" - - let bool_of_url_path x = - match x with "true" -> Some true | "false" -> Some false | _ -> None - - let rec last_wins f = function - | [] -> None - | [ x ] -> f x - | _ :: xs -> last_wins f xs - - let string_to_url_query x = [ x ] - let string_of_url_query = last_wins (fun x -> Some x) - let int_to_url_query x = [ string_of_int x ] - let int_of_url_query = last_wins int_of_string_opt - let bool_to_url_query x = if x then [ "true" ] else [] - - let bool_of_url_query = - last_wins (function "true" -> Some true | _ -> Some false) - - let option_to_url_query : - 'a url_query_encoder -> 'a option url_query_encoder = - fun f x -> match x with None -> [] | Some v -> f v - - let option_of_url_query : - 'a url_query_decoder -> 'a option url_query_decoder = - fun f x -> - match x with - | [] -> Some None - | x -> ( match f x with None -> None | Some v -> Some (Some v)) -end +module Witness = Ppx_deriving_router_witness +module Primitives = Ppx_deriving_router_primitives let encode_path out x = Buffer.add_string out (Js.Global.encodeURIComponent x) diff --git a/browser/runtime/ppx_deriving_router_runtime.mli b/browser/runtime/ppx_deriving_router_runtime.mli index 25f2079..6e929f5 100644 --- a/browser/runtime/ppx_deriving_router_runtime.mli +++ b/browser/runtime/ppx_deriving_router_runtime.mli @@ -3,28 +3,8 @@ type 'a url_path_decoder = string -> 'a option type 'a url_query_encoder = 'a -> string list type 'a url_query_decoder = string list -> 'a option -module Witness : module type of Witness - -module Types : sig - val string_to_url_path : string url_path_encoder - val string_of_url_path : string url_path_decoder - val int_to_url_path : int url_path_encoder - val int_of_url_path : int url_path_decoder - val bool_to_url_path : bool url_path_encoder - val bool_of_url_path : bool url_path_decoder - val string_to_url_query : string url_query_encoder - val string_of_url_query : string url_query_decoder - val int_to_url_query : int url_query_encoder - val int_of_url_query : int url_query_decoder - val bool_to_url_query : bool url_query_encoder - val bool_of_url_query : bool url_query_decoder - - val option_to_url_query : - 'a url_query_encoder -> 'a option url_query_encoder - - val option_of_url_query : - 'a url_query_decoder -> 'a option url_query_decoder -end +module Witness : module type of Ppx_deriving_router_witness +module Primitives : module type of Ppx_deriving_router_primitives val encode_path : Buffer.t -> string -> unit val encode_query_key : Buffer.t -> string -> unit diff --git a/native/runtime/ppx_deriving_router_primitives.ml b/native/runtime/ppx_deriving_router_primitives.ml new file mode 100644 index 0000000..ba973de --- /dev/null +++ b/native/runtime/ppx_deriving_router_primitives.ml @@ -0,0 +1,29 @@ +let string_to_url_path x = x +let string_of_url_path x = Some x +let int_to_url_path x = string_of_int x +let int_of_url_path x = int_of_string_opt x +let bool_to_url_path x = if x then "true" else "false" + +let bool_of_url_path x = + match x with "true" -> Some true | "false" -> Some false | _ -> None + +let rec last_wins f = function + | [] -> None + | [ x ] -> f x + | _ :: xs -> last_wins f xs + +let string_to_url_query x = [ x ] +let string_of_url_query x = last_wins (fun x -> Some x) x +let int_to_url_query x = [ string_of_int x ] +let int_of_url_query = last_wins int_of_string_opt +let bool_to_url_query x = if x then [ "true" ] else [] + +let bool_of_url_query = + last_wins (function "true" -> Some true | _ -> Some false) + +let option_to_url_query f x = match x with None -> [] | Some v -> f v + +let option_of_url_query f x = + match x with + | [] -> Some None + | x -> ( match f x with None -> None | Some v -> Some (Some v)) diff --git a/native/runtime/ppx_deriving_router_runtime.ml b/native/runtime/ppx_deriving_router_runtime.ml index 185339d..4dc881c 100644 --- a/native/runtime/ppx_deriving_router_runtime.ml +++ b/native/runtime/ppx_deriving_router_runtime.ml @@ -1,46 +1,11 @@ +module Witness = Ppx_deriving_router_witness +module Primitives = Ppx_deriving_router_primitives + type 'a url_path_encoder = 'a -> string type 'a url_path_decoder = string -> 'a option type 'a url_query_encoder = 'a -> string list type 'a url_query_decoder = string list -> 'a option -module Witness = Witness - -module Types = struct - let string_to_url_path x = x - let string_of_url_path x = Some x - let int_to_url_path x = string_of_int x - let int_of_url_path x = int_of_string_opt x - let bool_to_url_path x = if x then "true" else "false" - - let bool_of_url_path x = - match x with "true" -> Some true | "false" -> Some false | _ -> None - - let rec last_wins f = function - | [] -> None - | [ x ] -> f x - | _ :: xs -> last_wins f xs - - let string_to_url_query x = [ x ] - let string_of_url_query = last_wins (fun x -> Some x) - let int_to_url_query x = [ string_of_int x ] - let int_of_url_query = last_wins int_of_string_opt - let bool_to_url_query x = if x then [ "true" ] else [] - - let bool_of_url_query = - last_wins (function "true" -> Some true | _ -> Some false) - - let option_to_url_query : - 'a url_query_encoder -> 'a option url_query_encoder = - fun f x -> match x with None -> [] | Some v -> f v - - let option_of_url_query : - 'a url_query_decoder -> 'a option url_query_decoder = - fun f x -> - match x with - | [] -> Some None - | x -> ( match f x with None -> None | Some v -> Some (Some v)) -end - let encode_path out x = Buffer.add_string out (Uri.pct_encode ~component:`Path x) diff --git a/native/runtime/ppx_deriving_router_runtime.mli b/native/runtime/ppx_deriving_router_runtime.mli index a0480bb..fb6269c 100644 --- a/native/runtime/ppx_deriving_router_runtime.mli +++ b/native/runtime/ppx_deriving_router_runtime.mli @@ -5,32 +5,8 @@ type 'a url_path_decoder = string -> 'a option type 'a url_query_encoder = 'a -> string list type 'a url_query_decoder = string list -> 'a option -module Witness : module type of Witness - -module Types : sig - val string_to_url_path : string url_path_encoder - val string_of_url_path : string url_path_decoder - val int_to_url_path : int url_path_encoder - val int_of_url_path : int url_path_decoder - val bool_to_url_path : bool url_path_encoder - val bool_of_url_path : bool url_path_decoder - val string_to_url_query : string url_query_encoder - val string_of_url_query : string url_query_decoder - val int_to_url_query : int url_query_encoder - val int_of_url_query : int url_query_decoder - val bool_to_url_query : bool url_query_encoder - val bool_of_url_query : bool url_query_decoder - - val option_to_url_query : - 'a url_query_encoder -> 'a option url_query_encoder - - val option_of_url_query : - 'a url_query_decoder -> 'a option url_query_decoder -end - -val encode_path : Buffer.t -> string -> unit -val encode_query_key : Buffer.t -> string -> unit -val encode_query_value : Buffer.t -> string -> unit +module Primitives : module type of Ppx_deriving_router_primitives +module Witness : module type of Ppx_deriving_router_witness exception Method_not_allowed exception Invalid_query_parameter of string * string list @@ -38,8 +14,12 @@ exception Invalid_body of string (** RESPONSE ENCODING *) -type json = Ppx_deriving_json_runtime.t +val encode_path : Buffer.t -> string -> unit +val encode_query_key : Buffer.t -> string -> unit +val encode_query_value : Buffer.t -> string -> unit + type response = Dream.response +type json = Ppx_deriving_json_runtime.t type _ encode = | Encode_raw : response encode diff --git a/native/runtime/witness.ml b/native/runtime/ppx_deriving_router_witness.ml similarity index 100% rename from native/runtime/witness.ml rename to native/runtime/ppx_deriving_router_witness.ml diff --git a/native/runtime/witness.mli b/native/runtime/ppx_deriving_router_witness.mli similarity index 100% rename from native/runtime/witness.mli rename to native/runtime/ppx_deriving_router_witness.mli diff --git a/native/test/routing.ml b/native/test/routing.ml index 2a279c0..c2e6829 100644 --- a/native/test/routing.ml +++ b/native/test/routing.ml @@ -15,7 +15,7 @@ let modifier_to_url_query = function | Lowercase -> [ "lowercase" ] module Pages = struct - open Ppx_deriving_router_runtime.Types + open Ppx_deriving_router_runtime.Primitives type t = | Home [@GET "/"] @@ -27,7 +27,7 @@ module Pages = struct end module Api = struct - open Ppx_deriving_router_runtime.Types + open Ppx_deriving_router_runtime.Primitives open Ppx_deriving_json_runtime.Primitives type user = { id : int } [@@deriving json]