Skip to content

Commit

Permalink
Merge pull request #185 from akabe/feature/refactor
Browse files Browse the repository at this point in the history
Migration from ppx_deriving_yojson to ppx_yojson_conv
  • Loading branch information
akabe authored Mar 21, 2022
2 parents 86f1a85 + 4de288b commit c896eb1
Show file tree
Hide file tree
Showing 38 changed files with 194 additions and 152 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
fail-fast: false
matrix:
ocaml-compiler:
- 4.10.0
- 4.11.0
- 4.12.0
- 4.13.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ opam pin add jupyter https://github.com/akabe/ocaml-jupyter.git
- [Introduction](https://github.com/akabe/ocaml-jupyter/blob/master/notebooks/introduction.ipynb):
a very simple example for use of OCaml Jupyter and sub-packages.
- [Get a description of a word from DuckDuckGo API](https://github.com/akabe/ocaml-jupyter/blob/master/notebooks/word_description_from_DuckDuckGoAPI.ipynb):
request to DuckDuckGo API server by `cohttp.lwt`, and parse a response JSON by `yojson` and `ppx_deriving_yojson`.
request to DuckDuckGo API server by `cohttp.lwt`, and parse a response JSON by `yojson` and `ppx_yojson_conv`.

In addition, many examples (e.g, image processing, voice analysis, etc.) are available at
[docker-ocaml-jupyter-datascience/notebooks](https://github.com/akabe/docker-ocaml-jupyter-datascience/tree/master/notebooks).
Expand Down
4 changes: 1 addition & 3 deletions config/dune
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
(targets ocaml_flags.sexp)
(deps configure.exe)
(action (run %{deps} -o %{targets}
-ppx ppx_deriving_yojson
-ppx lwt_ppx
-ocamlfind %{bin:ocamlfind}
-ocaml-flag "-w A-4-31-33-34-39-41-42-43-44-45-48-49-50-58"
-ocaml-flag "-w A-4-31-33-34-39-41-42-43-44-45-48-49-50-58-66"
-ocaml-flag "-safe-string -strict-sequence -strict-formats"
-ocaml-flag "-short-paths")))

Expand Down
1 change: 0 additions & 1 deletion jupyter-archimedes/src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
(modes byte)
(modules Jupyter_archimedes)
(flags ((:include %{workspace_root}/config/ocaml_flags.sexp)))
(preprocess (pps ppx_deriving_yojson))
(libraries jupyter.notebook
archimedes.cairo))
5 changes: 3 additions & 2 deletions jupyter.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ build: [
[ "dune" "build" "-p" name "-j" jobs ]
]
depends: [
"ocaml" {>= "4.04.0" & < "4.15"}
"ocaml" {>= "4.10.0" & < "4.15"}
"base-threads"
"base-unix"
"uuidm" {>= "0.9.6"}
Expand All @@ -29,11 +29,12 @@ depends: [
"zmq" {>= "5.0.0"}
"zmq-lwt" {>= "5.0.0"}
"yojson" {>="1.6.0"}
"ppx_deriving_yojson" {>= "3.0"}
"ppx_yojson_conv" {>= "0.14.0"}
"cryptokit" {>= "1.12"}
"dune" {build & >= "1.0.0"}
"ounit2" {with-test & >= "2.0.0"}
"ocp-indent" {with-test & >= "1.7.0"}
"ppx_deriving" {with-test}
]
depopts: [
"merlin"
Expand Down
2 changes: 1 addition & 1 deletion jupyter/src/comm/dune
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
(flags (:standard
-thread
(:include %{workspace_root}/config/ocaml_flags.sexp)))
(preprocess (pps ppx_deriving_yojson))
(preprocess (pps ppx_yojson_conv))
(libraries jupyter
jupyter.notebook))
4 changes: 2 additions & 2 deletions jupyter/src/completor/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
Merlin
Intf)
(flags ((:include %{workspace_root}/config/ocaml_flags.sexp)))
(preprocess (pps lwt_ppx ppx_deriving_yojson))
(preprocess (pps lwt_ppx ppx_yojson_conv))
(libraries jupyter
jupyter_log
lwt
lwt.unix
yojson
ppx_deriving_yojson.runtime))
ppx_yojson_conv_lib))
4 changes: 2 additions & 2 deletions jupyter/src/completor/intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ sig
cmpl_type : string;
cmpl_doc : string;
}
[@@deriving yojson { strict = false }]
[@@deriving yojson]

type reply =
{
cmpl_candidates : candidate list;
cmpl_start : int;
cmpl_end : int;
}
[@@deriving yojson { strict = false }]
[@@deriving yojson]

type t

