Client refactor: Move Data_encoding_ezjsonm.read_file into Lwt_utils_unix

This commit is contained in:
Grégoire Henry 2018-02-08 10:51:01 +01:00
parent 24c6f4ea98
commit 84d8ae9222
16 changed files with 60 additions and 69 deletions

View File

@ -440,7 +440,7 @@ let encoding =
let read fp = let read fp =
if Sys.file_exists fp then begin if Sys.file_exists fp then begin
Data_encoding_ezjsonm.read_file fp >>=? fun json -> Lwt_utils_unix.Json.read_file fp >>=? fun json ->
try return (Data_encoding.Json.destruct encoding json) try return (Data_encoding.Json.destruct encoding json)
with exn -> fail (Exn exn) with exn -> fail (Exn exn)
end else end else
@ -448,7 +448,7 @@ let read fp =
let write fp cfg = let write fp cfg =
Node_data_version.ensure_data_dir (Filename.dirname fp) >>=? fun () -> Node_data_version.ensure_data_dir (Filename.dirname fp) >>=? fun () ->
Data_encoding_ezjsonm.write_file fp Lwt_utils_unix.Json.write_file fp
(Data_encoding.Json.construct encoding cfg) (Data_encoding.Json.construct encoding cfg)
let to_string cfg = let to_string cfg =

View File

@ -91,7 +91,7 @@ let check_data_dir_version data_dir =
let version_file = version_file data_dir in let version_file = version_file data_dir in
fail_unless (Sys.file_exists version_file) fail_unless (Sys.file_exists version_file)
(No_data_dir_version_file version_file) >>=? fun () -> (No_data_dir_version_file version_file) >>=? fun () ->
Data_encoding_ezjsonm.read_file version_file Lwt_utils_unix.Json.read_file version_file
|> trace (Could_not_read_data_dir_version version_file) >>=? fun json -> |> trace (Could_not_read_data_dir_version version_file) >>=? fun json ->
begin begin
try return (Data_encoding.Json.destruct version_encoding json) try return (Data_encoding.Json.destruct version_encoding json)
@ -104,7 +104,7 @@ let check_data_dir_version data_dir =
let ensure_data_dir data_dir = let ensure_data_dir data_dir =
let write_version () = let write_version () =
Data_encoding_ezjsonm.write_file Lwt_utils_unix.Json.write_file
(version_file data_dir) (version_file data_dir)
(Data_encoding.Json.construct version_encoding data_version) in (Data_encoding.Json.construct version_encoding data_version) in
try if Sys.file_exists data_dir then try if Sys.file_exists data_dir then

View File

@ -46,7 +46,7 @@ let read ?expected_pow file =
| false -> | false ->
fail (No_identity_file file) fail (No_identity_file file)
| true -> | true ->
Data_encoding_ezjsonm.read_file file >>=? fun json -> Lwt_utils_unix.Json.read_file file >>=? fun json ->
let id = Data_encoding.Json.destruct P2p_identity.encoding json in let id = Data_encoding.Json.destruct P2p_identity.encoding json in
match expected_pow with match expected_pow with
| None -> return id | None -> return id
@ -80,5 +80,5 @@ let write file identity =
fail (Existent_identity_file file) fail (Existent_identity_file file)
else else
Node_data_version.ensure_data_dir (Filename.dirname file) >>=? fun () -> Node_data_version.ensure_data_dir (Filename.dirname file) >>=? fun () ->
Data_encoding_ezjsonm.write_file file Lwt_utils_unix.Json.write_file file
(Data_encoding.Json.construct P2p_identity.encoding identity) (Data_encoding.Json.construct P2p_identity.encoding identity)

View File

@ -109,7 +109,7 @@ let init_node ?sandbox (config : Node_config_file.t) =
match sandbox_param with match sandbox_param with
| None -> Lwt.return (Some (patch_context None)) | None -> Lwt.return (Some (patch_context None))
| Some file -> | Some file ->
Data_encoding_ezjsonm.read_file file >>= function Lwt_utils_unix.Json.read_file file >>= function
| Error err -> | Error err ->
lwt_warn lwt_warn
"Can't parse sandbox parameters: %s" file >>= fun () -> "Can't parse sandbox parameters: %s" file >>= fun () ->

View File

