Skip to content

Commit

Permalink
Merge pull request #202 from mransan/wip-refactor-2023-08-17
Browse files Browse the repository at this point in the history
some refactors
  • Loading branch information
c-cube authored Aug 23, 2023
2 parents 9c9276c + d476ab5 commit aacb470
Show file tree
Hide file tree
Showing 22 changed files with 98 additions and 133 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*.sw*
_build

ocaml-protoc
ocaml_protoc.byte
ocaml_protoc.native

Expand Down
25 changes: 0 additions & 25 deletions .merlin

This file was deleted.

2 changes: 1 addition & 1 deletion benchs/benchs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ module Nested = struct
*)

let int64_as_varint = varint
let int_as_varint i e = (varint [@inlined]) (Int64.of_int i) e
let int_as_varint i e = varint (Int64.of_int i) e

let[@inline] key k pk f e =
let pk' =
Expand Down
33 changes: 0 additions & 33 deletions src/compilerlib/compilerlib.odocl

This file was deleted.

1 change: 1 addition & 0 deletions src/compilerlib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
(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")
(wrapped true)
(modules pb_codegen_backend pb_codegen_decode_binary pb_codegen_decode_bs
pb_codegen_decode_yojson pb_codegen_default pb_codegen_encode_binary
Expand Down
12 changes: 6 additions & 6 deletions src/compilerlib/pb_codegen_backend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -209,31 +209,31 @@ let compile_field_type ~unsigned_tag all_types file_options field_options
file_name field_type =
let ocaml_type =
match Pb_option.get field_options "ocaml_type" with
| Some (Pb_option.Constant_litteral "int_t") -> `Int_t
| Some (Pb_option.Constant_literal "int_t") -> `Int_t
| _ -> `None
in

let int32_type =
match Pb_option.get file_options "int32_type" with
| Some (Pb_option.Constant_litteral "int_t") -> Ot.(Ft_basic_type Bt_int)
| Some (Pb_option.Constant_literal "int_t") -> Ot.(Ft_basic_type Bt_int)
| _ -> Ot.(Ft_basic_type Bt_int32)
in

let uint32_type =
match Pb_option.get file_options "int32_type" with
| Some (Pb_option.Constant_litteral "int_t") -> Ot.(Ft_basic_type Bt_int)
| Some (Pb_option.Constant_literal "int_t") -> Ot.(Ft_basic_type Bt_int)
| _ -> Ot.(Ft_basic_type Bt_uint32)
in

let int64_type =
match Pb_option.get file_options "int64_type" with
| Some (Pb_option.Constant_litteral "int_t") -> Ot.(Ft_basic_type Bt_int)
| Some (Pb_option.Constant_literal "int_t") -> Ot.(Ft_basic_type Bt_int)
| _ -> Ot.(Ft_basic_type Bt_int64)
in

let uint64_type =
match Pb_option.get file_options "int64_type" with
| Some (Pb_option.Constant_litteral "int_t") -> Ot.(Ft_basic_type Bt_int)
| Some (Pb_option.Constant_literal "int_t") -> Ot.(Ft_basic_type Bt_int)
| _ -> Ot.(Ft_basic_type Bt_uint64)
in

Expand Down Expand Up @@ -292,7 +292,7 @@ let is_mutable ?field_name field_options =
let ocaml_container field_options =
match Pb_option.get field_options "ocaml_container" with
| None -> None
| Some (Pb_option.Constant_litteral container_name) -> Some container_name
| Some (Pb_option.Constant_literal container_name) -> Some container_name
| Some _ -> None

let variant_of_oneof ?include_oneof_name ~outer_message_names ~unsigned_tag
Expand Down
2 changes: 2 additions & 0 deletions src/compilerlib/pb_codegen_sig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ module type S = sig
Pb_codegen_ocaml_type.type_ ->
Pb_codegen_formatting.scope ->
bool
(** Generate a signature file (.mli) *)

val gen_struct :
?and_:unit ->
Pb_codegen_ocaml_type.type_ ->
Pb_codegen_formatting.scope ->
bool
(** Generate the implementation (.ml) *)

val ocamldoc_title : string
val file_suffix : string
Expand Down
1 change: 0 additions & 1 deletion src/compilerlib/pb_codegen_util.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Ot = Pb_codegen_ocaml_type

(** [sp x] same as sprintf but prefixed with new line *)
let sp x = Printf.sprintf x

let let_decl_of_and = function
Expand Down
12 changes: 6 additions & 6 deletions src/compilerlib/pb_logger.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@
*)

val setup_from_out_channel : out_channel -> unit
(** [setup_from_out_channel oc] will configure the logger to print all
logging statement to [oc].
(** [setup_from_out_channel oc] will configure the logger to print all
logging statement to [oc].
Function can be called multiple times, each call will disable (override) the
Function can be called multiple times, each call will disable (override) the
previously setup out channel
*)

val log : ('a, out_channel, unit) format -> 'a
(** [log format x y ] same as [Printf.printf] except that the message will
(** [log format x y ] same as [Printf.printf] except that the message will
get printed to the previously setup [out_channel] in the
[setup_from_out_channel] function.
*)

val endline : string -> unit
(** [endline format x y ] same as [Pervasives.print_endline] except that the message will
(** [endline str] same as [Stdlib.print_endline str] except that the message will
get printed to the previously setup [out_channel] in the
[setup_from_out_channel] function.
[setup_from_out_channel] function.
*)
2 changes: 1 addition & 1 deletion src/compilerlib/pb_option.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type constant =
| Constant_bool of bool
| Constant_int of int
| Constant_float of float
| Constant_litteral of string
| Constant_literal of string

type option_name = string
type t = option_name * constant
Expand Down
6 changes: 3 additions & 3 deletions src/compilerlib/pb_option.mli
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(** Protobuf File/Message/Field options *)

(** Protobuf constance
(** Protobuf constant
As defined in: {{:https://goo.gl/FzpQY2} Protobuf Language Spec}. *)
type constant =
| Constant_string of string
| Constant_bool of bool
| Constant_int of int
| Constant_float of float
| Constant_litteral of string
| Constant_literal of string

type option_name = string
(** Option identifier *)
Expand Down
2 changes: 1 addition & 1 deletion src/compilerlib/pb_parsing_parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ constant :
match (snd $1) with
| "true" -> Pb_option.Constant_bool true
| "false" -> Pb_option.Constant_bool false
| litteral -> Pb_option.Constant_litteral litteral
| str -> Pb_option.Constant_literal str
}
| T_string { Pb_option.Constant_string $1 };

Expand Down
2 changes: 1 addition & 1 deletion src/compilerlib/pb_typing_resolution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ let resolve_builtin_type_field_default field_name builtin_type field_default =
let resolve_enum_field_default field_name type_ field_default =
match field_default with
| None -> None
| Some (Pb_option.Constant_litteral default_enum_value as constant) ->
| Some (Pb_option.Constant_literal default_enum_value as constant) ->
let { Tt.spec; _ } = type_ in
(match spec with
| Tt.Message _ ->
Expand Down
9 changes: 9 additions & 0 deletions src/compilerlib/pb_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ let read_file file_name =
close_in ic;
Bytes.sub_string b 0 !offset

let protect ~finally f =
try
let x = f () in
finally ();
x
with e ->
finally ();
raise e

module List = struct
let rec pop_last = function
| [] -> failwith "Invalid argument [] for pop_last"
Expand Down
1 change: 1 addition & 0 deletions src/compilerlib/pb_util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ end
(* Option *)

val read_file : string -> string
val protect : finally:(unit -> unit) -> (unit -> 'a) -> 'a

module List : sig
val pop_last : 'a list -> 'a list
Expand Down
1 change: 1 addition & 0 deletions src/include/ocaml-protoc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocamloptions.pb.*
72 changes: 37 additions & 35 deletions src/ocaml-protoc/ocaml_protoc_cmdline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
*)

(* [ocaml-protoc] provides the ability to override all the custom
*
* protobuf file options defined in
* src/include/ocaml-protoc/ocamloptions.proto as command line arguments.
*
* This module implements the bridge functionality between the 2
* mechanism; command line options are converted to file options and
* appended there.
(** [ocaml-protoc] provides the ability to override all the custom
protobuf file options defined in
[src/include/ocaml-protoc/ocamloptions.proto] as command line arguments.
This module implements the bridge functionality between the 2
mechanism; command line options are converted to file options and
appended there.
*)
module File_options = struct
type t = {
Expand All @@ -39,8 +39,10 @@ module File_options = struct
mutable ocaml_file_ppx: string option;
mutable ocaml_all_types_ppx: string option;
}
(* all file options supported... this needs to be kept in sync with
* src/include/ocaml-protoc/ocamloptions.proto *)
(** all file options supported. *)

(* NOTE: this needs to be kept in sync with
src/include/ocaml-protoc/ocamloptions.proto *)

let make () =
{
Expand All @@ -50,27 +52,27 @@ module File_options = struct
ocaml_all_types_ppx = None;
}

(* Compute the command line arguments for be used with the Arg module. *)
(** Compute the command line arguments for be used with the Arg module. *)
let cmd_line_args t =
[
( "-int32_type",
Arg.String
(function
| "int_t" -> t.int32_type <- Some "int_t"
| x -> failwith @@ Printf.sprintf "Invalid int32_type value %s" x),
"int32_type file option" );
" int32_type file option" );
( "-int64_type",
Arg.String
(function
| "int_t" -> t.int64_type <- Some "int_t"
| x -> failwith @@ Printf.sprintf "Invalid int64_type value %s" x),
"int64_type file option" );
" int64_type file option" );
( "-ocaml_file_ppx",
Arg.String (fun s -> t.ocaml_file_ppx <- Some s),
"ocaml_file_ppx file option" );
" ocaml_file_ppx file option" );
( "-ocaml_all_types_ppx",
Arg.String (fun s -> t.ocaml_all_types_ppx <- Some s),
"ocaml_all_types_ppx file option" );
" ocaml_all_types_ppx file option" );
]

(** Converts the command line values to Parse Tree file options
Expand All @@ -86,30 +88,30 @@ module File_options = struct
Pb_option.add options option_name option_value
in
Pb_option.empty
|> map int32_type (fun s -> "int32_type", Pb_option.Constant_litteral s)
|> map int64_type (fun s -> "int64_type", Pb_option.Constant_litteral s)
|> map int32_type (fun s -> "int32_type", Pb_option.Constant_literal s)
|> map int64_type (fun s -> "int64_type", Pb_option.Constant_literal s)
|> map ocaml_file_ppx (fun s ->
"ocaml_file_ppx", Pb_option.Constant_string s)
|> map ocaml_all_types_ppx (fun s ->
"ocaml_all_types_ppx", Pb_option.Constant_string s)
end

(* Command line argument for the ocaml-protoc *)
(** Command line argument for the ocaml-protoc *)
module Cmdline = struct
type t = {
mutable ml_out: string; (* output directory *)
mutable ml_out: string; (** output directory *)
mutable proto_file_name: string;
(* proto file name as given on the cmd line *)
(** proto file name as given on the cmd line *)
mutable include_dirs: string list;
(* include directories given with -I argument *)
binary: bool ref; (* whether binary encoding is enabled *)
yojson: bool ref; (* whether yojson encoding is enabled *)
bs: bool ref; (* whether BuckleScript encoding is enabled *)
pp: bool ref; (* whether pretty printing is enabled *)
(** include directories given with -I argument *)
binary: bool ref; (** whether binary encoding is enabled *)
yojson: bool ref; (** whether yojson encoding is enabled *)
bs: bool ref; (** whether BuckleScript encoding is enabled *)
pp: bool ref; (** whether pretty printing is enabled *)
mutable cmd_line_file_options: File_options.t;
(* file options override from the cmd line *)
(** file options override from the cmd line *)
unsigned_tag: bool ref;
(* if true, unsigned int32/64s will be generated with a polymorphic
(** if true, unsigned int32/64s will be generated with a polymorphic
variant [`unsigned int32/64], otherwise will be emitted as
immediate [int32/int64]. *)
}
Expand All @@ -129,17 +131,17 @@ module Cmdline = struct

