summaryrefslogtreecommitdiff
path: root/test/update.ml
blob: 74208deea61cbeade04eda8348e662d3b8a788a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
let name = __FILE__
let description = "Update a content"

type actions =
  | Commit
  | Unset of string
  | Text of string * string
  | Integer of string * int
  | Reference of Model.reference * string * int
  | References of Model.reference * string * int64 Seq.t

let actions : actions list ref = ref []

type t = string list

let pp : Format.formatter -> t -> unit = Format.(pp_print_list pp_print_string)

let configuration =
  lazy
    (let model =
       {|
    {
     "content": [
       {
         "id": "entity",
         "section": [
           {
             "field": [
               {
                 "name": "references",
                 "label": {},
                 "type": {
                   "content": "entity",
                   "multivaluated": true
                 }
               },
               {
                 "name": "reference",
                 "label": {},
                 "type": {
                   "content": "entity",
                   "multivaluated": false
                 }
               },
               {
                 "name": "integer",
                 "label": {},
                 "type": "integer"
               },
               {
                 "name": "text",
                 "label": {},
                 "type": "text"
               },
             ]
           }
         ]
       }
     ]
    }|}
     in
     Configuration.Read_conf.read_string_conf model
     |> Result.map_error (fun e ->
         Croquette_common.Format.format_string (fun fmt ->
             Configuration.Read_conf.pp_error fmt e))
     |> Result.get_ok')

let speclist () =
  let (lazy configuration) = configuration in
  let content = Configuration.Ast.get_content configuration (Model.Identifier.of_string "entity") in
  let fields = Model.Content.fields (Option.get content) in
  Seq.fold_left
    (fun acc feature ->
      let key = "--" ^ Model.Identifier.to_string (Model.Field.name feature) in
      let (Model.Field.T type_) = Model.Field.type_ feature in
      let argument =
        match type_ with
        | Model.Typ.Text ->
            Some
              ( key,
                Arg.String
                  (fun value ->
                    actions :=
                      Text (Model.Identifier.to_string (Model.Field.name feature), value) :: !actions),
                "<v> Set field with the value <v>" )
        | Model.Typ.Integer ->
            Some
              ( key,
                Arg.Int
                  (fun value ->
                    actions :=
                      Integer (Model.Identifier.to_string (Model.Field.name feature), value) :: !actions),
                "<n> Set field with the value <n>" )
        | Model.Typ.MonoReference target ->
            Some
              ( key,
                Arg.Int
                  (fun value ->
                    actions :=
                      Reference (target, Model.Identifier.to_string (Model.Field.name feature), value)
                      :: !actions),
                "<id> Set field with the id" )
        | Model.Typ.MultiReference target ->
            Some
              ( key,
                Arg.String
                  (fun value ->
                    let ids =
                      String.split_on_char ',' value
                      |> List.map (fun v -> Int64.of_string (String.trim v))
                      |> List.to_seq
                    in

                    actions :=
                      References (target, Model.Identifier.to_string (Model.Field.name feature), ids)
                      :: !actions),
                "<id,..> Set this list of values" )
      in

      match argument with None -> acc | Some v -> v :: acc)
    [
      ( "--commit",
        Arg.Unit (fun () -> actions := Commit :: !actions),
        "\tCommit this group of update" );
      ( "--clear",
        Arg.String (fun feature -> actions := Unset feature :: !actions),
        "<f> Clear the field <f>" );
    ]
    fields
  |> Arg.align

let exec : string list -> t =
 fun _ ->
  let (lazy configuration) = configuration in
  let content =
    Configuration.Ast.get_content configuration (Model.Identifier.of_string "entity") |> Option.get
  in

  Lwt_main.run
    (let ( let* ) = Lwt.bind in
     let* db = Common.connect configuration in
     let module Db = (val db) in
     (* Insert a content without any value *)
     let* id = Query.insert db content (fun _ -> Model.Typ.None) in

     let group_commit : actions Seq.t Seq.t =
       List.rev !actions |> List.to_seq
       |> Seq.group (fun a b ->
           match (a, b) with _, Commit -> false | _ -> true)
     in
     (* Update the values from the argument in the command line *)
     let* printers =
       Seq.fold_left
         (fun acc actions ->
           let* acc = acc in
           let* () =
             Query.update db id content (fun key ->
                 let value =
                   Seq.find_map
                     (function
                       | Unset k when String.equal k key ->
                           let field =
                             Model.Content.fields content
                             |> Seq.find (fun f ->
                                 String.equal key (Model.Identifier.to_string (Model.Field.name f)))
                             |> Option.get
                           in
                           let (Model.Field.T t) = Model.Field.type_ field in
                           begin match t with
                           | Model.Typ.MultiReference _ as e ->
                               Some Model.Typ.(Seq (e, Seq.empty))
                           | Integer as e -> Some Model.Typ.(V (e, None))
                           | Text as e -> Some Model.Typ.(V (e, None))
                           | MonoReference _ as e ->
                               Some Model.Typ.(V (e, None))
                           end
                       | Text (k, v) when String.equal k key ->
                           Some Model.Typ.(V (Text, Some v))
                       | Integer (k, v) when String.equal k key ->
                           Some Model.Typ.(V (Integer, Some (Int64.of_int v)))
                       | Reference (target, k, v) when String.equal k key ->
                           Some
                             Model.Typ.(
                               V (MonoReference target, Some (Int64.of_int v)))
                       | References (target, k, v) when String.equal k key ->
                           Some Model.Typ.(Seq (MultiReference target, v))
                       | _ -> None)
                     actions
                 in
                 match value with Some v -> v | None -> Model.Typ.None)
           in
           (* Get the values from the content registered in the database *)
           let* json_result = Db.find (Common.content_as_json content) id in
           let* json = Caqti_lwt.or_fail json_result in

           (* Also query for the referenoces *)
           let* refs = Common.collect_references db content id in
           let acc =
             List.fold_left
               (fun acc (_, _, ref) -> ref :: acc)
               (json :: acc) (List.rev refs)
           in

           Lwt.return acc)
         (Lwt.return []) group_commit
     in
     Lwt.return (List.rev printers))