Skip to content

Commit 7ce615a

Browse files
committed
refactor(Learnocaml_store): Move Json_codec to Learnocaml_api
Motivation: - Learnocaml_store contained Json_codec - learnocaml_client.ml relies on Json_codec - Learnocaml_store depends on Learnocaml_api - Learnocaml_store pulls irmin-git.unix and cryptokit - unlike Learnocaml_api - and these two dependencies are unneeded for compiling learnocaml_client.ml
1 parent 57cb49a commit 7ce615a

File tree

7 files changed

+28
-22
lines changed

7 files changed

+28
-22
lines changed

src/main/dune

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
cohttp-lwt-unix
4444
grading_cli
4545
learnocaml_data
46-
learnocaml_store
4746
learnocaml_api)
4847
)
4948
(install

src/main/learnocaml_client.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ let console_report ?(verbose=false) ex report =
452452
List.iter (fun i -> print_endline (format_item i)) report;
453453
print_newline ()
454454

455-
module Api_client = Learnocaml_api.Client (Learnocaml_store.Json_codec)
455+
module Api_client = Learnocaml_api.Client (Learnocaml_api.Json_codec)
456456

457457
let fetch server_url req =
458458
let url path args =

src/server/learnocaml_server.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ module Request_handler = struct
754754

755755
end
756756

757-
module Api_server = Api.Server (Json_codec) (Request_handler)
757+
module Api_server = Api.Server (Api.Json_codec) (Request_handler)
758758

759759
let init_teacher_token () =
760760
Token.Index.get () >>= function tokens ->

src/state/learnocaml_api.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ module type JSON_CODEC = sig
253253
val encode: ?minify:bool -> 'a J.encoding -> 'a -> string
254254
end
255255

256+
(* Erik: Json_codec was initially in learnocaml_store.ml,
257+
which induced unneeded dependencies:
258+
learn-ocaml-client -> irmin-git.unix, cryptokit *)
259+
module Json_codec = struct
260+
let decode enc s =
261+
(match s with
262+
| "" -> `O []
263+
| s -> Ezjsonm.from_string s)
264+
|> J.destruct enc
265+
266+
let encode ?minify enc x =
267+
match J.construct enc x with
268+
| `A _ | `O _ as json -> Ezjsonm.to_string ?minify json
269+
| `Null -> ""
270+
| _ -> assert false
271+
end
272+
256273
module Conversions (Json: JSON_CODEC) = struct
257274

258275
let response_codec

src/state/learnocaml_api.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ module type JSON_CODEC = sig
196196
val encode: ?minify:bool -> 'a Json_encoding.encoding -> 'a -> string
197197
end
198198

199+
(** Used both for file i/o and request handling *)
200+
module Json_codec: JSON_CODEC
201+
199202
module type REQUEST_HANDLER = sig
200203
type 'resp ret
201204

src/state/learnocaml_store.ml

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,13 @@ let sync_dir = ref (Filename.concat (Sys.getcwd ()) "sync")
1717

1818
let data_dir = ref (Filename.concat !sync_dir "data")
1919

20-
module Json_codec = struct
21-
let decode enc s =
22-
(match s with
23-
| "" -> `O []
24-
| s -> Ezjsonm.from_string s)
25-
|> J.destruct enc
26-
27-
let encode ?minify enc x =
28-
match J.construct enc x with
29-
| `A _ | `O _ as json -> Ezjsonm.to_string ?minify json
30-
| `Null -> ""
31-
| _ -> assert false
32-
end
3320
let get_from_file enc p =
3421
Lwt_io.(with_file ~mode: Input p read) >|=
35-
Json_codec.decode enc
22+
Learnocaml_api.Json_codec.decode enc
3623

3724
let write_to_file enc s p =
3825
let open Lwt_io in
39-
let s = Json_codec.encode enc s in
26+
let s = Learnocaml_api.Json_codec.encode enc s in
4027
with_file ~mode:output p @@ fun oc -> write oc s
4128

4229
let sanitise_path prefix subpath =
@@ -217,7 +204,7 @@ module Exercise = struct
217204
let save () =
218205
Lazy.force tbl >>= fun tbl ->
219206
let l = Hashtbl.fold (fun _ s acc -> s::acc) tbl [] in
220-
let s = Json_codec.encode (J.list enc) l in
207+
let s = Learnocaml_api.Json_codec.encode (J.list enc) l in
221208
write (store_file ()) s
222209

223210
let get id =
@@ -503,7 +490,7 @@ module Save = struct
503490
in
504491
Lwt.catch (fun () ->
505492
write ~no_create:(Token.is_teacher token) ~extra file
506-
(Json_codec.encode ~minify:false enc save))
493+
(Learnocaml_api.Json_codec.encode ~minify:false enc save))
507494
(function
508495
| Not_found -> Lwt.fail_with "Unregistered teacher token"
509496
| e -> Lwt.fail e)
@@ -566,7 +553,7 @@ module Student = struct
566553

567554
let save () =
568555
Lazy.force map >>= fun map ->
569-
let s = Json_codec.encode store_enc !map in
556+
let s = Learnocaml_api.Json_codec.encode store_enc !map in
570557
write (store_file ()) s
571558

572559
let get_student map token =

src/state/learnocaml_store.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ val data_dir: string ref
2020
(** {2 Utility server-side conversion functions} *)
2121

2222
(** Used both for file i/o and request handling *)
23-
module Json_codec: Learnocaml_api.JSON_CODEC
23+
2424
val get_from_file : 'a Json_encoding.encoding -> string -> 'a Lwt.t
2525
val write_to_file : 'a Json_encoding.encoding -> 'a -> string -> unit Lwt.t
2626

0 commit comments

Comments
 (0)