Source file common.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
[@@@ocaml.warning "-23-27-30-39-44"]

type any_value =
  | String_value of string
  | Bool_value of bool
  | Int_value of int64
  | Double_value of float
  | Array_value of array_value
  | Kvlist_value of key_value_list
  | Bytes_value of bytes

and array_value = {
  mutable values : any_value list;
}

and key_value_list = {
  mutable values : key_value list;
}

and key_value = {
  mutable _presence: Pbrt.Bitfield.t; (** presence for 1 fields *)
  mutable key : string;
  mutable value : any_value option;
}

type instrumentation_scope = {
  mutable _presence: Pbrt.Bitfield.t; (** presence for 3 fields *)
  mutable name : string;
  mutable version : string;
  mutable attributes : key_value list;
  mutable dropped_attributes_count : int32;
}

type entity_ref = {
  mutable _presence: Pbrt.Bitfield.t; (** presence for 2 fields *)
  mutable schema_url : string;
  mutable type_ : string;
  mutable id_keys : string list;
  mutable description_keys : string list;
}

let default_any_value (): any_value = String_value ("")

let default_array_value (): array_value =
{
  values=[];
}

let default_key_value_list (): key_value_list =
{
  values=[];
}

let default_key_value (): key_value =
{
  _presence=Pbrt.Bitfield.empty;
  key="";
  value=None;
}

let default_instrumentation_scope (): instrumentation_scope =
{
  _presence=Pbrt.Bitfield.empty;
  name="";
  version="";
  attributes=[];
  dropped_attributes_count=0l;
}

let default_entity_ref (): entity_ref =
{
  _presence=Pbrt.Bitfield.empty;
  schema_url="";
  type_="";
  id_keys=[];
  description_keys=[];
}


(** {2 Make functions} *)


let[@inline] array_value_set_values (self:array_value) (x:any_value list) : unit =
  self.values <- x

let copy_array_value (self:array_value) : array_value =
  { self with values = self.values }

let make_array_value 
  ?(values=[])
  () : array_value  =
  let _res = default_array_value () in
  array_value_set_values _res values;
  _res


let[@inline] key_value_list_set_values (self:key_value_list) (x:key_value list) : unit =
  self.values <- x

let copy_key_value_list (self:key_value_list) : key_value_list =
  { self with values = self.values }

let make_key_value_list 
  ?(values=[])
  () : key_value_list  =
  let _res = default_key_value_list () in
  key_value_list_set_values _res values;
  _res

let[@inline] key_value_has_key (self:key_value) : bool = (Pbrt.Bitfield.get self._presence 0)

let[@inline] key_value_set_key (self:key_value) (x:string) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 0); self.key <- x
let[@inline] key_value_set_value (self:key_value) (x:any_value) : unit =
  self.value <- Some x

let copy_key_value (self:key_value) : key_value =
  { self with key = self.key }

let make_key_value 
  ?(key:string option)
  ?(value:any_value option)
  () : key_value  =
  let _res = default_key_value () in
  (match key with
  | None -> ()
  | Some v -> key_value_set_key _res v);
  (match value with
  | None -> ()
  | Some v -> key_value_set_value _res v);
  _res

let[@inline] instrumentation_scope_has_name (self:instrumentation_scope) : bool = (Pbrt.Bitfield.get self._presence 0)
let[@inline] instrumentation_scope_has_version (self:instrumentation_scope) : bool = (Pbrt.Bitfield.get self._presence 1)
let[@inline] instrumentation_scope_has_dropped_attributes_count (self:instrumentation_scope) : bool = (Pbrt.Bitfield.get self._presence 2)

let[@inline] instrumentation_scope_set_name (self:instrumentation_scope) (x:string) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 0); self.name <- x
let[@inline] instrumentation_scope_set_version (self:instrumentation_scope) (x:string) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 1); self.version <- x
let[@inline] instrumentation_scope_set_attributes (self:instrumentation_scope) (x:key_value list) : unit =
  self.attributes <- x
let[@inline] instrumentation_scope_set_dropped_attributes_count (self:instrumentation_scope) (x:int32) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 2); self.dropped_attributes_count <- x

let copy_instrumentation_scope (self:instrumentation_scope) : instrumentation_scope =
  { self with name = self.name }

