camlspotter avatar camlspotter committed 710bd90

refactor

Comments (0)

Files changed (5)

 MyOCamlPackage(bitbucket, $(FILES), $(EMPTY), $(EMPTY))
 
 api.p4.ml: api.ml
-    camlp4o dynlink.cma -I $(HOME)/.opam/system/lib/type_conv pa_type_conv.cma -I $(HOME)/.opam/system/lib/meta_conv pa_json_tc.cmo -printer Camlp4OCamlPrinter -o api.p4.ml api.ml
+    camlp4o dynlink.cma -I $(HOME)/.opam/system/lib/type_conv pa_type_conv.cma -I $(HOME)/.opam/system/lib/meta_conv pa_meta_conv.cma -printer Camlp4OCamlPrinter -o api.p4.ml api.ml
 open Printf
 open Common
+
 open Tiny_json
 open Meta_conv.Open
-open Json_conv
-open Ocaml_conv
+open Meta_conv.Types.Result.Open (* I use >>= only for Result here *)
+open Json_conv  (* for conv(json) *)
+open Ocaml_conv (* for conv(ocaml) *)
 
-open Meta_conv.Types.Result.Open
 
 (* field of type Json.t mc_leftovers is automatically handled for Json, but not for ocaml *)
 type 'target mc_leftovers = (string * 'target) list with conv(ocaml)
+
 (* CR jfuruse: mc_fields should be supported in Ocaml_conv *)
 type 'target mc_fields = (string * 'target) list with conv(ocaml)
 
 module Json = struct
   include Json
-  let ocaml_of_t j = Ocaml.String (Json.show j)
-  let t_of_ocaml = function
+
+  let ocaml_of_t : Json.t Ocaml_conv.encoder = fun j -> Ocaml.String (Json.show j)
+  let t_of_ocaml : Json.t Ocaml_conv.decoder = fun o ->
+    try match o with
     | Ocaml.String s -> `Ok (Json.parse s)
-    | _ -> assert false
+    | _ -> failwith "Ocaml.String expected"
+    with e -> `Error (Meta_conv.Types.Error.Deconstruction_error e, o)
+
+  let parse s = try `Ok (parse s) with e -> `Error (`Json_parse e)
 end
 
 module Scheme = struct
-  type t = Hg(:"hg":) | Git(:"git":) with conv(json), conv(ocaml)
+  type t = 
+    | Hg(:"hg":) 
+    | Git(:"git":) 
+  with conv(json), conv(ocaml)
 end
 
 module LocalTime = struct
+  (* CRv2 jfuruse: this should be lazily parsed date *)
   type t = string with conv(json), conv(ocaml)
 end
 
 module UTCTime = struct
+  (* CRv2 jfuruse: this should be lazily parsed date *)
   type t = string with conv(json), conv(ocaml)
 end
 
 module Error = struct
   open Format
 
-  let wrap_json_conv = function
+  let wrap_json_conv convf s = match convf s with
     | `Ok v -> `Ok v
     | `Error e -> `Error (`Json_conv e)
 
   let format ppf = function
     | `Http (n, _err) -> fprintf ppf "HTTP Error %d@." n
+    | `Json_parse exn -> fprintf ppf "Error at Json parse: %s@." (Printexc.to_string exn)
+    | `Other exn      -> fprintf ppf "Error: %s@." (Printexc.to_string exn)
     | `Json_conv ((_,v) as e) -> 
+        (* CR jfuruse: This is bit strange. Meta_conv.Types.Error.format should print [v] too? *)
         Format.eprintf "%a@.Error input: %s@." 
           (Meta_conv.Types.Error.format (fun _ _ -> ())) e
           (Json.show v)
-    | `Json_parse exn -> fprintf ppf "Error at Json parse: %s@." (Printexc.to_string exn)
-    | `Other exn -> fprintf ppf "Error: %s@." (Printexc.to_string exn)
 end
 
-let curl_get_and_parse convf curlf = 
-  Curl.get_string curlf |> Curl.ok200 >>= fun s ->
-  (try `Ok (Json.parse s) with e -> `Error (`Json_parse e)) >>= fun j ->
-  convf j |> Error.wrap_json_conv
+let curl_get_and_parse (convf : 'a Json_conv.decoder) curlf = 
+  Curl.get_string curlf >>= Json.parse >>= Error.wrap_json_conv convf
 
 module Data = struct
 
 end
 
 external (|>) : 'a -> ('a -> 'b) -> 'b = "%revapply"
-
-module Result = struct
-  type ('a, 'b) t = [ `Ok of 'a | `Error of 'b ]
-  let return a = `Ok a
-  let fail b = `Error b
-  let bind t f = match t with
-    | `Error b -> `Error b
-    | `Ok a -> f a
-  let (>>=) = bind
-  module Open = struct
-    let (>>=) = bind
-  end
-end
+let ok200 = function
+  | 200, v -> `Ok v
+  | n, mes -> `Error (`Http (n, mes))
+
 let get_string f = 
   let h = new Curl.handle in
   f h;
   let buf = Buffer.create 100 in
   h#set_writefunction (fun s -> Buffer.add_string buf s; String.length s);
   h#perform;
-  h#get_httpcode, Buffer.contents buf
+  ok200 (h#get_httpcode, Buffer.contents buf)
 
-let ok200 = function
-  | 200, v -> `Ok v
-  | n, mes -> `Error (`Http (n, mes))
-val get_string : (Curl.handle -> unit) -> int * string
-  (** [get_string init] initializes curl handle by [init] then add a write function for the simple string retrieval and perform it.
-      It returns the obtained string. *)
+val ok200 : (int * string) -> [`Ok of string | `Error of [> `Http of (int * string) ]]
 
-val ok200 : (int * string) -> [`Ok of string | `Error of [> `Http of (int * string) ]]
+val get_string : (Curl.handle -> unit) -> [`Ok of string | `Error of [> `Http of (int * string) ]]
+  (** [get_string init] initializes curl handle by [init] 
+      then add a write function for the simple string retrieval and perform it.
+      It returns the obtained string.
+  *)
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.