summaryrefslogtreecommitdiff
path: root/src/vycall_pbt.ml
blob: 8cccb0aeedbdc4ef2e41a164bb07364a1f5c5608 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
[@@@ocaml.warning "-27-30-39-44"]

type status = {
  success : bool;
  out : string;
}

type call = {
  script_name : string;
  tag_value : string option;
  arg_value : string option;
  reply : status option;
}

type commit = {
  session_id : string;
  named_active : string option;
  named_proposed : string option;
  dry_run : bool;
  atomic : bool;
  background : bool;
  init : status option;
  calls : call list;
}

let rec default_status 
  ?success:((success:bool) = false)
  ?out:((out:string) = "")
  () : status  = {
  success;
  out;
}

let rec default_call 
  ?script_name:((script_name:string) = "")
  ?tag_value:((tag_value:string option) = None)
  ?arg_value:((arg_value:string option) = None)
  ?reply:((reply:status option) = None)
  () : call  = {
  script_name;
  tag_value;
  arg_value;
  reply;
}

let rec default_commit 
  ?session_id:((session_id:string) = "")
  ?named_active:((named_active:string option) = None)
  ?named_proposed:((named_proposed:string option) = None)
  ?dry_run:((dry_run:bool) = false)
  ?atomic:((atomic:bool) = false)
  ?background:((background:bool) = false)
  ?init:((init:status option) = None)
  ?calls:((calls:call list) = [])
  () : commit  = {
  session_id;
  named_active;
  named_proposed;
  dry_run;
  atomic;
  background;
  init;
  calls;
}

type status_mutable = {
  mutable success : bool;
  mutable out : string;
}

let default_status_mutable () : status_mutable = {
  success = false;
  out = "";
}

type call_mutable = {
  mutable script_name : string;
  mutable tag_value : string option;
  mutable arg_value : string option;
  mutable reply : status option;
}

let default_call_mutable () : call_mutable = {
  script_name = "";
  tag_value = None;
  arg_value = None;
  reply = None;
}

type commit_mutable = {
  mutable session_id : string;
  mutable named_active : string option;
  mutable named_proposed : string option;
  mutable dry_run : bool;
  mutable atomic : bool;
  mutable background : bool;
  mutable init : status option;
  mutable calls : call list;
}

let default_commit_mutable () : commit_mutable = {
  session_id = "";
  named_active = None;
  named_proposed = None;
  dry_run = false;
  atomic = false;
  background = false;
  init = None;
  calls = [];
}

[@@@ocaml.warning "-27-30-39"]

(** {2 Formatters} *)

let rec pp_status fmt (v:status) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~first:true "success" Pbrt.Pp.pp_bool fmt v.success;
    Pbrt.Pp.pp_record_field ~first:false "out" Pbrt.Pp.pp_string fmt v.out;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

let rec pp_call fmt (v:call) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~first:true "script_name" Pbrt.Pp.pp_string fmt v.script_name;
    Pbrt.Pp.pp_record_field ~first:false "tag_value" (Pbrt.Pp.pp_option Pbrt.Pp.pp_string) fmt v.tag_value;
    Pbrt.Pp.pp_record_field ~first:false "arg_value" (Pbrt.Pp.pp_option Pbrt.Pp.pp_string) fmt v.arg_value;
    Pbrt.Pp.pp_record_field ~first:false "reply" (Pbrt.Pp.pp_option pp_status) fmt v.reply;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

let rec pp_commit fmt (v:commit) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~first:true "session_id" Pbrt.Pp.pp_string fmt v.session_id;
    Pbrt.Pp.pp_record_field ~first:false "named_active" (Pbrt.Pp.pp_option Pbrt.Pp.pp_string) fmt v.named_active;
    Pbrt.Pp.pp_record_field ~first:false "named_proposed" (Pbrt.Pp.pp_option Pbrt.Pp.pp_string) fmt v.named_proposed;
    Pbrt.Pp.pp_record_field ~first:false "dry_run" Pbrt.Pp.pp_bool fmt v.dry_run;
    Pbrt.Pp.pp_record_field ~first:false "atomic" Pbrt.Pp.pp_bool fmt v.atomic;
    Pbrt.Pp.pp_record_field ~first:false "background" Pbrt.Pp.pp_bool fmt v.background;
    Pbrt.Pp.pp_record_field ~first:false "init" (Pbrt.Pp.pp_option pp_status) fmt v.init;
    Pbrt.Pp.pp_record_field ~first:false "calls" (Pbrt.Pp.pp_list pp_call) fmt v.calls;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

[@@@ocaml.warning "-27-30-39"]

(** {2 Protobuf Encoding} *)

let rec encode_pb_status (v:status) encoder = 
  Pbrt.Encoder.bool v.success encoder;
  Pbrt.Encoder.key 1 Pbrt.Varint encoder; 
  Pbrt.Encoder.string v.out encoder;
  Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  ()

let rec encode_pb_call (v:call) encoder = 
  Pbrt.Encoder.string v.script_name encoder;
  Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  begin match v.tag_value with
  | Some x -> 
    Pbrt.Encoder.string x encoder;
    Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  | None -> ();
  end;
  begin match v.arg_value with
  | Some x -> 
    Pbrt.Encoder.string x encoder;
    Pbrt.Encoder.key 3 Pbrt.Bytes encoder; 
  | None -> ();
  end;
  begin match v.reply with
  | Some x -> 
    Pbrt.Encoder.nested encode_pb_status x encoder;
    Pbrt.Encoder.key 4 Pbrt.Bytes encoder; 
  | None -> ();
  end;
  ()