Expand Down
25 changes: 14 additions & 11 deletions jupyter/src/completor/merlin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ let call merlin command flags printer =

type 'a merlin_reply =
| RETURN of 'a [@name "return"]
| FAILURE of Yojson.Safe.t [@name "failure"]
| ERROR of Yojson.Safe.t [@name "error"]
| EXN of Yojson.Safe.t [@name "exception"]
| FAILURE of Jupyter.Json.t [@name "failure"]
| ERROR of Jupyter.Json.t [@name "error"]
| EXN of Jupyter.Json.t [@name "exception"]
[@@deriving of_yojson]

type 'a merlin_reply_body =
Expand All @@ -84,18 +84,17 @@ type 'a merlin_reply_body =
value : 'a;
notifications : string list;
}
[@@deriving of_yojson { strict = false }]
[@@deriving of_yojson]
[@@yojson.allow_extra_fields]

let parse_merlin_reply ~of_yojson str =
let error_json msg json =
error (fun pp -> pp "%s: %s" msg (Yojson.Safe.pretty_to_string json))
in
let reply = Yojson.Safe.from_string str
|> [%of_yojson: Yojson.Safe.t merlin_reply_body]
|> Jupyter.Json.or_die in
|> [%of_yojson: Jupyter.Json.t merlin_reply_body] in
`List [`String reply.klass; reply.value]
|> merlin_reply_of_yojson of_yojson
|> Jupyter.Json.or_die
|> function
| RETURN ret -> Some ret
| FAILURE j -> error_json "Merlin failure" j ; None
Expand All @@ -109,14 +108,16 @@ type ident_position =
id_line : int [@key "line"];
id_col : int [@key "col"];
}
[@@deriving yojson { strict = false }]
[@@deriving yojson]
[@@yojson.allow_extra_fields]

type ident_reply =
{
id_start : ident_position [@key "start"];
id_end : ident_position [@key "end"];
}
[@@deriving yojson { strict = false }]
[@@deriving yojson]
[@@yojson.allow_extra_fields]

let occurrences ~pos merlin code =
let args = ["-identifier-at"; string_of_int pos] in
Expand Down Expand Up @@ -164,15 +165,17 @@ type candidate =
cmpl_type : string [@key "desc"];
cmpl_doc : string [@key "info"];
}
[@@deriving yojson { strict = false }]
[@@deriving yojson]
[@@yojson.allow_extra_fields]

type reply =
{
cmpl_candidates : candidate list [@key "entries"];
cmpl_start : int [@key "start"] [@default 0];
cmpl_end : int [@key "end"] [@default 0];
}
[@@deriving yojson { strict = false }]
[@@deriving yojson]
[@@yojson.allow_extra_fields]

let empty = { cmpl_candidates = []; cmpl_start = 0; cmpl_end = 0; }

Expand Down
6 changes: 4 additions & 2 deletions jupyter/src/core/comm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ type t =
{
comm_target : string option [@key "target_name"] [@default None];
comm_id : string [@key "comm_id"];
comm_data : Yojson.Safe.t [@key "data"];
} [@@deriving yojson { strict = false }]
comm_data : Json.t [@key "data"];
}
[@@deriving yojson]
[@@yojson.allow_extra_fields]
4 changes: 2 additions & 2 deletions jupyter/src/core/dune
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
Json
Version)
(flags ((:include %{workspace_root}/config/ocaml_flags.sexp)))
(preprocess (pps ppx_deriving_yojson))
(libraries unix uuidm ppx_deriving_yojson.runtime))
(preprocess (pps ppx_yojson_conv))
(libraries unix uuidm ppx_yojson_conv_lib))
34 changes: 21 additions & 13 deletions jupyter/src/core/iopub.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,49 @@
type stream_name =
| IOPUB_STDOUT [@name "stdout"]
| IOPUB_STDERR [@name "stderr"]
[@@deriving yojson { strict = false }]
[@@deriving yojson]

type stream =
{
stream_name : stream_name Json.enum [@key "name"];
stream_text : string [@key "text"];
} [@@deriving yojson { strict = false }]
} [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 Display data} *)

type transient =
{
display_id : string [@key "display_id"];
} [@@deriving yojson { strict = false }]
} [@@deriving yojson]
[@@yojson.allow_extra_fields]

type display_data =
{
display_data : Yojson.Safe.t [@key "data"];
display_metadata : Yojson.Safe.t [@key "metadata"];
display_data : Json.t [@key "data"];
display_metadata : Json.t [@key "metadata"];
display_transient : transient option [@key "transient"] [@default None];
} [@@deriving yojson { strict = false }]
} [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 Code inputs} *)

