package ocaml-protoc

  1. Overview
  2. Docs
Protobuf compiler for OCaml

Install

Dune Dependency

Authors

Maintainers

Sources

2.4.0.tar.gz
md5=8a294e86c6202b8ec8016e71d19264cb
sha512=67020bef50b59c6590c1b25d85a75d6e19d6cd37d42b87c94aef798bff51a45f38fe7024b4c67d71c22c13d3f2776bec83acd77794a518f1c4a7eddfc30b6d0b

doc/src/ocaml-protoc.compiler-lib/pb_codegen_types.ml.html

Source file pb_codegen_types.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
module Ot = Pb_codegen_ocaml_type
module F = Pb_codegen_formatting
open Pb_codegen_util

let type_decl_of_and = function
  | Some () -> "and"
  | None -> "type"

let is_imperative_type = function
  (*TODO Rename *)
  | Ot.Rft_nolabel _ | Ot.Rft_required _ | Ot.Rft_optional _ | Ot.Rft_variant _
  | Ot.Rft_repeated (Ot.Rt_list, _, _, _, _)
  | Ot.Rft_associative (Ot.At_list, _, _, _) ->
    false
  | Ot.Rft_repeated (Ot.Rt_repeated_field, _, _, _, _)
  | Ot.Rft_associative (Ot.At_hashtable, _, _, _) ->
    true

let gen_record_mutable module_prefix { Ot.r_name; r_fields } sc =
  let field_prefix field_type =
    if is_imperative_type field_type then
      ""
    else
      "mutable "
  in

  let r_name = Pb_codegen_util.mutable_record_name r_name in

  F.linep sc "type %s = {" r_name;
  F.scope sc (fun sc ->
      List.iter
        (fun { Ot.rf_label; rf_field_type; _ } ->
          let prefix = field_prefix rf_field_type in
          let type_ =
            Pb_codegen_util.string_of_record_field_type ~module_prefix
              rf_field_type
          in
          F.linep sc "%s%s : %s;" prefix rf_label type_)
        r_fields);
  F.line sc "}"

let gen_record ?and_ { Ot.r_name; r_fields } sc =
  let field_prefix field_mutable =
    if field_mutable then
      "mutable "
    else
      ""
  in

  F.linep sc "%s %s = {" (type_decl_of_and and_) r_name;
  F.scope sc (fun sc ->
      List.iter
        (fun { Ot.rf_label; rf_field_type; rf_mutable } ->
          let prefix = field_prefix rf_mutable in
          let type_ =
            Pb_codegen_util.string_of_record_field_type rf_field_type
          in
          F.linep sc "%s%s : %s;" prefix rf_label type_)
        r_fields);
  F.line sc "}"

let gen_variant ?and_ variant sc =
  let { Ot.v_name; v_constructors } = variant in

  F.linep sc "%s %s =" (type_decl_of_and and_) v_name;

  F.scope sc (fun sc ->
      List.iter
        (fun { Ot.vc_constructor; vc_field_type; _ } ->
          match vc_field_type with
          | Ot.Vct_nullary -> F.linep sc "| %s" vc_constructor
          | Ot.Vct_non_nullary_constructor field_type ->
            let type_string = string_of_field_type field_type in
            F.linep sc "| %s of %s" vc_constructor type_string)
        v_constructors)

let gen_const_variant ?and_ { Ot.cv_name; cv_constructors } sc =
  F.linep sc "%s %s =" (type_decl_of_and and_) cv_name;
  F.scope sc (fun sc ->
      List.iter
        (fun { Ot.cvc_name; _ } -> F.linep sc "| %s " cvc_name)
        cv_constructors)

let print_ppx_extension { Ot.type_level_ppx_extension; _ } sc =
  match type_level_ppx_extension with
  | None -> ()
  | Some ppx_content -> F.linep sc "[@@%s]" ppx_content

let gen_struct ?and_ t scope =
  let { Ot.spec; _ } = t in
  (match spec with
  | Ot.Record r -> gen_record ?and_ r scope
  | Ot.Variant v -> gen_variant ?and_ v scope
  | Ot.Const_variant v -> gen_const_variant ?and_ v scope);
  print_ppx_extension t scope;
  true

let gen_sig ?and_ t scope =
  let { Ot.spec; _ } = t in
  (match spec with
  | Ot.Record r -> gen_record ?and_ r scope
  | Ot.Variant v -> gen_variant ?and_ v scope
  | Ot.Const_variant v -> gen_const_variant ?and_ v scope);
  print_ppx_extension t scope;
  true

let ocamldoc_title = "Types"
let file_suffix = "types"
OCaml

Innovation. Community. Security.