Skip to content

Commit

Permalink
Merge pull request #51 from issuu/dte/ocamlformat
Browse files Browse the repository at this point in the history
Migrate to OCamlformat 0.9
  • Loading branch information
darioteixeira authored Apr 24, 2019
2 parents ecdd2a5 + 1c7865a commit 74d8a38
Show file tree
Hide file tree
Showing 11 changed files with 808 additions and 1,044 deletions.
7 changes: 5 additions & 2 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
break-cases=all
break-cases=toplevel
break-infix=fit-or-vertical
break-string-literals=wrap
doc-comments=before
extension-sugar=preserve
field-space=loose
if-then-else=keyword-first
indicate-nested-or-patterns=false
indicate-nested-or-patterns=unsafe-no
infix-precedence=parens
leading-nested-match-parens=false
let-and=sparse
let-open=preserve
margin=90
ocp-indent-compat=true
parens-tuple=multi-line-only
parens-tuple-patterns=multi-line-only
sequence-style=terminator
type-decl=sparse
wrap-fun-args=false
module-item-spacing=sparse
break-separators=after-and-docked
break-infix-before-func=false
53 changes: 19 additions & 34 deletions examples/hello_world_with_async/hello_world_with_async.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@ let stdout = Lazy.force Writer.stdout
module Phone : Ppx_mysql_runtime.SERIALIZABLE with type t = string = struct
type t = string

let of_mysql str =
if String.length str <= 9
then Ok str
else Error "string too long"
let of_mysql str = if String.length str <= 9 then Ok str else Error "string too long"

let to_mysql str = str
end

(** The user type used throughout this example. *)

type user =
{ id : int32
; name : string
; phone : Phone.t option }
type user = {
id : int32;
name : string;
phone : Phone.t option
}

let user_of_tuple (id, name, phone) = {id; name; phone}

Expand All @@ -45,10 +43,8 @@ let print_user {id; name; phone} =
id
name
( match phone with
| Some p ->
p
| None ->
"--" )
| Some p -> p
| None -> "--" )

(** Database queries using the Ppx_mysql syntax extension. *)

Expand Down Expand Up @@ -100,32 +96,22 @@ let delete_user = [%mysql execute "DELETE FROM users WHERE id = %int32{id}"]

let test dbh =
let open Deferred.Result in
insert_user dbh ~id:1l ~name:"John" ~phone:(Some "123456")
>>= fun () ->
insert_user dbh ~id:2l ~name:"Jane" ~phone:None
>>= fun () ->
insert_user dbh ~id:3l ~name:"Claire" ~phone:None
>>= fun () ->
insert_users dbh [4l, "Mark", None; 5l, "Alice", Some "234567"]
>>= fun () ->
get_all_users dbh
>>= fun users ->
insert_user dbh ~id:1l ~name:"John" ~phone:(Some "123456") >>= fun () ->
insert_user dbh ~id:2l ~name:"Jane" ~phone:None >>= fun () ->
insert_user dbh ~id:3l ~name:"Claire" ~phone:None >>= fun () ->
insert_users dbh [4l, "Mark", None; 5l, "Alice", Some "234567"] >>= fun () ->
get_all_users dbh >>= fun users ->
Writer.writef stdout "All users:\n";
List.iter ~f:print_user users;
get_some_users dbh [1l; 2l; 3l]
>>= fun users ->
get_some_users dbh [1l; 2l; 3l] >>= fun users ->
Writer.writef stdout "Users with ID in {1, 2, 3}:\n";
List.iter ~f:print_user users;
update_user dbh ~id:2l ~name:"Mary" ~phone:(Some "654321")
>>= fun () ->
get_user dbh ~id:2l
>>= fun user ->
update_user dbh ~id:2l ~name:"Mary" ~phone:(Some "654321") >>= fun () ->
get_user dbh ~id:2l >>= fun user ->
Writer.writef stdout "User with ID = 2 after update:\n";
print_user user;
delete_user dbh ~id:3l
>>= fun () ->
get_all_users dbh
>>= fun users ->
delete_user dbh ~id:3l >>= fun () ->
get_all_users dbh >>= fun users ->
Writer.writef stdout "All users after deleting one with ID = 3:\n";
List.iter ~f:print_user users;
return ()
Expand All @@ -134,8 +120,7 @@ let main () =
let open Deferred.Infix in
let dbh = Mysql.quick_connect ~database:"test" ~user:"root" () in
let caching_dbh = Prepared.init dbh in
test caching_dbh
>>= fun res ->
test caching_dbh >>= fun res ->
Mysql.disconnect dbh;
match res with
| Ok () ->
Expand Down
56 changes: 20 additions & 36 deletions examples/hello_world_with_identity/hello_world_with_identity.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ open Mysql_with_identity
module Phone : Ppx_mysql_runtime.SERIALIZABLE with type t = string = struct
type t = string

