Skip to content

Commit baaea6b

Browse files
committed
refactor(Learnocaml_store): Move Json_codec (& 2 fcts) 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 baaea6b

File tree

9 files changed

+45
-39
lines changed

9 files changed

+45
-39
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/main/learnocaml_main.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,13 @@ let main o =
451451
Random.self_init ();
452452
Lwt.catch
453453
(fun () ->
454-
Learnocaml_store.get_from_file ServerData.preconfig_enc server_config)
454+
Learnocaml_api.get_from_file ServerData.preconfig_enc server_config)
455455
(function
456456
| Unix.Unix_error (Unix.ENOENT, _, _) -> Lwt.return ServerData.empty_preconfig
457457
| exn -> Lwt.fail exn)
458458
>>= fun preconfig ->
459459
let json_config = ServerData.build_config preconfig in
460-
Learnocaml_store.write_to_file ServerData.config_enc json_config www_server_config
460+
Learnocaml_api.write_to_file ServerData.config_enc json_config www_server_config
461461
>>= fun () ->
462462
if o.builder.Builder.base_url <> "" then
463463
Printf.printf "Base URL: %s\n%!" o.builder.Builder.base_url;

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/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
(libraries json-data-encoding
3434
ezjsonm
3535
conduit
36+
lwt.unix
3637
learnocaml_data)
3738
)
3839

src/state/learnocaml_api.ml

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

256+
(* Erik: Json_codec, get_from_file, write_to_file
257+
were initially in learnocaml_store.ml, 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+
273+
let get_from_file enc p =
274+
let open Lwt.Infix in
275+
Lwt_io.(with_file ~mode: Input p read) >|=
276+
Json_codec.decode enc
277+
278+
let write_to_file enc s p =
279+
let open Lwt.Infix in
280+
let open Lwt_io in
281+
let s = Json_codec.encode enc s in
282+
with_file ~mode:output p @@ fun oc -> write oc s
283+
256284
module Conversions (Json: JSON_CODEC) = struct
257285

258286
let response_codec

src/state/learnocaml_api.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ 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+
val get_from_file : 'a Json_encoding.encoding -> string -> 'a Lwt.t
202+
val write_to_file : 'a Json_encoding.encoding -> 'a -> string -> unit Lwt.t
203+
199204
module type REQUEST_HANDLER = sig
200205
type 'resp ret
201206

src/state/learnocaml_store.ml

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,6 @@ 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
33-
let get_from_file enc p =
34-
Lwt_io.(with_file ~mode: Input p read) >|=
35-
Json_codec.decode enc
36-
37-
let write_to_file enc s p =
38-
let open Lwt_io in
39-
let s = Json_codec.encode enc s in
40-
with_file ~mode:output p @@ fun oc -> write oc s
41-
4220
let sanitise_path prefix subpath =
4321
let rec resolve acc = function
4422
| [] -> List.rev acc
@@ -54,7 +32,7 @@ let sanitise_path prefix subpath =
5432

5533
let read_static_file path enc =
5634
let path = String.split_on_char '/' path in
57-
get_from_file enc (sanitise_path !static_dir path)
35+
Learnocaml_api.get_from_file enc (sanitise_path !static_dir path)
5836

5937
let with_git_register =
6038
let dir_mutex = Lwt_utils.gen_mutex_table () in
@@ -201,7 +179,7 @@ module Exercise = struct
201179
let tbl = lazy (
202180
let tbl = Hashtbl.create 223 in
203181
Lwt.catch (fun () ->
204-
get_from_file (J.list enc) (store_file ()) >|= fun l ->
182+
Learnocaml_api.get_from_file (J.list enc) (store_file ())>|= fun l ->
205183
List.iter (fun st -> Hashtbl.add tbl st.id st) l;
206184
tbl)
207185
@@ function
@@ -217,7 +195,7 @@ module Exercise = struct
217195
let save () =
218196
Lazy.force tbl >>= fun tbl ->
219197
let l = Hashtbl.fold (fun _ s acc -> s::acc) tbl [] in
220-
let s = Json_codec.encode (J.list enc) l in
198+
let s = Learnocaml_api.Json_codec.encode (J.list enc) l in
221199
write (store_file ()) s
222200

223201
let get id =
@@ -472,7 +450,7 @@ module Save = struct
472450

473451
let get token =
474452
Token.find_save token >>= function
475-
| Some save -> get_from_file (J.option enc) save
453+
| Some save -> Learnocaml_api.get_from_file (J.option enc) save
476454
| None -> Lwt.return_none
477455

478456
let set token save =
@@ -503,7 +481,7 @@ module Save = struct
503481
in
504482
Lwt.catch (fun () ->
505483
write ~no_create:(Token.is_teacher token) ~extra file
506-
(Json_codec.encode ~minify:false enc save))
484+
(Learnocaml_api.Json_codec.encode ~minify:false enc save))
507485
(function
508486
| Not_found -> Lwt.fail_with "Unregistered teacher token"
509487
| e -> Lwt.fail e)
@@ -557,7 +535,7 @@ module Student = struct
557535

558536
let load () =
559537
Lwt.catch
560-
(fun () -> get_from_file store_enc (store_file ()))
538+
(fun () -> Learnocaml_api.get_from_file store_enc (store_file ()))
561539
(function
562540
| Unix.Unix_error (Unix.ENOENT, _, _) -> Lwt.return Token.Map.empty
563541
| e -> Lwt.fail e)
@@ -566,7 +544,7 @@ module Student = struct
566544

567545
let save () =
568546
Lazy.force map >>= fun map ->
569-
let s = Json_codec.encode store_enc !map in
547+
let s = Learnocaml_api.Json_codec.encode store_enc !map in
570548
write (store_file ()) s
571549

572550
let get_student map token =

src/state/learnocaml_store.mli

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ val data_dir: string ref
1919

2020
(** {2 Utility server-side conversion functions} *)
2121

22-
(** Used both for file i/o and request handling *)
23-
module Json_codec: Learnocaml_api.JSON_CODEC
24-
val get_from_file : 'a Json_encoding.encoding -> string -> 'a Lwt.t
25-
val write_to_file : 'a Json_encoding.encoding -> 'a -> string -> unit Lwt.t
26-
2722
(* [sanitise_path prefix subdir] simplifies "." and ".." references in [subdir],
2823
and returns the concatenation, but guaranteeing the result remains below
2924
[prefix] (not accounting for symlinks of course, this is purely syntaxical)

0 commit comments

Comments
 (0)