type exec_input =
{
exin_code : string [@key "code"];
exin_count : int [@key "execution_count"];
} [@@deriving yojson { strict = false }]
} [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 Execution results} *)

type exec_result =
{
exres_count : int [@key "execution_count"];
exres_data : Yojson.Safe.t [@key "data"];
exres_metadata : Yojson.Safe.t [@key "metadata"];
} [@@deriving yojson { strict = false }]
exres_data : Json.t [@key "data"];
exres_metadata : Json.t [@key "metadata"];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 Kernel status} *)

Expand All @@ -77,7 +82,8 @@ type exec_status =
type status =
{
kernel_state : exec_status Json.enum [@key "execution_state"];
} [@@deriving yojson { strict = false }]
} [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 Execution errors} *)

Expand All @@ -86,14 +92,16 @@ type error =
ename : string [@key "ename"];
evalue : string [@key "evalue"];
traceback : string list [@key "traceback"];
} [@@deriving yojson { strict = false }]
} [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 Clear output} *)

type clear_output =
{
clear_wait : bool [@key "wait"];
} [@@deriving yojson { strict = false }]
} [@@deriving yojson]
[@@yojson.allow_extra_fields]

(** {2 IOPUB Request} *)

Expand Down
24 changes: 14 additions & 10 deletions jupyter/src/core/json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@

(** JSON utility *)

let or_none = function
| Result.Error _ -> None
| Result.Ok x -> Some x

let or_die = function
| Result.Error msg -> Yojson.json_error msg
| Result.Ok x -> x

type 'a enum = 'a

let enum_of_yojson of_yojson json = of_yojson (`List [json])

let enum_to_yojson to_yojson x = match to_yojson x with
let yojson_of_enum yojson_of x = match yojson_of x with
| `List [json] -> json
| _ -> assert false
| _ -> assert false (* TODO: replace with a suitable exception *)

type t = Yojson.Safe.t

let yojson_of_t x = x

let t_of_yojson x = x

type 'a option_try = 'a option

let option_try_of_yojson of_yojson x =
try Some (of_yojson x)
with _ -> None
10 changes: 7 additions & 3 deletions jupyter/src/core/json.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@

(** JSON utility *)

val or_none : ('a, string) Result.result -> 'a option
type 'a enum = 'a [@@deriving yojson]

val or_die : ('a, string) Result.result -> 'a
type t = Yojson.Safe.t

type 'a enum = 'a [@@deriving yojson]
val yojson_of_t : t -> Yojson.Safe.t

val t_of_yojson : Yojson.Safe.t -> t

type 'a option_try = 'a option [@@deriving of_yojson]
22 changes: 11 additions & 11 deletions jupyter/src/core/message.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,19 @@ type header =
the message is created *)
username : string; (** the current username *)
version : string; (** the message protocol version *)
} [@@deriving yojson { strict = false }]
} [@@deriving yojson]
[@@yojson.allow_extra_fields]

let header_of_string str =
Yojson.Safe.from_string str
|> [%of_yojson: header]
|> Json.or_die

let header_option_of_string s =
Yojson.Safe.from_string s
|> [%of_yojson: header]
|> Json.or_none
let header_option_of_string str =
Yojson.Safe.from_string str
|> [%of_yojson: header Json.option_try]

let string_of_header hdr =
[%to_yojson: header] hdr
[%yojson_of: header] hdr
|> Yojson.Safe.to_string

let string_of_header_option = function
Expand Down Expand Up @@ -81,7 +80,8 @@ type 'content t =
buffers : string list;
(** optional: buffers is a list of binary data buffers for implementations
that support binary extensions to the protocol. *)
} [@@deriving yojson { strict = false }]
} [@@deriving yojson]
[@@yojson.allow_extra_fields]

let epoch_to_iso8601_string epoch =
let open Unix in
Expand All @@ -108,13 +108,13 @@ let create_next ?(time = Unix.gettimeofday ()) ~content_to_yojson parent content
}

let create_next_shell ?time parent content =
create_next ?time ~content_to_yojson:[%to_yojson: Shell.reply] parent content
create_next ?time ~content_to_yojson:[%yojson_of: Shell.reply] parent content

let create_next_iopub ?time parent content =
create_next ?time ~content_to_yojson:[%to_yojson: Iopub.reply] parent content
create_next ?time ~content_to_yojson:[%yojson_of: Iopub.reply] parent content

let create_next_stdin ?time parent content =
create_next ?time ~content_to_yojson:[%to_yojson: Stdin.reply] parent content
create_next ?time ~content_to_yojson:[%yojson_of: Stdin.reply] parent content

(** {2 Messages} *)

Expand Down
Loading

0 comments on commit c896eb1

Please sign in to comment.