unwrap result list better

This commit is contained in:
Swrup 2022-02-21 07:22:07 +01:00
parent 41a7e2678c
commit 667dba9704

View file

@ -23,6 +23,13 @@ type t =
| Op of thread_data * post | Op of thread_data * post
| Post of post | Post of post
let unwrap_list f ids =
let l = List.map f ids in
let res = List.find_opt Result.is_error l in
if Option.is_some res then
Error (Result.fold ~ok:(assert false) ~error:Fun.id (Option.get res))
else Ok (List.map Result.get_ok l)
module Q = struct module Q = struct
let create_post_user_table = let create_post_user_table =
Caqti_request.exec Caqti_type.unit Caqti_request.exec Caqti_type.unit
@ -319,25 +326,11 @@ let upload_post ?image_content post =
( Db.exec Q.upload_image_info (id, name, alt) ( Db.exec Q.upload_image_info (id, name, alt)
, Db.exec Q.upload_image_content (id, content) ) ) , Db.exec Q.upload_image_content (id, content) ) )
in in
let^ () = let^ _ = unwrap_list (fun tag -> Db.exec Q.upload_post_tag (id, tag)) tags in
match let^ _ =
List.find_opt Result.is_error unwrap_list
(List.map (fun tag -> Db.exec Q.upload_post_tag (id, tag)) tags)
with
| Some (Error e) -> Error e
| Some _ -> assert false
| None -> Ok ()
in
let^ () =
match
List.find_opt Result.is_error
(List.map
(fun cited_id -> Db.exec Q.upload_post_reply (cited_id, id)) (fun cited_id -> Db.exec Q.upload_post_reply (cited_id, id))
citations ) citations
with
| Some (Error e) -> Error e
| Some _ -> assert false
| None -> Ok ()
in in
match thread_data with match thread_data with
| None -> Ok id | None -> Ok id
@ -446,13 +439,6 @@ let get_op id =
let* post = get_post id in let* post = get_post id in
Ok (thread_data, post) Ok (thread_data, post)
let unwrap_list f ids =
let l = List.map f ids in
let res = List.find_opt Result.is_error l in
if Option.is_some res then
Error (Result.fold ~ok:(assert false) ~error:Fun.id (Option.get res))
else Ok (List.map Result.get_ok l)
let get_posts ids = unwrap_list get_post ids let get_posts ids = unwrap_list get_post ids
let get_ops ids = unwrap_list get_op ids let get_ops ids = unwrap_list get_op ids