let make_instrumentation_scope 
  ?(name:string option)
  ?(version:string option)
  ?(attributes=[])
  ?(dropped_attributes_count:int32 option)
  () : instrumentation_scope  =
  let _res = default_instrumentation_scope () in
  (match name with
  | None -> ()
  | Some v -> instrumentation_scope_set_name _res v);
  (match version with
  | None -> ()
  | Some v -> instrumentation_scope_set_version _res v);
  instrumentation_scope_set_attributes _res attributes;
  (match dropped_attributes_count with
  | None -> ()
  | Some v -> instrumentation_scope_set_dropped_attributes_count _res v);
  _res

let[@inline] entity_ref_has_schema_url (self:entity_ref) : bool = (Pbrt.Bitfield.get self._presence 0)
let[@inline] entity_ref_has_type_ (self:entity_ref) : bool = (Pbrt.Bitfield.get self._presence 1)

let[@inline] entity_ref_set_schema_url (self:entity_ref) (x:string) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 0); self.schema_url <- x
let[@inline] entity_ref_set_type_ (self:entity_ref) (x:string) : unit =
  self._presence <- (Pbrt.Bitfield.set self._presence 1); self.type_ <- x
let[@inline] entity_ref_set_id_keys (self:entity_ref) (x:string list) : unit =
  self.id_keys <- x
let[@inline] entity_ref_set_description_keys (self:entity_ref) (x:string list) : unit =
  self.description_keys <- x

let copy_entity_ref (self:entity_ref) : entity_ref =
  { self with schema_url = self.schema_url }

let make_entity_ref 
  ?(schema_url:string option)
  ?(type_:string option)
  ?(id_keys=[])
  ?(description_keys=[])
  () : entity_ref  =
  let _res = default_entity_ref () in
  (match schema_url with
  | None -> ()
  | Some v -> entity_ref_set_schema_url _res v);
  (match type_ with
  | None -> ()
  | Some v -> entity_ref_set_type_ _res v);
  entity_ref_set_id_keys _res id_keys;
  entity_ref_set_description_keys _res description_keys;
  _res

[@@@ocaml.warning "-23-27-30-39"]

(** {2 Formatters} *)

let rec pp_any_value fmt (v:any_value) =
  match v with
  | String_value x -> Format.fprintf fmt "@[<hv2>String_value(@,%a)@]" Pbrt.Pp.pp_string x
  | Bool_value x -> Format.fprintf fmt "@[<hv2>Bool_value(@,%a)@]" Pbrt.Pp.pp_bool x
  | Int_value x -> Format.fprintf fmt "@[<hv2>Int_value(@,%a)@]" Pbrt.Pp.pp_int64 x
  | Double_value x -> Format.fprintf fmt "@[<hv2>Double_value(@,%a)@]" Pbrt.Pp.pp_float x
  | Array_value x -> Format.fprintf fmt "@[<hv2>Array_value(@,%a)@]" pp_array_value x
  | Kvlist_value x -> Format.fprintf fmt "@[<hv2>Kvlist_value(@,%a)@]" pp_key_value_list x
  | Bytes_value x -> Format.fprintf fmt "@[<hv2>Bytes_value(@,%a)@]" Pbrt.Pp.pp_bytes x

and pp_array_value fmt (v:array_value) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~first:true "values" (Pbrt.Pp.pp_list pp_any_value) fmt v.values;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

and pp_key_value_list fmt (v:key_value_list) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~first:true "values" (Pbrt.Pp.pp_list pp_key_value) fmt v.values;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

and pp_key_value fmt (v:key_value) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~absent:(not (key_value_has_key v)) ~first:true "key" Pbrt.Pp.pp_string fmt v.key;
    Pbrt.Pp.pp_record_field ~first:false "value" (Pbrt.Pp.pp_option pp_any_value) fmt v.value;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

let rec pp_instrumentation_scope fmt (v:instrumentation_scope) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~absent:(not (instrumentation_scope_has_name v)) ~first:true "name" Pbrt.Pp.pp_string fmt v.name;
    Pbrt.Pp.pp_record_field ~absent:(not (instrumentation_scope_has_version v)) ~first:false "version" Pbrt.Pp.pp_string fmt v.version;
    Pbrt.Pp.pp_record_field ~first:false "attributes" (Pbrt.Pp.pp_list pp_key_value) fmt v.attributes;
    Pbrt.Pp.pp_record_field ~absent:(not (instrumentation_scope_has_dropped_attributes_count v)) ~first:false "dropped_attributes_count" Pbrt.Pp.pp_int32 fmt v.dropped_attributes_count;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

