Source file pb_codegen_encode_yojson.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
module Ot = Pb_codegen_ocaml_type
module F = Pb_codegen_formatting
let sp = Pb_codegen_util.sp
let unsupported json_label =
failwith (sp "Unsupported field type for field: %s" json_label)
let runtime_function_for_basic_type json_label basic_type pk =
match basic_type, pk with
| Ot.Bt_string, _ -> "make_string", None
| Ot.Bt_float, Ot.Pk_bits32 -> "make_float", None
| Ot.Bt_float, Ot.Pk_bits64 -> "make_string", Some "string_of_float"
| Ot.Bt_int32, Ot.Pk_varint _ | Ot.Bt_int32, Ot.Pk_bits32 ->
"make_int", Some "Int32.to_int"
| Ot.Bt_uint32, Ot.Pk_varint _ | Ot.Bt_uint32, Ot.Pk_bits32 ->
"make_int", Some "(fun (`unsigned x) -> Int32.to_int x)"
| Ot.Bt_int64, Ot.Pk_varint _ | Ot.Bt_int64, Ot.Pk_bits64 ->
"make_string", Some "Int64.to_string"
| Ot.Bt_uint64, Ot.Pk_varint _ | Ot.Bt_uint64, Ot.Pk_bits64 ->
"make_string", Some "(fun (`unsigned x) -> Int64.to_string x)"
| Ot.Bt_int, Ot.Pk_bits32 -> "make_int", None
| Ot.Bt_int, Ot.Pk_varint _ | Ot.Bt_int, Ot.Pk_bits64 ->
"make_string", Some "string_of_int"
| Ot.Bt_bool, Ot.Pk_varint _ -> "make_bool", None
| Ot.Bt_bytes, Ot.Pk_bytes -> "make_bytes", None
| _ -> unsupported json_label
let gen_field var_name json_label field_type pk : string option =
match field_type, pk with
| Ot.Ft_unit, _ -> None
| Ot.Ft_basic_type basic_type, _ ->
let runtime_f, map_function =
runtime_function_for_basic_type json_label basic_type pk
in
(match map_function with
| None ->
Some (sp "(\"%s\", Pbrt_yojson.%s %s)" json_label runtime_f var_name)
| Some map_function ->
Some
(sp "(\"%s\", Pbrt_yojson.%s (%s %s))" json_label runtime_f map_function
var_name))
| Ot.Ft_user_defined_type udt, _ ->
let f_name =
let function_prefix = "encode_json" in
Pb_codegen_util.function_name_of_user_defined ~function_prefix udt
in
Some (sp "(\"%s\", %s %s)" json_label f_name var_name)
| _ -> assert false
let gen_rft_nolabel sc rf_label (field_type, _, pk) =
let var_name = sp "v.%s" rf_label in
let json_label = Pb_codegen_util.camel_case_of_label rf_label in
match gen_field var_name json_label field_type pk with
| None -> ()
| Some exp -> F.linep sc "let assoc = %s :: assoc in" exp
let gen_rft_optional sc rf_label (field_type, _, pk, _) =
F.linep sc "let assoc = match v.%s with" rf_label;
F.sub_scope sc (fun sc ->
F.line sc "| None -> assoc";
let json_label = Pb_codegen_util.camel_case_of_label rf_label in
match gen_field "v" json_label field_type pk with
| None -> F.line sc "| Some v -> assoc"
| Some exp -> F.linep sc "| Some v -> %s :: assoc" exp);
F.line sc "in"
let gen_rft_repeated sc rf_label repeated_field =
let repeated_type, field_type, _, pk, _ = repeated_field in
(match repeated_type with
| Ot.Rt_list -> ()
| Ot.Rt_repeated_field ->
sp "Pbrt.Repeated_field is not supported with JSON (field: %s)" rf_label
|> failwith);
let var_name = sp "v.%s" rf_label in
let json_label = Pb_codegen_util.camel_case_of_label rf_label in
F.line sc "let assoc =";
F.sub_scope sc (fun sc ->
(match field_type, pk with
| Ot.Ft_unit, _ -> unsupported json_label
| Ot.Ft_basic_type basic_type, _ ->
let runtime_f, map_function =
runtime_function_for_basic_type json_label basic_type pk
in
(match map_function with
| None ->
F.linep sc "let l = %s |> List.map Pbrt_yojson.%s in" var_name
runtime_f
| Some map_function ->
F.linep sc "let l = %s |> List.map %s |> List.map Pbrt_yojson.%s in "
var_name map_function runtime_f)
| Ot.Ft_user_defined_type udt, _ ->
let f_name =
let function_prefix = "encode_json" in
Pb_codegen_util.function_name_of_user_defined ~function_prefix udt
in
F.linep sc "let l = %s |> List.map %s in" var_name f_name
| _ -> unsupported json_label);
F.linep sc "(\"%s\", `List l) :: assoc " json_label);
F.line sc "in"
let gen_rft_variant sc rf_label { Ot.v_constructors; _ } =
F.linep sc "let assoc = match v.%s with" rf_label;
F.sub_scope sc (fun sc ->
List.iter
(fun { Ot.vc_constructor; vc_field_type; vc_payload_kind; _ } ->
let var_name = "v" in
let json_label =
Pb_codegen_util.camel_case_of_constructor vc_constructor
in
F.sub_scope sc (fun sc ->
match vc_field_type with
| Ot.Vct_nullary ->
F.linep sc "| %s -> (\"%s\", `Null) :: assoc" vc_constructor json_label
| Ot.Vct_non_nullary_constructor field_type ->
(match
gen_field var_name json_label field_type vc_payload_kind
with
| None -> F.linep sc "| %s -> (\"%s\", `Null) :: assoc" vc_constructor json_label
| Some exp -> F.linep sc "| %s v -> %s :: assoc" vc_constructor exp)))
v_constructors);
F.linep sc "in (* match v.%s *)" rf_label
let gen_rft_assoc sc ~rf_label ~assoc_type ~key_type
~value_field:(value_type, value_pk) =
let var_name = sp "v.%s" rf_label in
let json_label = Pb_codegen_util.camel_case_of_label rf_label in
let key_pat, key_exp =
match key_type with
| Ot.Bt_string -> "key", "key"
| Ot.Bt_int -> "key", "(Int.to_string key)"
| Ot.Bt_int32 -> "key", "(Int32.to_string key)"
| Ot.Bt_int64 -> "key", "(Int64.to_string key)"
| Ot.Bt_uint32 -> "(`unsigned key)", "(Int32.to_string key)"
| Ot.Bt_uint64 -> "(`unsigned key)", "(Int64.to_string key)"
| Ot.Bt_bool -> "key", "(Bool.to_string key)"
| Ot.Bt_float ->
Printf.eprintf "float cannot be used as a map key type";
exit 1
| Ot.Bt_bytes ->
Printf.eprintf "bytes cannot be used as a map key type";
exit 1
in
let write_assoc_field ~fn ~var_name =
F.line sc "let assoc_field =";
F.sub_scope sc (fun sc ->
F.linep sc "%s" var_name;
(match assoc_type with
| Ot.At_list -> ()
| Ot.At_hashtable -> F.line sc "|> Hashtbl.to_seq |> List.of_seq");
F.linep sc "|> List.map (fun (%s, value) -> %s, %s value)" key_pat
key_exp fn);
F.line sc "in"
in
F.line sc "let assoc =";
F.sub_scope sc (fun sc ->
(match value_type with
| Ot.Ft_unit -> unsupported json_label
| Ot.Ft_basic_type basic_type ->
let runtime_f, map_function =
runtime_function_for_basic_type json_label basic_type value_pk
in
(match map_function with
| None -> write_assoc_field ~fn:("Pbrt_yojson." ^ runtime_f) ~var_name
| Some map_function ->
let fn =
Printf.sprintf "(fun value -> value |> %s |> Pbrt_yojson.%s)"
map_function runtime_f
in
write_assoc_field ~fn ~var_name)
| Ot.Ft_user_defined_type udt ->
let fn =
let function_prefix = "encode_json" in
Pb_codegen_util.function_name_of_user_defined ~function_prefix udt
in
write_assoc_field ~fn ~var_name
| _ -> unsupported json_label);
F.linep sc "(\"%s\", `Assoc assoc_field) :: assoc " json_label);
F.line sc "in"
let gen_record ?and_ { Ot.r_name; r_fields } sc =
let rn = r_name in
F.linep sc "%s encode_json_%s (v:%s) = "
(Pb_codegen_util.let_decl_of_and and_)
rn rn;
F.sub_scope sc (fun sc ->
F.line sc "let assoc = [] in ";
List.iter
(fun record_field ->
let { Ot.rf_label; rf_field_type; _ } = record_field in
match rf_field_type with
| Ot.Rft_nolabel nolabel_field ->
gen_rft_nolabel sc rf_label nolabel_field
| Ot.Rft_repeated repeated_field ->
gen_rft_repeated sc rf_label repeated_field
| Ot.Rft_variant variant_field ->
gen_rft_variant sc rf_label variant_field
| Ot.Rft_optional optional_field ->
gen_rft_optional sc rf_label optional_field
| Ot.Rft_required _ ->
Printf.eprintf "Only proto3 syntax supported in JSON encoding";
exit 1
| Ot.Rft_associative (assoc_type, _, (key_type, _), value_field) ->
gen_rft_assoc sc ~rf_label ~assoc_type ~key_type ~value_field)
r_fields ;
F.line sc "`Assoc assoc")
let gen_unit ?and_ { Ot.er_name } sc =
let rn = er_name in
F.linep sc "%s encode_json_%s (v:%s) = "
(Pb_codegen_util.let_decl_of_and and_)
rn rn;
F.line sc (sp "Pbrt_yojson.%s %s" "make_unit" "v")
let gen_variant ?and_ { Ot.v_name; v_constructors } sc =
let process_v_constructor sc v_constructor =
let { Ot.vc_constructor; Ot.vc_field_type; Ot.vc_payload_kind; _ } =
v_constructor
in
let json_label = Pb_codegen_util.camel_case_of_constructor vc_constructor in
match vc_field_type with
| Ot.Vct_nullary ->
F.linep sc "| %s -> `Assoc [(\"%s\", `Null)]" vc_constructor json_label
| Ot.Vct_non_nullary_constructor field_type ->
(match gen_field "v" json_label field_type vc_payload_kind with
| None ->
F.linep sc "| %s -> `Assoc [(\"%s\", `Null)]" vc_constructor json_label
| Some exp -> F.linep sc "| %s v -> `Assoc [%s]" vc_constructor exp)
in
F.linep sc "%s encode_json_%s (v:%s) = "
(Pb_codegen_util.let_decl_of_and and_)
v_name v_name;
F.sub_scope sc (fun sc ->
F.line sc "begin match v with";
List.iter (process_v_constructor sc) v_constructors;
F.line sc "end")
let gen_const_variant ?and_ { Ot.cv_name; Ot.cv_constructors } sc =
F.linep sc "%s encode_json_%s (v:%s) = "
(Pb_codegen_util.let_decl_of_and and_)
cv_name cv_name;
F.sub_scope sc (fun sc ->
F.line sc "match v with";
List.iter
(fun { Ot.cvc_name; cvc_string_value; _ } ->
F.linep sc "| %s -> `String \"%s\"" cvc_name cvc_string_value)
cv_constructors)
let gen_struct ?and_ t sc =
let { Ot.spec; _ } = t in
let has_encoded =
match spec with
| Ot.Record r ->
gen_record ?and_ r sc;
true
| Ot.Variant v ->
gen_variant ?and_ v sc;
true
| Ot.Const_variant v ->
gen_const_variant ?and_ v sc;
true
| Ot.Unit u ->
gen_unit ?and_ u sc;
true
in
has_encoded
let gen_sig ?and_ t sc =
let _ = and_ in
let f type_name =
F.linep sc "val encode_json_%s : %s -> Yojson.Basic.t" type_name type_name;
F.linep sc
("(** [encode_json_%s v encoder] encodes [v] to " ^^ "to json *)")
type_name
in
match t with
| { Ot.spec = Ot.Record { Ot.r_name; _ }; _ } ->
f r_name;
true
| { Ot.spec = Ot.Variant v; _ } ->
f v.Ot.v_name;
true
| { Ot.spec = Ot.Const_variant { Ot.cv_name; _ }; _ } ->
f cv_name;
true
| { Ot.spec = Ot.Unit { Ot.er_name; _ }; _ } ->
f er_name;
true
let ocamldoc_title = "Protobuf YoJson Encoding"
let requires_mutable_records = false
let plugin : Pb_codegen_plugin.t =
let module P = struct
let gen_sig = gen_sig
let gen_struct = gen_struct
let ocamldoc_title = ocamldoc_title
let requires_mutable_records = requires_mutable_records
end in
(module P)