package ez_api

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file ezEncoding.ml

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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
(**************************************************************************)
(*                                                                        *)
(*                 Copyright 2018-2023 OCamlPro                           *)
(*                                                                        *)
(*  All rights reserved. This file is distributed under the terms of the  *)
(*  GNU Lesser General Public License version 2.1, with the special       *)
(*  exception on linking described in the file LICENSE.                   *)
(*                                                                        *)
(**************************************************************************)

open Json_encoding
module Ezjsonm  = Ezjsonm_interface

exception DestructError

(* Default behavior to destruct a json value *)
let destruct encoding buf =
  try
    let json = Ezjsonm.from_string buf in
    destruct encoding json
  with
  | Cannot_destruct (path, exn)  ->
    Format.eprintf "Error during destruction path %a : %s\n\n %s\n%!"
      (Json_query.print_path_as_json_path ~wildcards:true) path
      (Printexc.to_string exn)
      buf;
    raise DestructError
  | Unexpected_field field ->
    Format.eprintf "Error during destruction path, unexpected field %S %s\n%!"
      field buf ;
    raise DestructError

type destruct_error = [ `destruct_exn of exn ]

let destruct_res encoding s =
  try Ok (Json_encoding.destruct encoding (Ezjsonm.from_string s))
  with exn -> Error (`destruct_exn exn)

let construct ?compact encoding data =
  Ezjsonm.to_string ?minify:compact (Json_encoding.construct encoding data)

let unexpected_error ~kind ~expected =
  raise @@ Cannot_destruct ([], Unexpected (kind, expected))

exception Not_utf8