let of_mysql str =
if String.length str <= 9
then Ok str
else Error "string too long"
let of_mysql str = if String.length str <= 9 then Ok str else Error "string too long"

let to_mysql str = str
end

(** The user type used throughout this example. *)

type user =
{ id : int32
; name : string
; phone : Phone.t option }
type user = {
id : int32;
name : string;
phone : Phone.t option
}

let user_of_tuple (id, name, phone) = {id; name; phone}

Expand All @@ -40,10 +38,8 @@ let print_user {id; name; phone} =
id
name
( match phone with
| Some p ->
p
| None ->
"--" )
| Some p -> p
| None -> "--" )

(** Database queries using the Ppx_mysql syntax extension. *)

Expand Down Expand Up @@ -96,32 +92,22 @@ let delete_user = [%mysql execute "DELETE FROM users WHERE id = %int32{id}"]

let test dbh =
let open IO_result in
insert_user dbh ~id:1l ~name:"John" ~phone:(Some "123456")
>>= fun () ->
insert_user dbh ~id:2l ~name:"Jane" ~phone:None
>>= fun () ->
insert_user dbh ~id:3l ~name:"Claire" ~phone:None
>>= fun () ->
insert_users dbh [4l, "Mark", None; 5l, "Alice", Some "234567"]
>>= fun () ->
get_all_users dbh
>>= fun users ->
insert_user dbh ~id:1l ~name:"John" ~phone:(Some "123456") >>= fun () ->
insert_user dbh ~id:2l ~name:"Jane" ~phone:None >>= fun () ->
insert_user dbh ~id:3l ~name:"Claire" ~phone:None >>= fun () ->
insert_users dbh [4l, "Mark", None; 5l, "Alice", Some "234567"] >>= fun () ->
get_all_users dbh >>= fun users ->
Printf.printf "All users:\n";
List.iter print_user users;
get_some_users dbh [1l; 2l; 3l]
>>= fun users ->
get_some_users dbh [1l; 2l; 3l] >>= fun users ->
Printf.printf "Users with ID in {1, 2, 3}:\n";
List.iter print_user users;
update_user dbh ~id:2l ~name:"Mary" ~phone:(Some "654321")
>>= fun () ->
get_user dbh ~id:2l
>>= fun user ->
update_user dbh ~id:2l ~name:"Mary" ~phone:(Some "654321") >>= fun () ->
get_user dbh ~id:2l >>= fun user ->
Printf.printf "User with ID = 2 after update:\n";
print_user user;
delete_user dbh ~id:3l
>>= fun () ->
get_all_users dbh
>>= fun users ->
delete_user dbh ~id:3l >>= fun () ->
get_all_users dbh >>= fun users ->
Printf.printf "All users after deleting one with ID = 3:\n";
List.iter print_user users;
Ok ()
Expand All @@ -132,9 +118,7 @@ let main () =
let res = test caching_dbh in
Mysql.disconnect dbh;
match res with
| Ok () ->
Printf.printf "All went well!\n"
| Error _ ->
Printf.printf "An error occurred!\n"
| Ok () -> Printf.printf "All went well!\n"
| Error _ -> Printf.printf "An error occurred!\n"

let () = main ()
77 changes: 27 additions & 50 deletions examples/hello_world_with_lwt/hello_world_with_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ open Mysql_with_lwt
module Phone : Ppx_mysql_runtime.SERIALIZABLE with type t = string = struct
type t = string

let of_mysql str =
if String.length str <= 9
then Ok str
else Error "string too long"
let of_mysql str = if String.length str <= 9 then Ok str else Error "string too long"

let to_mysql str = str
end

(** The user type used throughout this example. *)

type user =
{ id : int32
; name : string
; phone : Phone.t option }
type user = {
id : int32;
name : string;
phone : Phone.t option
}