let rec pp_entity_ref fmt (v:entity_ref) = 
  let pp_i fmt () =
    Pbrt.Pp.pp_record_field ~absent:(not (entity_ref_has_schema_url v)) ~first:true "schema_url" Pbrt.Pp.pp_string fmt v.schema_url;
    Pbrt.Pp.pp_record_field ~absent:(not (entity_ref_has_type_ v)) ~first:false "type_" Pbrt.Pp.pp_string fmt v.type_;
    Pbrt.Pp.pp_record_field ~first:false "id_keys" (Pbrt.Pp.pp_list Pbrt.Pp.pp_string) fmt v.id_keys;
    Pbrt.Pp.pp_record_field ~first:false "description_keys" (Pbrt.Pp.pp_list Pbrt.Pp.pp_string) fmt v.description_keys;
  in
  Pbrt.Pp.pp_brk pp_i fmt ()

[@@@ocaml.warning "-23-27-30-39"]

(** {2 Protobuf Encoding} *)

let rec encode_pb_any_value (v:any_value) encoder = 
  begin match v with
  | String_value x ->
    Pbrt.Encoder.string x encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  | Bool_value x ->
    Pbrt.Encoder.bool x encoder;
    Pbrt.Encoder.key 2 Pbrt.Varint encoder; 
  | Int_value x ->
    Pbrt.Encoder.int64_as_varint x encoder;
    Pbrt.Encoder.key 3 Pbrt.Varint encoder; 
  | Double_value x ->
    Pbrt.Encoder.float_as_bits64 x encoder;
    Pbrt.Encoder.key 4 Pbrt.Bits64 encoder; 
  | Array_value x ->
    Pbrt.Encoder.nested encode_pb_array_value x encoder;
    Pbrt.Encoder.key 5 Pbrt.Bytes encoder; 
  | Kvlist_value x ->
    Pbrt.Encoder.nested encode_pb_key_value_list x encoder;
    Pbrt.Encoder.key 6 Pbrt.Bytes encoder; 
  | Bytes_value x ->
    Pbrt.Encoder.bytes x encoder;
    Pbrt.Encoder.key 7 Pbrt.Bytes encoder; 
  end

and encode_pb_array_value (v:array_value) encoder = 
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.nested encode_pb_any_value x encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  ) v.values encoder;
  ()

and encode_pb_key_value_list (v:key_value_list) encoder = 
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.nested encode_pb_key_value x encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  ) v.values encoder;
  ()

and encode_pb_key_value (v:key_value) encoder = 
  if key_value_has_key v then (
    Pbrt.Encoder.string v.key encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  );
  begin match v.value with
  | Some x -> 
    Pbrt.Encoder.nested encode_pb_any_value x encoder;
    Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  | None -> ();
  end;
  ()

let rec encode_pb_instrumentation_scope (v:instrumentation_scope) encoder = 
  if instrumentation_scope_has_name v then (
    Pbrt.Encoder.string v.name encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  );
  if instrumentation_scope_has_version v then (
    Pbrt.Encoder.string v.version encoder;
    Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  );
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.nested encode_pb_key_value x encoder;
    Pbrt.Encoder.key 3 Pbrt.Bytes encoder; 
  ) v.attributes encoder;
  if instrumentation_scope_has_dropped_attributes_count v then (
    Pbrt.Encoder.int32_as_varint v.dropped_attributes_count encoder;
    Pbrt.Encoder.key 4 Pbrt.Varint encoder; 
  );
  ()

let rec encode_pb_entity_ref (v:entity_ref) encoder = 
  if entity_ref_has_schema_url v then (
    Pbrt.Encoder.string v.schema_url encoder;
    Pbrt.Encoder.key 1 Pbrt.Bytes encoder; 
  );
  if entity_ref_has_type_ v then (
    Pbrt.Encoder.string v.type_ encoder;
    Pbrt.Encoder.key 2 Pbrt.Bytes encoder; 
  );
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.string x encoder;
    Pbrt.Encoder.key 3 Pbrt.Bytes encoder; 
  ) v.id_keys encoder;
  Pbrt.List_util.rev_iter_with (fun x encoder ->
    Pbrt.Encoder.string x encoder;
    Pbrt.Encoder.key 4 Pbrt.Bytes encoder; 
  ) v.description_keys encoder;
  ()