let cmd_line_args t =
[
"-yojson", Arg.Set t.yojson, "generate yojson encoding";
"-bs", Arg.Set t.bs, "generate BuckleScript encoding";
"-binary", Arg.Set t.binary, "generate binary encoding";
"-pp", Arg.Set t.pp, "generate pretty print functions";
"-yojson", Arg.Set t.yojson, " generate yojson encoding";
"-bs", Arg.Set t.bs, " generate BuckleScript encoding";
"-binary", Arg.Set t.binary, " generate binary encoding";
"-pp", Arg.Set t.pp, " generate pretty print functions";
( "-I",
Arg.String (fun s -> t.include_dirs <- s :: t.include_dirs),
"include directories" );
"-ml_out", Arg.String (fun s -> t.ml_out <- s), "output directory";
" include directories" );
"-ml_out", Arg.String (fun s -> t.ml_out <- s), " output directory";
( "-unsigned",
Arg.Set t.unsigned_tag,
"tag uint32 and uint64 types with `unsigned" );
" tag uint32 and uint64 types with `unsigned" );
]
@ File_options.cmd_line_args t.cmd_line_file_options

Expand All @@ -162,7 +164,7 @@ module Cmdline = struct
let parse () =
let args = make () in
let anon_fun = anon_fun args in
let cmd_line_args = cmd_line_args args in
let cmd_line_args = cmd_line_args args |> Arg.align in
Arg.parse cmd_line_args anon_fun usage;
validate args;
args
Expand Down
Loading

0 comments on commit aacb470

Please sign in to comment.