package ppx_xml_conv

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

Source file ppx_xml_conv.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
open Base
open Ppxlib
open Ast_builder.Default

let extension_name = "xml"

let unsupported_type_error_msg ~name =
  Printf.sprintf "The type %s is not natively supported in the xml camlp4 extension" name
;;

let useless_merge_recursive _log ~field_name:_ ~tp:_ ast = ast

let xsd_function_name = function
  | None -> "xsd"
  | Some param -> Printf.sprintf "xsd_of_%s" param
;;

let edot ~loc path_opt id =
  pexp_ident
    ~loc
    (Located.mk
       ~loc
       (match path_opt with
        | None -> Longident.Lident id
        | Some p -> Longident.Ldot (p, id)))
;;

module Xsd_of_type = Ppx_conv_func.Of_complete (struct
    let unsupported_type_error_msg = unsupported_type_error_msg
    let conversion_name = extension_name
    let function_name = xsd_function_name

    let prepend loc body =
      let acc loc = [%pat? acc] in
      let anonymous loc = [%pat? _] in
      let body = [%expr [%e body] :: acc] in
      Ppx_conv_func.lambda loc [ acc loc; anonymous loc ] body
    ;;

    let unit loc ~field_name:_ =
      let expr = [%expr Csvfields.Xml.xsd_of_unit] in
      prepend loc expr
    ;;

    let bool loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_bool]
    let string loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_string]
    let char loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_char]
    let int loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_int]
    let float loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_float]
    let int32 loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_int32]
    let int64 loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_int64]
    let nativeint loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_nativeint]
    let big_int loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_big_int]
    let nat loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_nat]
    let num loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_num]
    let ratio loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_ratio]
    let merge_recursive = useless_merge_recursive

    let recursive loc ~field_name:_ ~type_name:_ ~path =
      let tns = "xsd" in
      edot ~loc path tns
    ;;

    let list loc ~field_name =
      [%expr Csvfields.Xml.xsd_of_list [%e estring ~loc field_name]]
    ;;

    let array loc ~field_name =
      [%expr Csvfields.Xml.xsd_of_array [%e estring ~loc field_name]]
    ;;

    let option loc ~field_name =
      [%expr Csvfields.Xml.xsd_of_option [%e estring ~loc field_name]]
    ;;

    let lazy_t loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_lazy_t]
    let ref loc ~field_name:_ = [%expr Csvfields.Xml.xsd_of_ref]
  end)

let type_of_xml_function_name = function
  | None -> "of_xml"
  | Some param -> Printf.sprintf "%s_of_xml" param
;;

module Type_of_xml = Ppx_conv_func.Of_complete (struct
    let unsupported_type_error_msg = unsupported_type_error_msg
    let conversion_name = extension_name
    let function_name = type_of_xml_function_name
    let unit loc ~field_name:_ = [%expr Csvfields.Xml.unit_of_xml]
    let bool loc ~field_name:_ = [%expr Csvfields.Xml.bool_of_xml]
    let string loc ~field_name:_ = [%expr Csvfields.Xml.string_of_xml]
    let char loc ~field_name:_ = [%expr Csvfields.Xml.char_of_xml]
    let int loc ~field_name:_ = [%expr Csvfields.Xml.int_of_xml]
    let float loc ~field_name:_ = [%expr Csvfields.Xml.float_of_xml]
    let int32 loc ~field_name:_ = [%expr Csvfields.Xml.int32_of_xml]
    let int64 loc ~field_name:_ = [%expr Csvfields.Xml.int64_of_xml]
    let nativeint loc ~field_name:_ = [%expr Csvfields.Xml.nativeint_of_xml]
    let big_int loc ~field_name:_ = [%expr Csvfields.Xml.big_int_of_xml]
    let nat loc ~field_name:_ = [%expr Csvfields.Xml.nat_of_xml]
    let num loc ~field_name:_ = [%expr Csvfields.Xml.num_of_xml]
    let ratio loc ~field_name:_ = [%expr Csvfields.Xml.ratio_of_xml]

    let merge_recursive loc ~field_name ~tp expr =
      match tp.ptyp_desc with
      | Ptyp_constr (id, [ _ ]) ->
        (match id.txt with
         | Lident "sexp_option" | Lident "option" ->
           [%expr
             fun xml ->
               match Csvfields.Xml.child xml [%e estring ~loc field_name] with
               | None -> None
               | Some _xml -> [%e expr] xml]
         | _ -> expr)
      | _ -> expr
    ;;

    let recursive loc ~field_name ~type_name ~path =
      let tns = "of_xml" in
      match type_name with
      | "sexp_option" | "option" | "list" | "array" ->
        [%expr [%e edot ~loc path tns] ~tag:[%e estring ~loc field_name]]
      | _ -> edot ~loc path tns
    ;;

    let list loc ~field_name =
      [%expr Csvfields.Xml.list_of_xml ~tag:[%e estring ~loc field_name]]
    ;;

    let array loc ~field_name =
      [%expr Csvfields.Xml.array_of_xml ~tag:[%e estring ~loc field_name]]
    ;;

    let option loc ~field_name =
      [%expr Csvfields.Xml.option_of_xml ~tag:[%e estring ~loc field_name]]
    ;;

    let lazy_t loc ~field_name:_ = [%expr Csvfields.Xml.lazy_t_of_xml]
    let ref loc ~field_name:_ = [%expr Csvfields.Xml.ref_of_xml]
  end)

