Skip to content

Commit

Permalink
reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypopp committed Apr 25, 2024
1 parent debe367 commit 400cbbf
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 130 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 "/"]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
5 changes: 4 additions & 1 deletion browser/runtime/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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*))
39 changes: 2 additions & 37 deletions browser/runtime/ppx_deriving_router_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 2 additions & 22 deletions browser/runtime/ppx_deriving_router_runtime.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions native/runtime/ppx_deriving_router_primitives.ml
Original file line number Diff line number Diff line change
@@ -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))
41 changes: 3 additions & 38 deletions native/runtime/ppx_deriving_router_runtime.ml
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
34 changes: 7 additions & 27 deletions native/runtime/ppx_deriving_router_runtime.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,21 @@ 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
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
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions native/test/routing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "/"]
Expand All @@ -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]
Expand Down

0 comments on commit 400cbbf

Please sign in to comment.