let rec encode_pb_commit (v:commit) encoder = 
  Pbrt.Encoder.string v.session_id encoder;
  Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  begin match v.named_active with
  | Some x -> 
    Pbrt.Encoder.string x encoder;
    Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  | None -> ();
  end;
  begin match v.named_proposed with
  | Some x -> 
    Pbrt.Encoder.string x encoder;
    Pbrt.Encoder.key 3 Pbrt.Bytes encoder; 
  | None -> ();
  end;
  Pbrt.Encoder.bool v.dry_run encoder;
  Pbrt.Encoder.key 4 Pbrt.Varint encoder; 
  Pbrt.Encoder.bool v.atomic encoder;
  Pbrt.Encoder.key 5 Pbrt.Varint encoder; 
  Pbrt.Encoder.bool v.background encoder;
  Pbrt.Encoder.key 6 Pbrt.Varint encoder; 
  begin match v.init with
  | Some x -> 
    Pbrt.Encoder.nested encode_pb_status x encoder;
    Pbrt.Encoder.key 7 Pbrt.Bytes encoder; 
  | None -> ();
  end;
  Pbrt.List_util.rev_iter_with (fun x encoder -> 
    Pbrt.Encoder.nested encode_pb_call x encoder;
    Pbrt.Encoder.key 8 Pbrt.Bytes encoder; 
  ) v.calls encoder;
  ()

[@@@ocaml.warning "-27-30-39"]

(** {2 Protobuf Decoding} *)

let rec decode_pb_status d =
  let v = default_status_mutable () in
  let continue__= ref true in
  let out_is_set = ref false in
  let success_is_set = ref false in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
    ); continue__ := false
    | Some (1, Pbrt.Varint) -> begin
      v.success <- Pbrt.Decoder.bool d; success_is_set := true;
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(status), field(1)" pk
    | Some (2, Pbrt.Bytes) -> begin
      v.out <- Pbrt.Decoder.string d; out_is_set := true;
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(status), field(2)" pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  begin if not !out_is_set then Pbrt.Decoder.missing_field "out" end;
  begin if not !success_is_set then Pbrt.Decoder.missing_field "success" end;
  ({
    success = v.success;
    out = v.out;
  } : status)

let rec decode_pb_call d =
  let v = default_call_mutable () in
  let continue__= ref true in
  let script_name_is_set = ref false in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      v.script_name <- Pbrt.Decoder.string d; script_name_is_set := true;
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(call), field(1)" pk
    | Some (2, Pbrt.Bytes) -> begin
      v.tag_value <- Some (Pbrt.Decoder.string d);
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(call), field(2)" pk
    | Some (3, Pbrt.Bytes) -> begin
      v.arg_value <- Some (Pbrt.Decoder.string d);
    end
    | Some (3, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(call), field(3)" pk
    | Some (4, Pbrt.Bytes) -> begin
      v.reply <- Some (decode_pb_status (Pbrt.Decoder.nested d));
    end
    | Some (4, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(call), field(4)" pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  begin if not !script_name_is_set then Pbrt.Decoder.missing_field "script_name" end;
  ({
    script_name = v.script_name;
    tag_value = v.tag_value;
    arg_value = v.arg_value;
    reply = v.reply;
  } : call)

let rec decode_pb_commit d =
  let v = default_commit_mutable () in
  let continue__= ref true in
  let background_is_set = ref false in
  let atomic_is_set = ref false in
  let dry_run_is_set = ref false in
  let session_id_is_set = ref false in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      v.calls <- List.rev v.calls;
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      v.session_id <- Pbrt.Decoder.string d; session_id_is_set := true;
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(commit), field(1)" pk
    | Some (2, Pbrt.Bytes) -> begin
      v.named_active <- Some (Pbrt.Decoder.string d);
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(commit), field(2)" pk
    | Some (3, Pbrt.Bytes) -> begin
      v.named_proposed <- Some (Pbrt.Decoder.string d);
    end
    | Some (3, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(commit), field(3)" pk
    | Some (4, Pbrt.Varint) -> begin
      v.dry_run <- Pbrt.Decoder.bool d; dry_run_is_set := true;
    end
    | Some (4, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(commit), field(4)" pk
    | Some (5, Pbrt.Varint) -> begin
      v.atomic <- Pbrt.Decoder.bool d; atomic_is_set := true;
    end
    | Some (5, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(commit), field(5)" pk
    | Some (6, Pbrt.Varint) -> begin
      v.background <- Pbrt.Decoder.bool d; background_is_set := true;
    end
    | Some (6, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(commit), field(6)" pk
    | Some (7, Pbrt.Bytes) -> begin
      v.init <- Some (decode_pb_status (Pbrt.Decoder.nested d));
    end
    | Some (7, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(commit), field(7)" pk
    | Some (8, Pbrt.Bytes) -> begin
      v.calls <- (decode_pb_call (Pbrt.Decoder.nested d)) :: v.calls;
    end
    | Some (8, pk) -> 
      Pbrt.Decoder.unexpected_payload "Message(commit), field(8)" pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  begin if not !background_is_set then Pbrt.Decoder.missing_field "background" end;
  begin if not !atomic_is_set then Pbrt.Decoder.missing_field "atomic" end;
  begin if not !dry_run_is_set then Pbrt.Decoder.missing_field "dry_run" end;
  begin if not !session_id_is_set then Pbrt.Decoder.missing_field "session_id" end;
  ({
    session_id = v.session_id;
    named_active = v.named_active;
    named_proposed = v.named_proposed;
    dry_run = v.dry_run;
    atomic = v.atomic;
    background = v.background;
    init = v.init;
    calls = v.calls;
  } : commit)