open StdLabels let name = __FILE__ let description = "Insert data and check the result" let speclist () = [] type t = ((string * Int64.t * string) list, Configuration.Read_conf.error) result let pp : Format.formatter -> t -> unit = Format.pp_print_result ~error:Configuration.Read_conf.pp_error ~ok: (Format.pp_print_list ~pp_sep:Format.pp_print_cut (fun formatter (content_id, id, content) -> Format.fprintf formatter "@[%s:%Ld@;%s@]" content_id id content)) type content = Model.Reference.t Model.Content.t type value = Model.Reference.t Model.Typ.value type properties = (string * value) list (** [insert_and_get_result db content entry] will insert the value in the database and return the informations for the content just inserted. [content] is the type definition for the element we want to insert [entry] contains all the values to insert in the database. *) let insert_and_get_result : ?parent:int64 -> Caqti_lwt.connection -> content -> properties -> (string * int64 * string) Lwt.t = fun ?parent db content entry -> let ( let* ) = Lwt.bind in let* id = Query.insert ?parent db content (fun v -> match List.assoc_opt v entry with Some v -> v | None -> Model.Typ.None) in let content_id = Model.Content.id content in let module Db = (val db) in let* json_result = Db.find (Common.content_as_json content) id in let* json = Caqti_lwt.or_fail json_result in Lwt.return (content_id, id, json) (** Read the values to insert in the configuration *) module Make (S : Decoders.Decode.S) = struct let ( let* ) = S.( let* ) let ( let+ ) = S.( let+ ) type content_to_insert = content * properties list let ( let@@ ) = Lwt.bind (** Load the values associated with the configuration. *) class loader (db : Caqti_lwt.connection) (conf : Configuration.Ast.setting) = object (self) method insert_and_return_references : content -> properties list Lwt.t -> int64 list Lwt.t = fun content properties -> let ( let* ) = Lwt.bind in let* properties = properties in Lwt_list.map_s (fun line -> let* _, id, _ = insert_and_get_result db content line in Lwt.return id) properties method value : content -> string -> (string * value) Lwt.t S.decoder = fun content key -> ignore content; S.one_of [ ( "numeric", S.map (fun v -> Lwt.return (key, Model.Typ.(V (Integer, Some (Int64.of_int v))))) S.int ); ( "text", S.map (fun v -> Lwt.return (key, Model.Typ.(V (Text, Some v)))) S.string ); ( "reference", match Model.Content.find_target content key with | None -> S.fail "Not a feature" | Some target_ref -> let target = Model.Reference.get target_ref in let* properties = self#properties target in let sequence = let@@ properties = properties in let@@ _, id, _ = insert_and_get_result db target properties in let reference = Model.Typ.{ t = target_ref; constraints = None } in Lwt.return ( key, Model.Typ.( V (Model.Typ.MonoReference reference, Some id)) ) in S.succeed sequence ); ( "references", match Model.Content.find_target content key with | None -> S.fail "Not a feature" | Some target_ref -> let target = Model.Reference.get target_ref in let* properties = self#values target in let sequence = let@@ ids = self#insert_and_return_references target properties in let reference = Model.Typ.{ t = target_ref; constraints = None } in Lwt.return ( key, Model.Typ.( Seq ( Model.Typ.MultiReference reference, List.to_seq ids )) ) in S.succeed sequence ); ( "other", (* If the value is a dictionnary list, consider it as external references *) S.succeed (Lwt.return (key, Model.Typ.None)) ); ] (** The values are encoded in a very simple way, depending of the nature of the field in the json, we create a property in for text or numeric *) method properties : content -> properties Lwt.t S.decoder = fun content -> S.map Lwt.all (S.key_value_pairs_seq (self#value content)) method values : content -> properties list Lwt.t S.decoder = fun content -> S.map Lwt.all (S.list (self#properties content)) method load_content : content_to_insert list Lwt.t S.decoder = let* properties = S.key_value_pairs_seq (fun key -> S.one_of [ ( "content", fun v -> match (Configuration.Ast.get_content conf) key with | None -> Error (Decoders.Error.make "no content") | Some subcontent -> let values = self#values subcontent v in Result.map (fun v -> let content_to_insert = let@@ v = v in Lwt.return (subcontent, v) in Some content_to_insert) values ); ("other", fun _ -> Ok None); ]) in let properties = List.filter_map ~f:(fun v -> v) properties in S.succeed (Lwt.all properties) (** Read the content and for each key, check if there is an existing subcontent with the same name, If this subcontent exists, parse all the subcontents with all the values and return them. *) method root_content : string -> (content * (properties * content_to_insert list) list) Lwt.t S.decoder = fun name -> (* In this syntax, the content is supposed to follow the definition and will never be None *) let content = Configuration.Ast.get_content conf name |> Option.get in let* properties = S.field_opt_or ~default:(Lwt.return []) "values" (self#values content) in (* Look for subcontents in the given values. The subcontent is identfied by having a key name equal to the subcontent identifier. *) let* subcontents : content_to_insert list list Lwt.t = S.map Lwt.all (S.field_opt_or ~default:[] "values" (S.list self#load_content)) in let result = let@@ properties = properties in let@@ subcontents = subcontents in let to_insert = List.combine properties subcontents in Lwt.return (content, to_insert) in S.succeed result method content : (content * (properties * content_to_insert list) list) Lwt.t S.decoder = let* name = S.field "id" S.string in let* properties = self#root_content name in S.succeed properties method configuration : (content * (properties * content_to_insert list) list) list Lwt.t S.decoder = let* content = S.field "content" (S.list self#content) in let conf = Lwt.all content in S.succeed conf end end module D = Make (Configuration.Json5.Decode) let exec : string list -> t = fun _ -> let stdin = In_channel.input_all stdin in let ( let** ) = Result.bind in let** configuration = Configuration.Read_conf.read_string_conf stdin in Lwt_main.run (let ( let* ) = Lwt.bind in let* db = Common.connect configuration in let module Db = (val db) in let inserts = Configuration.Json5.Decode.decode_string (new D.loader db configuration)#configuration stdin in match inserts with | Error err -> Lwt.return_error err | Ok inserts -> let* inserts = inserts in let* insertions = Lwt_list.fold_left_s (fun acc (content, entries) -> Lwt_list.fold_left_s (fun acc (entry, subcontents) -> (* Insert the main content *) let* content_id, id, json = insert_and_get_result db content entry in let acc = (content_id, id, json) :: acc in (* Also query the multivaluated references *) let* ext = Common.collect_references db content id in let acc = List.append ext acc in (* Insert the subcontent associated *) Lwt_list.fold_left_s (fun acc (subcontent, entries) -> let* acc = Lwt_list.fold_left_s (fun acc entry -> let* subcontent_id, id, json = insert_and_get_result ~parent:id db subcontent entry in let acc = (subcontent_id, id, json) :: acc in (* Also query the multivaluated references *) let* ext = Common.collect_references db subcontent id in let acc = List.append ext acc in Lwt.return acc) acc entries in Lwt.return acc) acc subcontents) acc entries) [] inserts in Lwt.return_ok (List.rev insertions))