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
module Ot = Pb_codegen_ocaml_type
module F = Pb_codegen_formatting
let sp = Pb_codegen_util.sp
let file_suffix = Pb_codegen_decode_yojson.file_suffix
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_int64, Ot.Pk_varint _ | Ot.Bt_int64, Ot.Pk_bits64 ->
"make_string", Some "Int64.to_string"
| 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 -> unsupported json_label
| _ -> unsupported json_label
let gen_field var_name json_label field_type pk =
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" in
let module_suffix = file_suffix in
Pb_codegen_util.function_name_of_user_defined ~function_prefix
~module_suffix 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.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.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" in
let module_suffix = file_suffix in
Pb_codegen_util.function_name_of_user_defined ~function_prefix
~module_suffix 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.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.linep sc "| %s v ->" vc_constructor;
F.scope sc (fun sc ->
match vc_field_type with
| Ot.Vct_nullary ->
F.linep sc "(\"%s\", `Null) :: assoc" 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\", `Null) :: assoc" json_label
| Some exp -> F.linep sc "%s :: assoc " exp)))
v_constructors);
F.linep sc "in (* match v.%s *)" rf_label
let gen_record ?and_ module_prefix { Ot.r_name; r_fields } sc =
let rn = r_name in
F.linep sc "%s encode_%s (v:%s_types.%s) = "
(Pb_codegen_util.let_decl_of_and and_)
rn module_prefix rn;
F.scope sc (fun sc ->
F.linep sc "let open !%s_types in" module_prefix;
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 _ ->
Printf.eprintf "Map field are not currently supported for JSON";
exit 1)
r_fields ;
F.line sc "`Assoc assoc")
let gen_variant ?and_ module_prefix { 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_types.%s -> `Assoc [(\"%s\", `Null)]" module_prefix
vc_constructor json_label
| Some exp ->
F.linep sc "| %s_types.%s v -> `Assoc [%s]" module_prefix vc_constructor
exp)
in
F.linep sc "%s encode_%s (v:%s_types.%s) = "
(Pb_codegen_util.let_decl_of_and and_)
v_name module_prefix v_name;
F.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_ module_prefix { Ot.cv_name; Ot.cv_constructors } sc
=
F.linep sc "%s encode_%s (v:%s_types.%s) = "
(Pb_codegen_util.let_decl_of_and and_)
cv_name module_prefix cv_name;
F.scope sc (fun sc ->
F.line sc "match v with";
List.iter
(fun { Ot.cvc_name; cvc_string_value; _ } ->
F.linep sc "| %s_types.%s -> `String \"%s\"" module_prefix cvc_name
cvc_string_value)
cv_constructors)
let gen_struct ?and_ t sc =
let { Ot.spec; module_prefix; _ } = t in
let has_encoded =
match spec with
| Ot.Record r ->
gen_record ?and_ module_prefix r sc;
true
| Ot.Variant v ->
gen_variant ?and_ module_prefix v sc;
true
| Ot.Const_variant v ->
gen_const_variant ?and_ module_prefix v sc;
true
in
has_encoded
let gen_sig ?and_ t sc =
let _ = and_ in
let { Ot.module_prefix; _ } = t in
let f type_name =
F.linep sc "val encode_%s : %s_types.%s -> Yojson.Basic.json" type_name
module_prefix type_name;
F.linep sc
("(** [encode_%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
let ocamldoc_title = "Protobuf YoJson Encoding"