let is_valid_utf8 str =
  try
    Uutf.String.fold_utf_8 (fun _ _ -> function
        | `Malformed _ -> raise Not_utf8
        | _ -> ()
      ) () str;
    true
  with Not_utf8 -> false

let encode_string str =
  if is_valid_utf8 str then `String str
  else
    let `Hex h = Hex.of_string str in
    `O [ "hex", `String h ]

let decode_string = function
  | `String str -> str
  | `O [ "hex", `String str ] -> Hex.to_string (`Hex str)
  | _ -> unexpected_error ~kind:"raw string" ~expected:"encoded string"

let encoded_string =
  conv encode_string decode_string any_ezjson_value

let () =
  Printexc.register_printer @@ function
  | Cannot_destruct (path, exn) ->
    let s = Printf.sprintf "Cannot destruct JSON (%s, %s)"
        (Json_query.json_pointer_of_path path)
        (Printexc.to_string exn) in
    Some s
  | _ -> None

let obj11 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),x11)
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),x11)
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11)
    )
    (merge_objs
       (obj10
          f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (obj1 f11))

let obj12 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12)
    )
    (merge_objs
       (obj10
          f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (obj2 f11 f12)
    )

let obj13 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13)
    )
    (merge_objs
       (obj10
          f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (obj3 f11 f12 f13)
    )

let obj14 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13,x14))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13,x14))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14)
    )
    (merge_objs
       (obj10
          f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (obj4 f11 f12 f13 f14)
    )

let obj15 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13,x14,x15))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13,x14,x15))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15)
    )
    (merge_objs
       (obj10
          f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (obj5 f11 f12 f13 f14 f15)
    )

let obj16 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13,x14,x15,x16))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13,x14,x15,x16))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16)
    )
    (merge_objs
       (obj10
          f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (obj6 f11 f12 f13 f14 f15 f16)
    )

let obj17 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13,x14,x15,x16,x17))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13,x14,x15,x16,x17))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17)
    )
    (merge_objs
       (obj10
          f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (obj7 f11 f12 f13 f14 f15 f16 f17)
    )

let obj18 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18 =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13,x14,x15,x16,x17,x18))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13,x14,x15,x16,x17,x18))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18)
    )
    (merge_objs
       (obj10
          f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (obj8 f11 f12 f13 f14 f15 f16 f17 f18)
    )

let obj19 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18 f19 =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13,x14,x15,x16,x17,x18,x19))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),(x11,x12,x13,x14,x15,x16,x17,x18,x19))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19)
    )
    (merge_objs
       (obj10 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (obj9 f11 f12 f13 f14 f15 f16 f17 f18 f19)
    )

let obj20 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18 f19 f20 =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),
         (x11,x12,x13,x14,x15,x16,x17,x18,x19,x20))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),
       (x11,x12,x13,x14,x15,x16,x17,x18,x19,x20))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20)
    )
    (merge_objs
       (obj10 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (obj10 f11 f12 f13 f14 f15 f16 f17 f18 f19 f20)
    )

let obj21
    f1 f2 f3 f4 f5 f6 f7 f8 f9 f10
    f11 f12 f13 f14 f15 f16 f17 f18 f19 f20
    f21
  =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,
       x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,
       x21)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),
         (
           (x11,x12,x13,x14,x15,x16,x17,x18,x19,x20),
           (x21)
         ))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),
       ((x11,x12,x13,x14,x15,x16,x17,x18,x19,x20),
        (x21)
       ))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,
       x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,
       x21)
    )
    (merge_objs
       (obj10 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (merge_objs
          (obj10 f11 f12 f13 f14 f15 f16 f17 f18 f19 f20)
          (obj1 f21)
       )
    )

let obj22
    f1 f2 f3 f4 f5 f6 f7 f8 f9 f10
    f11 f12 f13 f14 f15 f16 f17 f18 f19 f20
    f21 f22
  =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,
       x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,
       x21,x22)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),
         (
           (x11,x12,x13,x14,x15,x16,x17,x18,x19,x20),
           (x21,x22)
         ))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),
       ((x11,x12,x13,x14,x15,x16,x17,x18,x19,x20),
        (x21,x22)
       ))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,
       x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,
       x21,x22)
    )
    (merge_objs
       (obj10 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (merge_objs
          (obj10 f11 f12 f13 f14 f15 f16 f17 f18 f19 f20)
          (obj2 f21 f22)
       )
    )

let obj23
    f1 f2 f3 f4 f5 f6 f7 f8 f9 f10
    f11 f12 f13 f14 f15 f16 f17 f18 f19 f20
    f21 f22 f23
  =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,
       x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,
       x21,x22,x23)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),
         (
           (x11,x12,x13,x14,x15,x16,x17,x18,x19,x20),
           (x21,x22,x23)
         ))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),
       ((x11,x12,x13,x14,x15,x16,x17,x18,x19,x20),
        (x21,x22,x23)
       ))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,
       x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,
       x21,x22,x23)
    )
    (merge_objs
       (obj10 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (merge_objs
          (obj10 f11 f12 f13 f14 f15 f16 f17 f18 f19 f20)
          (obj3 f21 f22 f23)
       )
    )

let obj24
    f1 f2 f3 f4 f5 f6 f7 f8 f9 f10
    f11 f12 f13 f14 f15 f16 f17 f18 f19 f20
    f21 f22 f23 f24
  =
  conv
    (fun
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,
       x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,
       x21,x22,x23,x24)
      ->
        ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),
         (
           (x11,x12,x13,x14,x15,x16,x17,x18,x19,x20),
           (x21,x22,x23,x24)
         ))
    )
    (fun
      ((x1,x2,x3,x4,x5,x6,x7,x8,x9,x10),
       ((x11,x12,x13,x14,x15,x16,x17,x18,x19,x20),
        (x21,x22,x23,x24)
       ))
      ->
      (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,
       x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,
       x21,x22,x23,x24)
    )
    (merge_objs
       (obj10 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10)
       (merge_objs
          (obj10 f11 f12 f13 f14 f15 f16 f17 f18 f19 f20)
          (obj4 f21 f22 f23 f24)
       )
    )

(* for swagger *)

let defs = ref []

let int_kind = Json_schema.(Integer numeric_specs)
let int_element = Json_schema.element int_kind
let tup1_int_kind = Json_schema.(Monomorphic_array (int_element, array_specs))
let tup1_int_element = Json_schema.element tup1_int_kind
let int64_element = Json_schema.({int_element with format = Some "int64"})
let tup1_int64_kind = Json_schema.(Monomorphic_array (int64_element, array_specs))
let tup1_int64_element = Json_schema.element tup1_int64_kind
let string_kind = Json_schema.(String string_specs)
let string_element = Json_schema.element string_kind
let tup1_string_kind = Json_schema.(Monomorphic_array (string_element, array_specs))
let tup1_string_element = Json_schema.element tup1_string_kind

let int64 =
  conv
    (fun x -> Int64.to_string x)
    (fun x -> Int64.of_string x)
    ~schema:(Json_schema.create int64_element)
    string

let int =
  conv (fun x -> x) (fun x -> x) ~schema:(Json_schema.create int_element) int
let tup1_int =
  conv (fun x -> x) (fun x -> x) ~schema:(Json_schema.create tup1_int_element) (tup1 int)
let tup1_int64 =
  conv (fun x -> x) (fun x -> x) ~schema:(Json_schema.create tup1_int64_element) (tup1 int64)
let tup1_string =
  conv (fun x -> x) (fun x -> x) ~schema:(Json_schema.create tup1_string_element) (tup1 string)


let register ?name ?(descr="") o =
  match name with
  | None -> ()
  | Some name ->
    let sch = lazy ( Json_encoding.schema o ) in
    defs := (name, (descr, sch)) :: !defs;
    ()

let merge_objs ?name ?descr o1 o2 =
  let o = Json_encoding.merge_objs o1 o2 in
  register ?name ?descr o;
  o

let result ok_enc err_enc =
  union [
    case
      ok_enc
      (function Ok s -> Some s | Error _ -> None)
      (fun s -> Ok s);
    case
      (obj1 (req "error" err_enc))
      (function Error s -> Some s | Ok _ -> None)
      (fun s -> Error s)
  ]

let ignore_enc encoding =
  conv (fun x -> x, ()) (fun (x, _) -> x) @@
  merge_objs encoding unit

let enc_constant enc v =
  conv (fun () -> v) (fun v2 -> if v2 = v then () else assert false) enc
OCaml

Innovation. Community. Security.