let user_of_tuple (id, name, phone) = {id; name; phone}

Expand All @@ -40,10 +38,8 @@ let print_user {id; name; phone} =
id
name
( match phone with
| Some p ->
p
| None ->
"--" )
| Some p -> p
| None -> "--" )

(** Database queries using the Ppx_mysql syntax extension. *)

Expand Down Expand Up @@ -95,38 +91,22 @@ let delete_user = [%mysql execute "DELETE FROM users WHERE id = %int32{id}"]

let test dbh =
let open Lwt_result.Infix in
insert_user dbh ~id:1l ~name:"John" ~phone:(Some "123456")
>>= fun () ->
insert_user dbh ~id:2l ~name:"Jane" ~phone:None
>>= fun () ->
insert_user dbh ~id:3l ~name:"Claire" ~phone:None
>>= fun () ->
insert_users dbh [4l, "Mark", None; 5l, "Alice", Some "234567"]
>>= fun () ->
get_all_users dbh
>>= fun users ->
Lwt_result.ok @@ Lwt_io.printf "All users:\n"
>>= fun () ->
Lwt_result.ok @@ Lwt_list.iter_s print_user users
>>= fun () ->
get_some_users dbh [1l; 2l; 3l]
>>= fun users ->
Lwt_result.ok @@ Lwt_io.printf "Users with ID in {1, 2, 3}:\n"
>>= fun () ->
Lwt_result.ok @@ Lwt_list.iter_s print_user users
>>= fun () ->
update_user dbh ~id:2l ~name:"Mary" ~phone:(Some "654321")
>>= fun () ->
get_user dbh ~id:2l
>>= fun user ->
Lwt_result.ok @@ Lwt_io.printf "User with ID = 2 after update:\n"
>>= fun () ->
Lwt_result.ok @@ print_user user
>>= fun () ->
delete_user dbh ~id:3l
>>= fun () ->
get_all_users dbh
>>= fun users ->
insert_user dbh ~id:1l ~name:"John" ~phone:(Some "123456") >>= fun () ->
insert_user dbh ~id:2l ~name:"Jane" ~phone:None >>= fun () ->
insert_user dbh ~id:3l ~name:"Claire" ~phone:None >>= fun () ->
insert_users dbh [4l, "Mark", None; 5l, "Alice", Some "234567"] >>= fun () ->
get_all_users dbh >>= fun users ->
Lwt_result.ok @@ Lwt_io.printf "All users:\n" >>= fun () ->
Lwt_result.ok @@ Lwt_list.iter_s print_user users >>= fun () ->
get_some_users dbh [1l; 2l; 3l] >>= fun users ->
Lwt_result.ok @@ Lwt_io.printf "Users with ID in {1, 2, 3}:\n" >>= fun () ->
Lwt_result.ok @@ Lwt_list.iter_s print_user users >>= fun () ->
update_user dbh ~id:2l ~name:"Mary" ~phone:(Some "654321") >>= fun () ->
get_user dbh ~id:2l >>= fun user ->
Lwt_result.ok @@ Lwt_io.printf "User with ID = 2 after update:\n" >>= fun () ->
Lwt_result.ok @@ print_user user >>= fun () ->
delete_user dbh ~id:3l >>= fun () ->
get_all_users dbh >>= fun users ->
Lwt_result.ok @@ Lwt_io.printf "All users after deleting one with ID = 3:\n"
>>= fun () ->
Lwt_result.ok @@ Lwt_list.iter_s print_user users >>= fun () -> Lwt_result.return ()
Expand All @@ -135,13 +115,10 @@ let main () =
let open Lwt.Infix in
let dbh = Mysql.quick_connect ~database:"test" ~user:"root" () in
let caching_dbh = Prepared.init dbh in
test caching_dbh
>>= fun res ->
test caching_dbh >>= fun res ->
Mysql.disconnect dbh;
match res with
| Ok () ->
Lwt_io.printf "All went well!\n"
| Error _ ->
Lwt_io.printf "An error occurred!\n"
| Ok () -> Lwt_io.printf "All went well!\n"
| Error _ -> Lwt_io.printf "An error occurred!\n"

let () = Lwt_main.run @@ main ()
Loading

0 comments on commit 74d8a38

Please # to comment.