@ -1,34 +0,0 @@
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2018. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
open Error_monad
let to_root = function
| `O ctns -> `O ctns
| `A ctns -> `A ctns
| `Null -> `O []
| oth -> `A [ oth ]
let write_file file json =
let json = to_root json in
protect begin fun () ->
Lwt_io.with_file ~mode:Output file begin fun chan ->
let str = Data_encoding.Json.to_string ~minify:false json in
Lwt_io.write chan str >>= fun _ ->
return ()
end
end
let read_file file =
protect begin fun () ->
Lwt_io.with_file ~mode:Input file begin fun chan ->
Lwt_io.read chan >>= fun str ->
return (Ezjsonm.from_string str :> Data_encoding.json)
end
end

View File

@ -1,16 +0,0 @@
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2018. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
open Error_monad
(** Loads a JSON file in memory *)
val read_file : string -> Data_encoding.json tzresult Lwt.t
(** (Over)write a JSON file from in memory data *)
val write_file : string -> Data_encoding.json -> unit tzresult Lwt.t

View File

@ -29,7 +29,6 @@ module String = struct
end end
module Time = Time module Time = Time
module Data_encoding_ezjsonm = Data_encoding_ezjsonm
module Fitness = Fitness module Fitness = Fitness
module Block_header = Block_header module Block_header = Block_header
module Operation = Operation module Operation = Operation

View File

@ -27,7 +27,6 @@ module String : sig
end end
module Time = Time module Time = Time
module Data_encoding_ezjsonm = Data_encoding_ezjsonm
module Fitness = Fitness module Fitness = Fitness
module Block_header = Block_header module Block_header = Block_header
module Operation = Operation module Operation = Operation

View File

@ -77,7 +77,7 @@ class file_wallet dir : wallet = object (self)
if not (Sys.file_exists filename) then if not (Sys.file_exists filename) then
return default return default
else else
Data_encoding_ezjsonm.read_file filename Lwt_utils_unix.Json.read_file filename
|> generic_trace |> generic_trace
"couldn't to read the %s file" alias_name >>=? fun json -> "couldn't to read the %s file" alias_name >>=? fun json ->
match Data_encoding.Json.destruct encoding json with match Data_encoding.Json.destruct encoding json with
@ -94,7 +94,7 @@ class file_wallet dir : wallet = object (self)
Lwt_utils_unix.create_dir dir >>= fun () -> Lwt_utils_unix.create_dir dir >>= fun () ->
let filename = self#filename alias_name in let filename = self#filename alias_name in
let json = Data_encoding.Json.construct encoding list in let json = Data_encoding.Json.construct encoding list in
Data_encoding_ezjsonm.write_file filename json) Lwt_utils_unix.Json.write_file filename json)
(fun exn -> Lwt.return (error_exn exn)) (fun exn -> Lwt.return (error_exn exn))
|> generic_trace "could not write the %s alias file." alias_name |> generic_trace "could not write the %s alias file." alias_name
end end

View File

@ -92,7 +92,7 @@ module Cfg_file = struct
Data_encoding.Json.destruct encoding json Data_encoding.Json.destruct encoding json
let read fp = let read fp =
Data_encoding_ezjsonm.read_file fp >>=? fun json -> Lwt_utils_unix.Json.read_file fp >>=? fun json ->
return (from_json json) return (from_json json)
let write out cfg = let write out cfg =

View File

@ -299,7 +299,7 @@ let test_json_input testdir =
} }
|} |}
in in
Data_encoding_ezjsonm.read_file file >>= function Lwt_utils_unix.Json.read_file file >>= function
| Error _ -> Assert.fail_msg "Cannot parse \"good.json\"." | Error _ -> Assert.fail_msg "Cannot parse \"good.json\"."
| Ok json -> | Ok json ->
let (id, value, popup) = Json.destruct enc json in let (id, value, popup) = Json.destruct enc json in
@ -325,7 +325,7 @@ let test_json_input testdir =
} }
|} |}
in in
Data_encoding_ezjsonm.read_file file >>= function Lwt_utils_unix.Json.read_file file >>= function
| Error _ -> Assert.fail_msg "Cannot parse \"unknown.json\"." | Error _ -> Assert.fail_msg "Cannot parse \"unknown.json\"."
| Ok json -> | Ok json ->
Assert.test_fail ~msg:__LOC__ Assert.test_fail ~msg:__LOC__

View File

@ -133,14 +133,14 @@ module Info = struct
let load path metadata_encoding = let load path metadata_encoding =
let enc = Data_encoding.list (encoding metadata_encoding) in let enc = Data_encoding.list (encoding metadata_encoding) in
if path <> "/dev/null" && Sys.file_exists path then if path <> "/dev/null" && Sys.file_exists path then
Data_encoding_ezjsonm.read_file path >>=? fun json -> Lwt_utils_unix.Json.read_file path >>=? fun json ->
return (Data_encoding.Json.destruct enc json) return (Data_encoding.Json.destruct enc json)
else else
return [] return []
let save path metadata_encoding peers = let save path metadata_encoding peers =
let open Data_encoding in let open Data_encoding in
Data_encoding_ezjsonm.write_file path @@ Lwt_utils_unix.Json.write_file path @@
Json.construct (list (encoding metadata_encoding)) peers Json.construct (list (encoding metadata_encoding)) peers
end end

View File

@ -7,7 +7,7 @@
(* *) (* *)
(**************************************************************************) (**************************************************************************)
open Lwt.Infix open Error_monad
let read_bytes ?(pos = 0) ?len fd buf = let read_bytes ?(pos = 0) ?len fd buf =
let len = match len with None -> Bytes.length buf - pos | Some l -> l in let len = match len with None -> Bytes.length buf - pos | Some l -> l in
@ -116,3 +116,32 @@ let getaddrinfo ~passive ~node ~service =
(fun { ai_addr ; _ } -> of_sockaddr ai_addr) (fun { ai_addr ; _ } -> of_sockaddr ai_addr)
addr in addr in
Lwt.return points Lwt.return points
module Json = struct
let to_root = function
| `O ctns -> `O ctns
| `A ctns -> `A ctns
| `Null -> `O []
| oth -> `A [ oth ]
let write_file file json =
let json = to_root json in
protect begin fun () ->
Lwt_io.with_file ~mode:Output file begin fun chan ->
let str = Data_encoding.Json.to_string ~minify:false json in
Lwt_io.write chan str >>= fun _ ->
return ()
end
end
let read_file file =
protect begin fun () ->
Lwt_io.with_file ~mode:Input file begin fun chan ->
Lwt_io.read chan >>= fun str ->
return (Ezjsonm.from_string str :> Data_encoding.json)
end
end
end

