package ocaml-protoc

  1. Overview
  2. Docs
Pure OCaml compiler for .proto files

Install

dune-project
 Dependency

Authors

Maintainers

Sources

ocaml-protoc-4.0.tbz
sha256=88533848ee8ad662bfb063f34932286405977fa3810515b997119b06f05105e4
sha512=df12c71f7181eafc94cd0fc400edf7f258cdd3740a5badafce097f771b7828fed9a9a9c0a457e7e118848a8b1ddd87fe3134a5bdf88d4adcb0d0e04ba6808c5f

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

Source file pb_codegen_all.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
(*
  The MIT License (MIT)

  Copyright (c) 2016 Maxime Ransan <maxime.ransan@gmail.com>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.

*)

module E = Pb_exception
module Ot = Pb_codegen_ocaml_type
module F = Pb_codegen_formatting
module Plugin = Pb_codegen_plugin

type codegen_f = Plugin.codegen_f
type codegen_service_f = Ot.service -> F.scope -> unit

type ocaml_mod = {
  ml: F.scope;
  mli: F.scope;
}

let new_ocaml_mod ~proto_file_options ~proto_file_name () : ocaml_mod =
  let self = { ml = F.empty_scope (); mli = F.empty_scope () } in

  let print_ppx sc =
    match Pb_raw_option.get_ext proto_file_options "ocaml_file_ppx" with
    | None -> ()
    | Some Pb_option.(Scalar_value (Constant_string s)) ->
      F.linep sc "[@@@%s]" s
    | _ -> E.invalid_ppx_extension_option proto_file_name
  in

  (* write preludes *)
  F.line self.ml "[@@@ocaml.warning \"-23-27-30-39-44\"]";
  F.empty_line self.ml;
  print_ppx self.ml;
  F.empty_line self.mli;

  F.linep self.mli "(** Code for %s *)" (Filename.basename proto_file_name);
  F.empty_line self.mli;
  F.linep self.mli "(* generated from %S, do not edit *)" proto_file_name;
  F.empty_line self.mli;
  print_ppx self.mli;
  F.empty_line self.mli;

  self

let generate_for_all_types (ocaml_types : Ot.type_ list list)
    ~(mode : Pb_codegen_mode.t) (sc : F.scope) (f : codegen_f) ocamldoc_title :
    unit =
  (match ocamldoc_title with
  | None -> ()
  | Some ocamldoc_title ->
    F.empty_line sc;
    F.linep sc "(** {2 %s} *)" ocamldoc_title;
    F.empty_line sc);

  List.iter
    (fun types ->
      let (_ : bool) =
        List.fold_left
          (fun first type_ ->
            let has_encoded =
              if first then
                f ~mode type_ sc
              else
                f ~and_:() ~mode type_ sc
            in
            if has_encoded then F.empty_line sc;
            first && not has_encoded)
          true types
      in
      ())
    ocaml_types

let generate_for_all_services (services : Ot.service list) (sc : F.scope)
    (f : codegen_service_f) ocamldoc_title : unit =
  (match ocamldoc_title with
  | None -> ()
  | Some ocamldoc_title ->
    F.empty_line sc;
    F.linep sc "(** {2 %s} *)" ocamldoc_title;
    F.empty_line sc);

  List.iter (fun (service : Ot.service) -> f service sc) services

let generate_type (self : ocaml_mod) ~(mode : Pb_codegen_mode.t) ocaml_types :
    unit =
  generate_for_all_types ocaml_types self.ml ~mode Pb_codegen_types.gen_struct
    None;
  generate_for_all_types ocaml_types self.ml ~mode Pb_codegen_default.gen_struct
    None;
  generate_for_all_types ocaml_types self.mli ~mode Pb_codegen_types.gen_sig
    (Some Pb_codegen_types.ocamldoc_title);
  generate_for_all_types ocaml_types self.mli ~mode Pb_codegen_default.gen_sig
    (Some Pb_codegen_default.ocamldoc_title);
  ()

let generate_make_and_accessors (self : ocaml_mod) ~mode ocaml_types : unit =
  generate_for_all_types ocaml_types self.ml ~mode Pb_codegen_make.gen_struct
    (Some Pb_codegen_make.ocamldoc_title);
  generate_for_all_types ocaml_types self.mli ~mode Pb_codegen_make.gen_sig
    (Some Pb_codegen_make.ocamldoc_title)

let generate_service_struct (service : Ot.service) sc : unit =
  Pb_logger.log "Generating code for service %s\n" service.service_name;
  Pb_codegen_services.gen_service_struct service sc

let generate_service_sig service sc : unit =
  Pb_codegen_services.gen_service_sig service sc

let generate_services (self : ocaml_mod) services : unit =
  generate_for_all_services services self.ml generate_service_struct None;
  generate_for_all_services services self.mli generate_service_sig
    (Some "Services")

let generate_plugin (self : ocaml_mod) ~(mode : Pb_codegen_mode.t) ocaml_types
    (p : Plugin.t) : unit =
  let (module P) = p in
  F.line self.ml "[@@@ocaml.warning \"-23-27-30-39\"]";
  generate_for_all_types ocaml_types ~mode self.ml P.gen_struct
    (Some P.ocamldoc_title);
  generate_for_all_types ocaml_types ~mode self.mli P.gen_sig
    (Some P.ocamldoc_title);
  ()

let codegen (proto : Ot.proto) ~(mode : Pb_codegen_mode.t) ~proto_file_options
    ~proto_file_name ~services (plugins : Plugin.t list) : ocaml_mod =
  let self = new_ocaml_mod ~proto_file_options ~proto_file_name () in
  generate_type self ~mode proto.proto_types;
  generate_make_and_accessors self ~mode proto.proto_types;
  List.iter (generate_plugin self ~mode proto.proto_types) plugins;

  (* services come last, they need binary and json *)
  if services then generate_services self proto.proto_services;
  self