Source file google_protobuf_struct_pc.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
[@@@ocaml.warning "-39"]
let (>>=) = Runtime.Result.(>>=)
let (>>|) = Runtime.Result.(>>|)
module Field' = Runtime.Field_value
module Bin' = Runtime.Binary_format
module Text' = Runtime.Text_format
module Null_value : sig
type t =
| Null_value
[@@deriving eq, show]
val default : unit -> t
val to_int : t -> int
val of_int : int -> t option
val to_string : t -> string
val of_string : string -> t option
end = struct
type t =
| Null_value
[@@deriving eq, show]
let default =
fun () -> Null_value
let to_int =
function
| Null_value -> 0
let of_int =
function
| 0 -> Some Null_value
| _ -> None
let to_string =
function
| Null_value -> "NULL_VALUE"
let of_string =
function
| "NULL_VALUE" -> Some Null_value
| _ -> None
end
module rec Struct' : sig
module rec Fields_entry : sig
type t = {
key : string;
value' : Value'.t option;
}
[@@deriving eq, show]
val to_binary : t -> (string, [> Bin'.serialization_error]) result
val of_binary : string -> (t, [> Bin'.deserialization_error]) result
val to_text : t -> (string, [> Text'.serialization_error]) result
val of_text : string -> (t, [> Text'.deserialization_error]) result
end
type t = {
fields : Struct'.Fields_entry.t list;
}
[@@deriving eq, show]
val to_binary : t -> (string, [> Bin'.serialization_error]) result
val of_binary : string -> (t, [> Bin'.deserialization_error]) result
val to_text : t -> (string, [> Text'.serialization_error]) result
val of_text : string -> (t, [> Text'.deserialization_error]) result
end = struct
module rec Fields_entry : sig
type t = {
key : string;
value' : Value'.t option;
}
[@@deriving eq, show]
val to_binary : t -> (string, [> Bin'.serialization_error]) result
val of_binary : string -> (t, [> Bin'.deserialization_error]) result
val to_text : t -> (string, [> Text'.serialization_error]) result
val of_text : string -> (t, [> Text'.deserialization_error]) result
end = struct
type t = {
key : string;
value' : Value'.t option;
}
[@@deriving eq, show]
let rec to_binary =
fun { key; value' } ->
let _o = Runtime.Byte_output.create () in
Bin'.serialize_field 1 Field'.String_t key _o >>= fun () ->
Bin'.serialize_user_field 2 Value'.to_binary value' _o >>= fun () ->
Ok (Runtime.Byte_output.contents _o)
let rec of_binary =
fun input' ->
Ok (Runtime.Byte_input.create input') >>=
Bin'.deserialize_message >>= fun _m ->
Bin'.decode_field 1 Field'.String_t _m >>= fun key ->
Bin'.decode_user_field 2 Value'.of_binary _m >>= fun value' ->
Ok { key; value' }
let rec to_text =
fun { key; value' } ->
let _o = Runtime.Byte_output.create () in
Text'.serialize_field "key" Field'.String_t key _o >>= fun () ->
Text'.serialize_user_field "value" Value'.to_text value' _o >>= fun () ->
Ok (Runtime.Byte_output.contents _o)
let rec of_text =
fun input' ->
Ok (Runtime.Byte_input.create input') >>=
Text'.deserialize_message >>= fun _m ->
Text'.decode_field "key" Field'.String_t _m >>= fun key ->
Text'.decode_user_field "value" Value'.of_text _m >>= fun value' ->
Ok { key; value' }
end
type t = {
fields : Struct'.Fields_entry.t list;
}
[@@deriving eq, show]
let rec to_binary =
fun { fields } ->
let _o = Runtime.Byte_output.create () in
Bin'.serialize_repeated_user_field 1 Struct'.Fields_entry.to_binary fields _o >>= fun () ->
Ok (Runtime.Byte_output.contents _o)
let rec of_binary =
fun input' ->
Ok (Runtime.Byte_input.create input') >>=
Bin'.deserialize_message >>= fun _m ->
Bin'.decode_repeated_user_field 1 Struct'.Fields_entry.of_binary _m >>= fun fields ->
Ok { fields }
let rec to_text =
fun { fields } ->
let _o = Runtime.Byte_output.create () in
Text'.serialize_repeated_user_field "fields" Struct'.Fields_entry.to_text fields _o >>= fun () ->
Ok (Runtime.Byte_output.contents _o)
let rec of_text =
fun input' ->
Ok (Runtime.Byte_input.create input') >>=
Text'.deserialize_message >>= fun _m ->
Text'.decode_repeated_user_field "fields" Struct'.Fields_entry.of_text _m >>= fun fields ->
Ok { fields }
end
and Value' : sig
module Kind : sig
type t =
| Null_value of Null_value.t
| Number_value of float
| String_value of string
| Bool_value of bool
| Struct_value of Struct'.t
| List_value of List_value.t
[@@deriving eq, show]
val null_value : Null_value.t -> t
val number_value : float -> t
val string_value : string -> t
val bool_value : bool -> t
val struct_value : Struct'.t -> t
val list_value : List_value.t -> t
end
type t = {
kind : Kind.t option;
}
[@@deriving eq, show]
val to_binary : t -> (string, [> Bin'.serialization_error]) result
val of_binary : string -> (t, [> Bin'.deserialization_error]) result
val to_text : t -> (string, [> Text'.serialization_error]) result
val of_text : string -> (t, [> Text'.deserialization_error]) result
end = struct
module Kind : sig
type t =
| Null_value of Null_value.t
| Number_value of float
| String_value of string
| Bool_value of bool
| Struct_value of Struct'.t
| List_value of List_value.t
[@@deriving eq, show]
val null_value : Null_value.t -> t
val number_value : float -> t
val string_value : string -> t
val bool_value : bool -> t
val struct_value : Struct'.t -> t
val list_value : List_value.t -> t
end = struct
type t =
| Null_value of Null_value.t
| Number_value of float
| String_value of string
| Bool_value of bool
| Struct_value of Struct'.t
| List_value of List_value.t
[@@deriving eq, show]
let null_value value = Null_value value
let number_value value = Number_value value
let string_value value = String_value value
let bool_value value = Bool_value value
let struct_value value = Struct_value value
let list_value value = List_value value
end
type t = {
kind : Kind.t option;
}
[@@deriving eq, show]
let rec to_binary =
fun { kind } ->
let _o = Runtime.Byte_output.create () in
(match kind with
| None -> Ok ()
| Some Null_value null_value -> Bin'.serialize_enum_field 1 Null_value.to_int null_value _o
| Some Number_value number_value -> Bin'.serialize_field 2 Field'.Double_t number_value _o
| Some String_value string_value -> Bin'.serialize_field 3 Field'.String_t string_value _o
| Some Bool_value bool_value -> Bin'.serialize_field 4 Field'.Bool_t bool_value _o
| Some Struct_value struct_value -> Bin'.serialize_user_oneof_field 5 Struct'.to_binary struct_value _o
| Some List_value list_value -> Bin'.serialize_user_oneof_field 6 List_value.to_binary list_value _o) >>= fun () ->
Ok (Runtime.Byte_output.contents _o)
let rec of_binary =
fun input' ->
Ok (Runtime.Byte_input.create input') >>=
Bin'.deserialize_message >>= fun _m ->
Bin'.decode_oneof_field [
1, (fun _m -> Bin'.decode_enum_field 1 Null_value.of_int Null_value.default _m >>| Kind.null_value);
2, (fun _m -> Bin'.decode_field 2 Field'.Double_t _m >>| Kind.number_value);
3, (fun _m -> Bin'.decode_field 3 Field'.String_t _m >>| Kind.string_value);
4, (fun _m -> Bin'.decode_field 4 Field'.Bool_t _m >>| Kind.bool_value);
5, (fun _m -> Bin'.decode_user_oneof_field 5 Struct'.of_binary _m >>| Kind.struct_value);
6, (fun _m -> Bin'.decode_user_oneof_field 6 List_value.of_binary _m >>| Kind.list_value);
] _m >>= fun kind ->
Ok { kind }
let rec to_text =
fun { kind } ->
let _o = Runtime.Byte_output.create () in
(match kind with
| None -> Ok ()
| Some Null_value null_value -> Text'.serialize_enum_field "null_value" Null_value.to_string null_value _o
| Some Number_value number_value -> Text'.serialize_field "number_value" Field'.Double_t number_value _o
| Some String_value string_value -> Text'.serialize_field "string_value" Field'.String_t string_value _o
| Some Bool_value bool_value -> Text'.serialize_field "bool_value" Field'.Bool_t bool_value _o
| Some Struct_value struct_value -> Text'.serialize_user_oneof_field "struct_value" Struct'.to_text struct_value _o
| Some List_value list_value -> Text'.serialize_user_oneof_field "list_value" List_value.to_text list_value _o) >>= fun () ->
Ok (Runtime.Byte_output.contents _o)
let rec of_text =
fun input' ->
Ok (Runtime.Byte_input.create input') >>=
Text'.deserialize_message >>= fun _m ->
Text'.decode_oneof_field [
"null_value", (fun _m -> Text'.decode_enum_field "null_value" Null_value.of_string Null_value.default _m >>| Kind.null_value);
"number_value", (fun _m -> Text'.decode_field "number_value" Field'.Double_t _m >>| Kind.number_value);
"string_value", (fun _m -> Text'.decode_field "string_value" Field'.String_t _m >>| Kind.string_value);
"bool_value", (fun _m -> Text'.decode_field "bool_value" Field'.Bool_t _m >>| Kind.bool_value);
"struct_value", (fun _m -> Text'.decode_user_oneof_field "struct_value" Struct'.of_text _m >>| Kind.struct_value);
"list_value", (fun _m -> Text'.decode_user_oneof_field "list_value" List_value.of_text _m >>| Kind.list_value);
] _m >>= fun kind ->
Ok { kind }
end
and List_value : sig
type t = {
values : Value'.t list;
}
[@@deriving eq, show]
val to_binary : t -> (string, [> Bin'.serialization_error]) result
val of_binary : string -> (t, [> Bin'.deserialization_error]) result
val to_text : t -> (string, [> Text'.serialization_error]) result
val of_text : string -> (t, [> Text'.deserialization_error]) result
end = struct
type t = {
values : Value'.t list;
}
[@@deriving eq, show]
let rec to_binary =
fun { values } ->
let _o = Runtime.Byte_output.create () in
Bin'.serialize_repeated_user_field 1 Value'.to_binary values _o >>= fun () ->
Ok (Runtime.Byte_output.contents _o)
let rec of_binary =
fun input' ->
Ok (Runtime.Byte_input.create input') >>=
Bin'.deserialize_message >>= fun _m ->
Bin'.decode_repeated_user_field 1 Value'.of_binary _m >>= fun values ->
Ok { values }
let rec to_text =
fun { values } ->
let _o = Runtime.Byte_output.create () in
Text'.serialize_repeated_user_field "values" Value'.to_text values _o >>= fun () ->
Ok (Runtime.Byte_output.contents _o)
let rec of_text =
fun input' ->
Ok (Runtime.Byte_input.create input') >>=
Text'.deserialize_message >>= fun _m ->
Text'.decode_repeated_user_field "values" Value'.of_text _m >>= fun values ->
Ok { values }
end