[@@@ocaml.warning "-23-27-30-39"]

(** {2 Protobuf Decoding} *)

let rec decode_pb_any_value d = 
  let rec loop () = 
    let ret:any_value = match Pbrt.Decoder.key d with
      | None -> Pbrt.Decoder.malformed_variant "any_value"
      | Some (1, _) -> (String_value (Pbrt.Decoder.string d) : any_value) 
      | Some (2, _) -> (Bool_value (Pbrt.Decoder.bool d) : any_value) 
      | Some (3, _) -> (Int_value (Pbrt.Decoder.int64_as_varint d) : any_value) 
      | Some (4, _) -> (Double_value (Pbrt.Decoder.float_as_bits64 d) : any_value) 
      | Some (5, _) -> (Array_value (decode_pb_array_value (Pbrt.Decoder.nested d)) : any_value) 
      | Some (6, _) -> (Kvlist_value (decode_pb_key_value_list (Pbrt.Decoder.nested d)) : any_value) 
      | Some (7, _) -> (Bytes_value (Pbrt.Decoder.bytes d) : any_value) 
      | Some (n, payload_kind) -> (
        Pbrt.Decoder.skip d payload_kind; 
        loop () 
      )
    in
    ret
  in
  loop ()

and decode_pb_array_value d =
  let v = default_array_value () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      (* put lists in the correct order *)
      array_value_set_values v (List.rev v.values);
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      array_value_set_values v ((decode_pb_any_value (Pbrt.Decoder.nested d)) :: v.values);
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "array_value" 1 pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  (v : array_value)

and decode_pb_key_value_list d =
  let v = default_key_value_list () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      (* put lists in the correct order *)
      key_value_list_set_values v (List.rev v.values);
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      key_value_list_set_values v ((decode_pb_key_value (Pbrt.Decoder.nested d)) :: v.values);
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "key_value_list" 1 pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  (v : key_value_list)

and decode_pb_key_value d =
  let v = default_key_value () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      key_value_set_key v (Pbrt.Decoder.string d);
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "key_value" 1 pk
    | Some (2, Pbrt.Bytes) -> begin
      key_value_set_value v (decode_pb_any_value (Pbrt.Decoder.nested d));
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "key_value" 2 pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  (v : key_value)