let xml_of_type_function_name = function
  | None -> "to_xml"
  | Some param -> Printf.sprintf "%s_to_xml" param
;;

module Xml_of_type = Ppx_conv_func.Of_complete (struct
    let unsupported_type_error_msg = unsupported_type_error_msg
    let conversion_name = extension_name
    let function_name = xml_of_type_function_name
    let unit loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_unit]
    let bool loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_bool]
    let string loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_string]
    let char loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_char]
    let int loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_int]
    let float loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_float]
    let int32 loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_int32]
    let int64 loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_int64]
    let big_int loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_big_int]
    let nat loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_nat]
    let num loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_num]
    let ratio loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_ratio]
    let nativeint loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_nativeint]
    let merge_recursive = useless_merge_recursive

    let recursive loc ~field_name ~type_name ~path =
      let tns = function_name None in
      match type_name with
      | "sexp_option" | "option" | "list" | "array" ->
        [%expr [%e edot ~loc path tns] ~tag:[%e estring ~loc field_name]]
      | _ -> edot ~loc path tns
    ;;

    let list loc ~field_name =
      [%expr Csvfields.Xml.xml_of_list ~tag:[%e estring ~loc field_name]]
    ;;

    let array loc ~field_name =
      [%expr Csvfields.Xml.xml_of_array ~tag:[%e estring ~loc field_name]]
    ;;

    let option loc ~field_name =
      [%expr Csvfields.Xml.xml_of_option ~tag:[%e estring ~loc field_name]]
    ;;

    let lazy_t loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_lazy_t]
    let ref loc ~field_name:_ = [%expr Csvfields.Xml.xml_of_ref]
  end)

let make_xsd_definition loc arg_tys =
  List.fold_right arg_tys ~init:[%type: Csvfields.Xml.xml list] ~f:(fun _arg_ty acc ->
    [%type: Csvfields.Xml.xml list -> [%t acc]])
;;

let xml_record_sig ~tps ~record_name loc =
  let xsd = make_xsd_definition loc tps in
  let record_type = ptyp_constr ~loc (Located.lident ~loc record_name) tps in
  let to_xml =
    let base = [%type: Csvfields.Xml.xml list] in
    let tps = List.map tps ~f:(fun arg_ty -> [%type: [%t arg_ty] -> [%t base]]) in
    let init = [%type: [%t record_type] -> Csvfields.Xml.xml list] in
    List.fold_right tps ~init ~f:(fun arg acc -> [%type: [%t arg] -> [%t acc]])
  in
  let of_xml =
    let base = [%type: Csvfields.Xml.xml] in
    let tps = List.map tps ~f:(fun arg_ty -> [%type: [%t base] -> [%t arg_ty]]) in
    let init = [%type: Csvfields.Xml.xml -> [%t record_type]] in
    List.fold_right tps ~init ~f:(fun arg acc -> [%type: [%t arg] -> [%t acc]])
  in
  [ psig_value
      ~loc
      (value_description ~loc ~name:(Located.mk ~loc "xsd") ~type_:xsd ~prim:[])
  ; psig_value
      ~loc
      (value_description ~loc ~name:(Located.mk ~loc "to_xml") ~type_:to_xml ~prim:[])
  ; psig_value
      ~loc
      (value_description ~loc ~name:(Located.mk ~loc "of_xml") ~type_:of_xml ~prim:[])
  ]
;;

let param_of_ctyp ty =
  match ty.ptyp_desc with
  | Ptyp_var param -> Some param
  | _ -> None
;;

let make_parameter_functions loc ~function_name tps =
  List.fold_right tps ~init:[] ~f:(fun name acc ->
    let name = function_name (param_of_ctyp name) in
    pvar ~loc name :: acc)
;;

let is_list_array_or_option field_ty =
  match field_ty.ptyp_desc with
  | Ptyp_constr (id, [ _ ]) ->
    (match id.txt with
     | Lident ("sexp_option" | "option" | "list" | "array") -> true
     | _ -> false)
  | _ -> false
;;

