Skip to content

Commit

Permalink
Merge pull request #234 from Lupus/expose-pb-options-to-ocaml-types
Browse files Browse the repository at this point in the history
plugin to dump a representation of the parsed proto file in comments
  • Loading branch information
c-cube authored Jan 17, 2024
2 parents 67870ed + 1b1af6f commit 55fc03b
Show file tree
Hide file tree
Showing 18 changed files with 514 additions and 25 deletions.
22 changes: 12 additions & 10 deletions src/compilerlib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
(library
(name ocaml_protoc_compiler_lib)
(public_name ocaml-protoc.compiler-lib)
(synopsis "Compiler library for ocaml-protoc, to turn .proto files into OCaml code")
(synopsis
"Compiler library for ocaml-protoc, to turn .proto files into OCaml code")
(wrapped true)
(modules pb_codegen_all pb_codegen_backend pb_codegen_decode_binary pb_codegen_decode_bs
pb_codegen_decode_yojson pb_codegen_default pb_codegen_make pb_codegen_encode_binary
pb_codegen_encode_bs pb_codegen_encode_yojson pb_codegen_formatting
pb_codegen_ocaml_type pb_codegen_pp pb_codegen_plugin pb_codegen_types pb_codegen_services
pb_codegen_util pb_exception pb_field_type pb_location pb_logger pb_option
pb_parsing pb_parsing_lexer pb_parsing_parser pb_parsing_parse_tree
pb_parsing_util pb_typing_graph pb_typing pb_typing_recursion
pb_typing_resolution pb_typing_type_tree pb_typing_util
pb_typing_validation pb_util pb_format_util)
(modules pb_codegen_all pb_codegen_backend pb_codegen_decode_binary
pb_codegen_decode_bs pb_codegen_decode_yojson pb_codegen_default
pb_codegen_make pb_codegen_encode_binary pb_codegen_encode_bs
pb_codegen_encode_yojson pb_codegen_formatting pb_codegen_ocaml_type_dump
pb_codegen_ocaml_type pb_codegen_pp pb_codegen_plugin pb_codegen_types
pb_codegen_services pb_codegen_util pb_exception pb_field_type pb_location
pb_logger pb_option pb_parsing pb_parsing_lexer pb_parsing_parser
pb_parsing_parse_tree pb_parsing_util pb_typing_graph pb_typing
pb_typing_recursion pb_typing_resolution pb_typing_type_tree
pb_typing_util pb_typing_validation pb_util pb_format_util)
(libraries stdlib-shims))
37 changes: 31 additions & 6 deletions src/compilerlib/pb_codegen_backend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ let compile_message ~(unsigned_tag : bool) (file_options : Pb_option.set)
(* TODO maybe module_ should be resolved before `compile_message` since
it is common with compile_enum
*)
let { Tt.message_name; Tt.message_body; _ } = message in
let { Tt.message_name; Tt.message_body; Tt.message_options; _ } = message in

let { Tt.message_names; _ } = scope in

Expand All @@ -403,7 +403,12 @@ let compile_message ~(unsigned_tag : bool) (file_options : Pb_option.set)

let type_ =
Ot.
{ module_prefix; spec = Ot.Unit empty_record; type_level_ppx_extension }
{
module_prefix;
spec = Ot.Unit empty_record;
type_level_ppx_extension;
type_options = message_options;
}
in
[ type_ ]
| Tt.Message_oneof_field f :: [] ->
Expand All @@ -412,7 +417,15 @@ let compile_message ~(unsigned_tag : bool) (file_options : Pb_option.set)
variant_of_oneof ~unsigned_tag ~outer_message_names ~all_types
file_options file_name f
in
[ Ot.{ module_prefix; spec = Variant variant; type_level_ppx_extension } ]
[
Ot.
{
module_prefix;
spec = Variant variant;
type_level_ppx_extension;
type_options = message_options;
};
]
| _ ->
let variants, fields =
List.fold_left
Expand Down Expand Up @@ -485,6 +498,7 @@ let compile_message ~(unsigned_tag : bool) (file_options : Pb_option.set)
rf_label = label_name_of_field_name field_name;
rf_field_type = record_field_type;
rf_mutable = mutable_;
rf_options = field.field_options;
}
in

Expand All @@ -508,6 +522,7 @@ let compile_message ~(unsigned_tag : bool) (file_options : Pb_option.set)
* parser all the way down to here.
*)
rf_field_type = Rft_variant variant;
rf_options = field.oneof_options;
}
in

Expand All @@ -518,6 +533,7 @@ let compile_message ~(unsigned_tag : bool) (file_options : Pb_option.set)
module_prefix;
spec = Variant variant;
type_level_ppx_extension;
type_options = Pb_option.empty;
}
in
t :: variants
Expand Down Expand Up @@ -583,6 +599,7 @@ let compile_message ~(unsigned_tag : bool) (file_options : Pb_option.set)
rf_label = label_name_of_field_name map_name;
rf_field_type = record_field_type;
rf_mutable = is_mutable ~field_name:map_name map_options;
rf_options = map_options;
}
in

Expand All @@ -601,23 +618,30 @@ let compile_message ~(unsigned_tag : bool) (file_options : Pb_option.set)
in

let type_ =
Ot.{ module_prefix; spec = Record record; type_level_ppx_extension }
Ot.
{
module_prefix;
spec = Record record;
type_level_ppx_extension;
type_options = message_options;
}
in

List.rev (type_ :: variants)

let compile_enum file_options file_name scope enum =
let { Tt.enum_name; enum_values; _ } = enum in
let { Tt.enum_name; enum_values; enum_options; _ } = enum in
let module_prefix = module_prefix_of_file_name file_name in
let { Tt.message_names; Tt.packages = _ } = scope in

let cv_constructors =
List.map
(fun { Tt.enum_value_name; Tt.enum_value_int } ->
(fun { Tt.enum_value_name; Tt.enum_value_int; Tt.enum_value_options } ->
{
Ot.cvc_name = constructor_name enum_value_name;
Ot.cvc_binary_value = enum_value_int;
Ot.cvc_string_value = enum_value_name;
Ot.cvc_options = enum_value_options;
})
enum_values
in
Expand All @@ -635,6 +659,7 @@ let compile_enum file_options file_name scope enum =
Const_variant
{ cv_name = type_name message_names enum_name; cv_constructors };
type_level_ppx_extension;
type_options = enum_options;
}

let compile_rpc ~(file_name : string) ~all_types
Expand Down
3 changes: 3 additions & 0 deletions src/compilerlib/pb_codegen_ocaml_type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ and record_field = {
rf_label: string;
rf_field_type: record_field_type;
rf_mutable: bool;
rf_options: Pb_option.set;
}

and record = {
Expand All @@ -132,6 +133,7 @@ and const_variant_constructor = {
cvc_name: string;
cvc_binary_value: int;
cvc_string_value: string;
cvc_options: Pb_option.set;
}

and const_variant = {
Expand All @@ -154,6 +156,7 @@ type type_ = {
generated module and it is based on the `.proto` filename. *)
spec: type_spec;
type_level_ppx_extension: string option;
type_options: Pb_option.set;
}

(** RPC argument or return type. We require message types in RPC. *)
Expand Down
Loading

0 comments on commit 55fc03b

Please sign in to comment.