let rec decode_pb_instrumentation_scope d =
  let v = default_instrumentation_scope () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      (* put lists in the correct order *)
      instrumentation_scope_set_attributes v (List.rev v.attributes);
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      instrumentation_scope_set_name v (Pbrt.Decoder.string d);
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "instrumentation_scope" 1 pk
    | Some (2, Pbrt.Bytes) -> begin
      instrumentation_scope_set_version v (Pbrt.Decoder.string d);
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "instrumentation_scope" 2 pk
    | Some (3, Pbrt.Bytes) -> begin
      instrumentation_scope_set_attributes v ((decode_pb_key_value (Pbrt.Decoder.nested d)) :: v.attributes);
    end
    | Some (3, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "instrumentation_scope" 3 pk
    | Some (4, Pbrt.Varint) -> begin
      instrumentation_scope_set_dropped_attributes_count v (Pbrt.Decoder.int32_as_varint d);
    end
    | Some (4, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "instrumentation_scope" 4 pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  (v : instrumentation_scope)

let rec decode_pb_entity_ref d =
  let v = default_entity_ref () in
  let continue__= ref true in
  while !continue__ do
    match Pbrt.Decoder.key d with
    | None -> (
      (* put lists in the correct order *)
      entity_ref_set_description_keys v (List.rev v.description_keys);
      entity_ref_set_id_keys v (List.rev v.id_keys);
    ); continue__ := false
    | Some (1, Pbrt.Bytes) -> begin
      entity_ref_set_schema_url v (Pbrt.Decoder.string d);
    end
    | Some (1, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "entity_ref" 1 pk
    | Some (2, Pbrt.Bytes) -> begin
      entity_ref_set_type_ v (Pbrt.Decoder.string d);
    end
    | Some (2, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "entity_ref" 2 pk
    | Some (3, Pbrt.Bytes) -> begin
      entity_ref_set_id_keys v ((Pbrt.Decoder.string d) :: v.id_keys);
    end
    | Some (3, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "entity_ref" 3 pk
    | Some (4, Pbrt.Bytes) -> begin
      entity_ref_set_description_keys v ((Pbrt.Decoder.string d) :: v.description_keys);
    end
    | Some (4, pk) -> 
      Pbrt.Decoder.unexpected_payload_message "entity_ref" 4 pk
    | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
  done;
  (v : entity_ref)

[@@@ocaml.warning "-23-27-30-39"]

(** {2 Protobuf YoJson Encoding} *)

let rec encode_json_any_value (v:any_value) = 
  begin match v with
  | String_value v -> `Assoc [("stringValue", Pbrt_yojson.make_string v)]
  | Bool_value v -> `Assoc [("boolValue", Pbrt_yojson.make_bool v)]
  | Int_value v -> `Assoc [("intValue", Pbrt_yojson.make_string (Int64.to_string v))]
  | Double_value v -> `Assoc [("doubleValue", Pbrt_yojson.make_string (string_of_float v))]
  | Array_value v -> `Assoc [("arrayValue", encode_json_array_value v)]
  | Kvlist_value v -> `Assoc [("kvlistValue", encode_json_key_value_list v)]
  | Bytes_value v -> `Assoc [("bytesValue", Pbrt_yojson.make_bytes v)]
  end

and encode_json_array_value (v:array_value) = 
  let assoc = ref [] in
  assoc := (
    let l = v.values |> List.map encode_json_any_value in
    ("values", `List l) :: !assoc 
  );
  `Assoc !assoc

and encode_json_key_value_list (v:key_value_list) = 
  let assoc = ref [] in
  assoc := (
    let l = v.values |> List.map encode_json_key_value in
    ("values", `List l) :: !assoc 
  );
  `Assoc !assoc

and encode_json_key_value (v:key_value) = 
  let assoc = ref [] in
  if key_value_has_key v then (
    assoc := ("key", Pbrt_yojson.make_string v.key) :: !assoc;
  );
  assoc := (match v.value with
    | None -> !assoc
    | Some v -> ("value", encode_json_any_value v) :: !assoc);
  `Assoc !assoc

let rec encode_json_instrumentation_scope (v:instrumentation_scope) = 
  let assoc = ref [] in
  if instrumentation_scope_has_name v then (
    assoc := ("name", Pbrt_yojson.make_string v.name) :: !assoc;
  );
  if instrumentation_scope_has_version v then (
    assoc := ("version", Pbrt_yojson.make_string v.version) :: !assoc;
  );
  assoc := (
    let l = v.attributes |> List.map encode_json_key_value in
    ("attributes", `List l) :: !assoc 
  );
  if instrumentation_scope_has_dropped_attributes_count v then (
    assoc := ("droppedAttributesCount", Pbrt_yojson.make_int (Int32.to_int v.dropped_attributes_count)) :: !assoc;
  );
  `Assoc !assoc

let rec encode_json_entity_ref (v:entity_ref) = 
  let assoc = ref [] in
  if entity_ref_has_schema_url v then (
    assoc := ("schemaUrl", Pbrt_yojson.make_string v.schema_url) :: !assoc;
  );
  if entity_ref_has_type_ v then (
    assoc := ("type", Pbrt_yojson.make_string v.type_) :: !assoc;
  );
  assoc := (
    let l = v.id_keys |> List.map Pbrt_yojson.make_string in
    ("idKeys", `List l) :: !assoc 
  );
  assoc := (
    let l = v.description_keys |> List.map Pbrt_yojson.make_string in
    ("descriptionKeys", `List l) :: !assoc 
  );
  `Assoc !assoc

[@@@ocaml.warning "-23-27-30-39"]

(** {2 JSON Decoding} *)

let rec decode_json_any_value json =
  let assoc = match json with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  let rec loop = function
    | [] -> Pbrt_yojson.E.malformed_variant "any_value"
    | ("stringValue", json_value)::_ -> 
      (String_value (Pbrt_yojson.string json_value "any_value" "String_value") : any_value)
    | ("boolValue", json_value)::_ -> 
      (Bool_value (Pbrt_yojson.bool json_value "any_value" "Bool_value") : any_value)
    | ("intValue", json_value)::_ -> 
      (Int_value (Pbrt_yojson.int64 json_value "any_value" "Int_value") : any_value)
    | ("doubleValue", json_value)::_ -> 
      (Double_value (Pbrt_yojson.float json_value "any_value" "Double_value") : any_value)
    | ("arrayValue", json_value)::_ -> 
      (Array_value ((decode_json_array_value json_value)) : any_value)
    | ("kvlistValue", json_value)::_ -> 
      (Kvlist_value ((decode_json_key_value_list json_value)) : any_value)
    | ("bytesValue", json_value)::_ -> 
      (Bytes_value (Pbrt_yojson.bytes json_value "any_value" "Bytes_value") : any_value)
    
    | _ :: tl -> loop tl
  in
  loop assoc

and decode_json_array_value d =
  let v = default_array_value () in
  let assoc = match d with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  List.iter (function 
    | ("values", `List l) -> begin
      array_value_set_values v @@ List.map (function
        | json_value -> (decode_json_any_value json_value)
      ) l;
    end
    
    | (_, _) -> () (*Unknown fields are ignored*)
  ) assoc;
  ({
    values = v.values;
  } : array_value)

and decode_json_key_value_list d =
  let v = default_key_value_list () in
  let assoc = match d with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  List.iter (function 
    | ("values", `List l) -> begin
      key_value_list_set_values v @@ List.map (function
        | json_value -> (decode_json_key_value json_value)
      ) l;
    end
    
    | (_, _) -> () (*Unknown fields are ignored*)
  ) assoc;
  ({
    values = v.values;
  } : key_value_list)

and decode_json_key_value d =
  let v = default_key_value () in
  let assoc = match d with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  List.iter (function 
    | ("key", json_value) -> 
      key_value_set_key v (Pbrt_yojson.string json_value "key_value" "key")
    | ("value", json_value) -> 
      key_value_set_value v (decode_json_any_value json_value)
    
    | (_, _) -> () (*Unknown fields are ignored*)
  ) assoc;
  ({
    _presence = v._presence;
    key = v.key;
    value = v.value;
  } : key_value)

let rec decode_json_instrumentation_scope d =
  let v = default_instrumentation_scope () in
  let assoc = match d with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  List.iter (function 
    | ("name", json_value) -> 
      instrumentation_scope_set_name v (Pbrt_yojson.string json_value "instrumentation_scope" "name")
    | ("version", json_value) -> 
      instrumentation_scope_set_version v (Pbrt_yojson.string json_value "instrumentation_scope" "version")
    | ("attributes", `List l) -> begin
      instrumentation_scope_set_attributes v @@ List.map (function
        | json_value -> (decode_json_key_value json_value)
      ) l;
    end
    | ("droppedAttributesCount", json_value) -> 
      instrumentation_scope_set_dropped_attributes_count v (Pbrt_yojson.int32 json_value "instrumentation_scope" "dropped_attributes_count")
    
    | (_, _) -> () (*Unknown fields are ignored*)
  ) assoc;
  ({
    _presence = v._presence;
    name = v.name;
    version = v.version;
    attributes = v.attributes;
    dropped_attributes_count = v.dropped_attributes_count;
  } : instrumentation_scope)

let rec decode_json_entity_ref d =
  let v = default_entity_ref () in
  let assoc = match d with
    | `Assoc assoc -> assoc
    | _ -> assert(false)
  in
  List.iter (function 
    | ("schemaUrl", json_value) -> 
      entity_ref_set_schema_url v (Pbrt_yojson.string json_value "entity_ref" "schema_url")
    | ("type", json_value) -> 
      entity_ref_set_type_ v (Pbrt_yojson.string json_value "entity_ref" "type_")
    | ("idKeys", `List l) -> begin
      entity_ref_set_id_keys v @@ List.map (function
        | json_value -> Pbrt_yojson.string json_value "entity_ref" "id_keys"
      ) l;
    end
    | ("descriptionKeys", `List l) -> begin
      entity_ref_set_description_keys v @@ List.map (function
        | json_value -> Pbrt_yojson.string json_value "entity_ref" "description_keys"
      ) l;
    end
    
    | (_, _) -> () (*Unknown fields are ignored*)
  ) assoc;
  ({
    _presence = v._presence;
    schema_url = v.schema_url;
    type_ = v.type_;
    id_keys = v.id_keys;
    description_keys = v.description_keys;
  } : entity_ref)