let of_xml ~tps ~lds loc =
  let fields = Ppx_conv_func.Gen_struct.fields lds in
  let bindings =
    List.map
      ~f:(fun (field_name, _, field_ty) ->
        let conversion_fun = Type_of_xml.conversion_of_type loc ~field_name ~field_ty in
        let conversion_fun = [%expr [%e conversion_fun]] in
        if is_list_array_or_option field_ty
        then Located.lident ~loc field_name, [%expr [%e conversion_fun] xml]
        else
          ( Located.lident ~loc field_name
          , [%expr
            Csvfields.Xml.recursive_of_xml
              [%e estring ~loc field_name]
              [%e conversion_fun]
              xml] ))
      fields
  in
  let record = pexp_record ~loc bindings None in
  let arguments =
    make_parameter_functions loc ~function_name:type_of_xml_function_name tps
    @ [ [%pat? xml] ]
  in
  let labels =
    List.fold_right
      ~f:(fun (x, _, _) acc -> [%expr [%e estring ~loc x] :: [%e acc]])
      fields
      ~init:[%expr []]
  in
  let check = [%expr Csvfields.Xml.check_extra_fields xml [%e labels]] in
  let body = Ppx_conv_func.lambda loc arguments (pexp_sequence ~loc check record) in
  [%stri let of_xml = [%e body]]
;;

let prepend loc body =
  let acc = [%pat? acc] in
  let field = [%pat? _field] in
  let body = [%expr [%e body] :: acc] in
  Ppx_conv_func.lambda loc [ acc; field ] body
;;

let prepend_at loc body =
  let acc = [%pat? acc] in
  let field = [%pat? _field] in
  let body = [%expr [%e body] @ acc] in
  Ppx_conv_func.lambda loc [ acc; field ] body
;;

let xsd ~tps ~lds loc =
  let name = [%pat? xsd] in
  let conversion_of_type loc ~field_name ~field_ty =
    let body = Xsd_of_type.conversion_of_type loc ~field_name ~field_ty in
    prepend
      loc
      (if is_list_array_or_option field_ty
       then [%expr Csvfields.Xml.decomplexify [%e body]]
       else [%expr Csvfields.Xml.xsd_element ~name:[%e estring ~loc field_name] [%e body]])
  in
  let wrap_body body =
    let tps = make_parameter_functions loc ~function_name:xsd_function_name tps in
    Ppx_conv_func.lambda loc tps [%expr [ Csvfields.Xml.complex_type [%e body] ]]
  in
  Ppx_conv_func.Gen_struct.generate_using_fold
    ~wrap_body
    ~pass_acc:false
    ~pass_anonymous:false
    ~conversion_of_type
    ~name
    ~lds
    loc
;;

let to_xml ~tps ~lds loc =
  let name = [%pat? to_xml] in
  let conversion_of_type loc ~field_name ~field_ty =
    let body = Xml_of_type.conversion_of_type loc ~field_name ~field_ty in
    let expr =
      if is_list_array_or_option field_ty
      then [%expr [%e body] (Fieldslib.Field.get _field t)]
      else
        [%expr
          [ Csvfields.Xml.create_node
              ~tag:[%e estring ~loc field_name]
              ~body:([%e body] (Fieldslib.Field.get _field t))
          ]]
    in
    prepend_at loc expr
  in
  let wrap_body body =
    let tps =
      make_parameter_functions loc ~function_name:xml_of_type_function_name tps
    in
    let arguments = tps @ [ [%pat? t] ] in
    Ppx_conv_func.lambda loc arguments body
  in
  Ppx_conv_func.Gen_struct.generate_using_fold
    ~wrap_body
    ~pass_acc:false
    ~pass_anonymous:false
    ~conversion_of_type
    ~name
    ~lds
    loc
;;

let xml_record ~tps ~record_name:_ loc lds =
  let xsd = xsd ~tps ~lds loc in
  let to_xml = to_xml ~tps ~lds loc in
  let of_xml = of_xml ~tps ~lds loc in
  [ to_xml; of_xml; xsd ]
;;

let raise_unsupported ~loc s =
  Location.raise_errorf
    ~loc
    "Unsupported use of %s (you can only use it on records, abstract types or type \
     aliases)."
    s
;;

let xml_sig_of_ty ~extension_name =
  Ppx_conv_func.Gen_sig.generate
    ~extension_name
    ~nil:xml_record_sig
    ~record:(fun ~tps ~record_name loc _ -> xml_record_sig ~tps ~record_name loc)
;;

let generate_sig ~extension_name ~loc ~path ((_rf, tds) as x) =
  match tds with
  | [ _ ] -> xml_sig_of_ty ~extension_name ~loc ~path x
  | _ -> raise_unsupported ~loc extension_name
;;

let xml =
  Deriving.add
    extension_name
    ~sig_type_decl:(Deriving.Generator.make_noarg (generate_sig ~extension_name))
    ~str_type_decl:
      (Deriving.Generator.make_noarg
         (Ppx_conv_func.Gen_struct.generate ~extension_name ~record:xml_record)
         ~deps:[ Ppx_fields_conv.fields ])
;;
OCaml

Innovation. Community. Security.