package ppx_deriving_yaml

  1. Overview
  2. Docs

Source file ppx_deriving_yaml.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
open Ppxlib
open Ast_helper
open Ast_builder.Default

let suf_to = Helpers.suf_to
let suf_of = Helpers.suf_of

let mangle_name_label suff label =
  if label = "t" then suff else label ^ "_" ^ suff

let generate_impl_of_yaml ~ctxt (_rec_flag, type_decls) skip_unknown =
  let loc = Expansion_context.Deriver.derived_item_loc ctxt in
  List.concat
    (List.map
       (fun typ_decl ->
         match typ_decl with
         | { ptype_kind = Ptype_abstract; ptype_manifest; ptype_name; _ } -> (
             match ptype_manifest with
             | Some t ->
                 let ocamliser =
                   Helpers.poly_fun ~loc:typ_decl.ptype_loc typ_decl
                     (Value.of_yaml_type_to_expr None t)
                 in
                 let of_yaml = mangle_name_label suf_of ptype_name.txt in
                 [
                   pstr_value ~loc Nonrecursive
                     [ Vb.mk (ppat_var ~loc { loc; txt = of_yaml }) ocamliser ];
                 ]
             | None ->
                 Location.raise_errorf ~loc
                   "Cannot derive anything for this type")
         | { ptype_kind = Ptype_variant constructors; ptype_name; _ } ->
             let of_yaml = mangle_name_label suf_of ptype_name.txt in
             let of_yaml_cases =
               List.map
                 (fun ({ pcd_name; pcd_args; _ } as p) ->
                   let name =
                     Option.value ~default:pcd_name.txt
                       (Attribute.get Attrs.name p)
                   in
                   match pcd_args with
                   | Pcstr_tuple args ->
                       let tuple =
                         if List.length args = 0 then None
                         else
                           Some
                             (Helpers.etuple ~loc
                                (List.mapi
                                   (fun i _ -> evar ~loc (Helpers.arg i))
                                   args))
                       in
                       Exp.case
                         [%pat?
                           `O
                             [
                               ( [%p pstring ~loc name],
                                 `A
                                   [%p
                                     plist ~loc
                                       (List.mapi
                                          (fun i _ -> pvar ~loc (Helpers.arg i))
                                          args)] );
                             ]]
                         Value.(
                           monad_fold
                             (of_yaml_type_to_expr None)
                             [%expr
                               Result.Ok
                                 [%e
                                   Exp.construct
                                     {
                                       txt = Lident pcd_name.txt;
                                       loc = pcd_name.loc;
                                     }
                                     tuple]]
                             (List.mapi (fun i t -> (t, i)) args))
                   | _ -> failwith "Not implemented!")
                 constructors
             in
             let of_yaml_cases =
               of_yaml_cases
               @ [
                   Exp.case
                     [%pat? _]
                     [%expr Error (`Msg "no match for this variant expression")];
                 ]
             in
             let of_yaml_expr =
               Value.wrap_open_rresult ~loc (Exp.function_ ~loc of_yaml_cases)
             in
             [
               pstr_value ~loc Nonrecursive
                 [ Vb.mk (ppat_var ~loc { loc; txt = of_yaml }) of_yaml_expr ];
             ]
         | { ptype_kind = Ptype_record fields; ptype_loc; ptype_name; _ } ->
             let of_yaml = mangle_name_label suf_of ptype_name.txt in
             [
               pstr_value ~loc Nonrecursive
                 [
                   Vb.mk
                     (ppat_var ~loc { loc; txt = of_yaml })
                     (Helpers.poly_fun ~loc:ptype_loc typ_decl
                        (Value.of_yaml_record_to_expr ~skip_unknown
                           ~loc:ptype_loc fields));
                 ];
             ]
         | _ ->
             Location.raise_errorf ~loc "Cannot derive anything for this type")
       type_decls)

let generate_impl_to_yaml ~ctxt (_rec_flag, type_decls) =
  let loc = Expansion_context.Deriver.derived_item_loc ctxt in
  List.concat
    (List.map
       (fun typ_decl ->
         match typ_decl with
         | { ptype_kind = Ptype_abstract; ptype_manifest; ptype_name; _ } -> (
             match ptype_manifest with
             | Some t ->
                 let yamliser =
                   Helpers.poly_fun ~loc:typ_decl.ptype_loc typ_decl
                     [%expr [%e Value.type_to_expr t]]
                 in
                 let to_yaml = mangle_name_label suf_to ptype_name.txt in
                 [
                   pstr_value ~loc Nonrecursive
                     [ Vb.mk (ppat_var ~loc { loc; txt = to_yaml }) yamliser ];
                 ]
             | None ->
                 Location.raise_errorf ~loc
                   "Cannot derive anything for this type")
         | { ptype_kind = Ptype_variant constructors; ptype_name; _ } ->
             let to_yaml = mangle_name_label suf_to ptype_name.txt in
             let to_yaml_cases =
               List.map
                 (fun ({ pcd_name; pcd_args; _ } as p) ->
                   let name =
                     Option.value ~default:pcd_name.txt
                       (Attribute.get Attrs.name p)
                   in
                   match pcd_args with
                   | Pcstr_tuple args ->
                       let pat_arg =
                         if List.length args = 0 then None
                         else
                           Some
                             (Helpers.ptuple ~loc
                                (List.mapi
                                   (fun i _ -> pvar ~loc (Helpers.arg i))
                                   args))
                       in
                       Exp.case (pconstruct p pat_arg)
                         [%expr
                           `O
                             [
                               ( [%e estring ~loc name],
                                 `A
                                   [%e
                                     elist ~loc
                                       (List.mapi
                                          (fun i t ->
                                            [%expr
                                              [%e Value.type_to_expr t]
                                                [%e evar ~loc (Helpers.arg i)]])
                                          args)] );
                             ]]
                   | _ -> failwith "Not implemented!")
                 constructors
             in
             let to_yaml_expr = Exp.function_ ~loc to_yaml_cases in
             [
               pstr_value ~loc Nonrecursive
                 [ Vb.mk (ppat_var ~loc { loc; txt = to_yaml }) to_yaml_expr ];
             ]
         | { ptype_kind = Ptype_record fields; ptype_loc; ptype_name; _ } ->
             let to_yaml = mangle_name_label suf_to ptype_name.txt in
             [
               pstr_value ~loc Nonrecursive
                 [
                   Vb.mk
                     [%pat? [%p ppat_var ~loc { loc; txt = to_yaml }]]
                     [%expr
                       [%e
                         Helpers.poly_fun ~loc:ptype_loc typ_decl
                           (Value.record_to_expr
                              ~typ:(core_type_of_type_declaration typ_decl)
                              ~loc:ptype_loc fields)]];
                 ];
             ]
         | _ ->
             Location.raise_errorf ~loc "Cannot derive anything for this type")
       type_decls)

let generate_intf_to_yaml ~ctxt (_rec_flag, type_decls) :
    Ppxlib.Ast.signature_item list =
  let loc = Expansion_context.Deriver.derived_item_loc ctxt in
  List.map
    (fun typ_decl ->
      match typ_decl with
      | { ptype_kind = Ptype_abstract | Ptype_record _; _ } ->
          [
            psig_value ~loc
              (Val.mk
                 {
                   loc = typ_decl.ptype_name.loc;
                   txt =
                     mangle_name_label Helpers.suf_to typ_decl.ptype_name.txt;
                 }
                 (Value.type_decl_to_type typ_decl));
          ]
      | _ -> Location.raise_errorf ~loc "Cannot derive anything for this type")
    type_decls
  |> List.concat

let generate_intf_of_yaml ~ctxt (_rec_flag, type_decls) :
    Ppxlib.Ast.signature_item list =
  let loc = Expansion_context.Deriver.derived_item_loc ctxt in
  List.map
    (fun typ_decl ->
      match typ_decl with
      | { ptype_kind = Ptype_abstract | Ptype_record _; _ } ->
          [
            psig_value ~loc
              (Val.mk
                 {
                   loc = typ_decl.ptype_name.loc;
                   txt =
                     mangle_name_label Helpers.suf_of typ_decl.ptype_name.txt;
                 }
                 (Value.type_decl_of_type typ_decl));
          ]
      | _ -> Location.raise_errorf ~loc "Cannot derive anything for this type")
    type_decls
  |> List.concat

let impl_generator_to impl =
  Deriving.Generator.V2.make_noarg
    ~attributes:
      [
        Attribute.T Attrs.default; Attribute.T Attrs.name; Attribute.T Attrs.key;
      ]
    impl

let impl_generator_of impl =
  Deriving.Generator.V2.make
    ~attributes:
      [
        Attribute.T Attrs.default; Attribute.T Attrs.name; Attribute.T Attrs.key;
      ]
    Deriving.Args.(empty +> flag "skip_unknown")
    impl

let intf_generator intf = Deriving.Generator.V2.make_noarg intf

let deriver =
  let of_yaml =
    Deriving.add "of_yaml"
      ~str_type_decl:(impl_generator_of generate_impl_of_yaml)
      ~sig_type_decl:(intf_generator generate_intf_of_yaml)
  in
  let to_yaml =
    Deriving.add "to_yaml"
      ~str_type_decl:(impl_generator_to generate_impl_to_yaml)
      ~sig_type_decl:(intf_generator generate_intf_to_yaml)
  in
  Deriving.add_alias "yaml" [ of_yaml; to_yaml ]