View File

@ -7,6 +7,8 @@
(* *) (* *)
(**************************************************************************) (**************************************************************************)
open Error_monad
val read_bytes: val read_bytes:
?pos:int -> ?len:int -> Lwt_unix.file_descr -> bytes -> unit Lwt.t ?pos:int -> ?len:int -> Lwt_unix.file_descr -> bytes -> unit Lwt.t
@ -28,3 +30,13 @@ val getaddrinfo:
passive:bool -> passive:bool ->
node:string -> service:string -> node:string -> service:string ->
(Ipaddr.V6.t * int) list Lwt.t (Ipaddr.V6.t * int) list Lwt.t
module Json : sig
(** Loads a JSON file in memory *)
val read_file : string -> Data_encoding.json tzresult Lwt.t
(** (Over)write a JSON file from in memory data *)
val write_file : string -> Data_encoding.json -> unit tzresult Lwt.t
end

View File

@ -11,11 +11,11 @@ open Proto_alpha
open Error_monad open Error_monad
let get_sandbox () = let get_sandbox () =
Data_encoding_ezjsonm.read_file Lwt_utils_unix.Json.read_file
"src/proto_alpha/lib_protocol/test/sandbox.json" >>= function "src/proto_alpha/lib_protocol/test/sandbox.json" >>= function
| Ok x -> Lwt.return x | Ok x -> Lwt.return x
| Error _ -> | Error _ ->
Data_encoding_ezjsonm.read_file "test/sandbox.json" >>= fun x -> Lwt_utils_unix.Json.read_file "test/sandbox.json" >>= fun x ->
Lwt.return @@ Helpers_assert.no_error ~msg:__LOC__ x Lwt.return @@ Helpers_assert.no_error ~msg:__LOC__ x
let main () = let main () =

View File

@ -4,11 +4,13 @@
((name tezos_proto_alpha_isolate_helpers) ((name tezos_proto_alpha_isolate_helpers)
(libraries (tezos-test-helpers (libraries (tezos-test-helpers
tezos-base tezos-base
tezos-stdlib-lwt
tezos-protocol-environment-client tezos-protocol-environment-client
tezos-protocol-alpha)) tezos-protocol-alpha))
(wrapped false) (wrapped false)
(flags (:standard -w -9-32 -safe-string (flags (:standard -w -9-32 -safe-string
-open Tezos_base__TzPervasives -open Tezos_base__TzPervasives
-open Tezos_stdlib_lwt
-open Tezos_test_helpers -open Tezos_test_helpers
-open Tezos_protocol_environment_client)))) -open Tezos_protocol_environment_client))))