Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format check in ci #237

Merged
merged 3 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ jobs:
#- macos-latest
#- windows-latest
ocaml-compiler:
- 4.08.x
- 4.12.x
- '4.08'
- '4.14'
- '5.1'
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand All @@ -24,10 +25,15 @@ jobs:
- uses: ocaml/setup-ocaml@v2
with:
ocaml-compiler: ${{ matrix.ocaml-compiler }}
allow-prerelease-opam: true
dune-cache: true
#- run: sudo apt install protobuf-compiler libprotobuf-dev
- run: opam pin -n .
- run: opam depext -yt ocaml-protoc
- run: opam install -t . --deps-only
- run: opam exec -- dune build @install
- run: opam exec -- dune runtest
#- run: opam exec -- make integration
- run: opam install ocamlformat.0.24.1
- run: opam exec -- dune build @fmt --auto-promote
- run: git diff -q
1 change: 0 additions & 1 deletion benchs/bin/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

(executable
(name run)
(ocamlopt_flags :standard -inline 100)
Expand Down
5 changes: 4 additions & 1 deletion benchs/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
(executable
(name benchs)
(ocamlopt_flags :standard -inline 100)
(foreign_stubs (language c) (flags :standard -std=c99 -O2) (names stubs))
(foreign_stubs
(language c)
(flags :standard -std=c99 -O2)
(names stubs))
(libraries ocaml-protoc benchmark))

(rule
Expand Down
1 change: 0 additions & 1 deletion src/compilerlib/pb_codegen_decode_bs.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

include Pb_codegen_plugin.S

val plugin : Pb_codegen_plugin.t
82 changes: 41 additions & 41 deletions src/compilerlib/pb_codegen_decode_yojson.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let field_pattern_match ~r_name ~rf_label field_type =
| Ot.Bt_bool -> decode "bool"
| Ot.Bt_bytes -> decode "bytes"
in
("json_value", exp)
"json_value", exp
| Ot.Ft_unit ->
"json_value", sp "Pbrt_yojson.unit json_value \"%s\" \"%s\"" r_name rf_label
(* TODO Wrapper: add similar one for wrapper type (with different
Expand Down Expand Up @@ -103,44 +103,42 @@ let gen_rft_assoc_field sc ~r_name ~rf_label ~assoc_type ~key_type ~value_type =
let json_label = Pb_codegen_util.camel_case_of_label rf_label in
F.linep sc "| (\"%s\", `Assoc assoc) ->" json_label;
F.sub_scope sc (fun sc ->
let value_name, value_exp = field_pattern_match ~r_name ~rf_label value_type in
let key_name = "key" in
let key_exp =
match key_type with
| Ot.Bt_string -> "key"
| Ot.Bt_int -> "(Int.of_string key)"
| Ot.Bt_int32 -> "(Int32.of_string key)"
| Ot.Bt_int64 -> "(Int64.of_string key)"
| Ot.Bt_uint32 -> "(`unsigned (Int32.of_string key))"
| Ot.Bt_uint64 -> "(`unsigned (Int64.of_string key))"
| Ot.Bt_bool -> "(Bool.of_string key)"
| Ot.Bt_float ->
Printf.eprintf "float cannot be used as a map key type";
exit 1
| Ot.Bt_bytes ->
Printf.eprintf "bytes cannot be used as a map key type";
exit 1
in
F.line sc "let assoc =";
F.sub_scope sc (fun sc ->
F.line sc "assoc";
F.linep sc "|> List.map (fun (%s, %s) -> (%s, %s)) "
key_name
value_name
key_exp
value_exp;
F.line sc "|> List.to_seq";
(* Passing through [Hashtbl.of_seq] even in the [At_list] case ensures that if there
is a repeated key we take the last value associated with it. *)
F.line sc "|> Hashtbl.of_seq");
F.line sc "in";
let assoc_exp =
match assoc_type with
| Ot.At_hashtable -> "assoc"
| Ot.At_list -> "assoc |> Hashtbl.to_seq |> List.of_seq"
in
F.linep sc "v.%s <- %s" rf_label assoc_exp);
;;
let value_name, value_exp =
field_pattern_match ~r_name ~rf_label value_type
in
let key_name = "key" in
let key_exp =
match key_type with
| Ot.Bt_string -> "key"
| Ot.Bt_int -> "(Int.of_string key)"
| Ot.Bt_int32 -> "(Int32.of_string key)"
| Ot.Bt_int64 -> "(Int64.of_string key)"
| Ot.Bt_uint32 -> "(`unsigned (Int32.of_string key))"
| Ot.Bt_uint64 -> "(`unsigned (Int64.of_string key))"
| Ot.Bt_bool -> "(Bool.of_string key)"
| Ot.Bt_float ->
Printf.eprintf "float cannot be used as a map key type";
exit 1
| Ot.Bt_bytes ->
Printf.eprintf "bytes cannot be used as a map key type";
exit 1
in
F.line sc "let assoc =";
F.sub_scope sc (fun sc ->
F.line sc "assoc";
F.linep sc "|> List.map (fun (%s, %s) -> (%s, %s)) " key_name
value_name key_exp value_exp;
F.line sc "|> List.to_seq";
(* Passing through [Hashtbl.of_seq] even in the [At_list] case ensures that if there
is a repeated key we take the last value associated with it. *)
F.line sc "|> Hashtbl.of_seq");
F.line sc "in";
let assoc_exp =
match assoc_type with
| Ot.At_hashtable -> "assoc"
| Ot.At_list -> "assoc |> Hashtbl.to_seq |> List.of_seq"
in
F.linep sc "v.%s <- %s" rf_label assoc_exp)

(* Generate decode function for a record *)
let gen_record ?and_ { Ot.r_name; r_fields } sc =
Expand Down Expand Up @@ -174,8 +172,10 @@ let gen_record ?and_ { Ot.r_name; r_fields } sc =
| Ot.Rft_required _ ->
Printf.eprintf "Only proto3 syntax supported in JSON encoding";
exit 1
| Ot.Rft_associative (assoc_type, _, (key_type, _), (value_type, _)) ->
gen_rft_assoc_field sc ~r_name ~rf_label ~assoc_type ~key_type ~value_type)
| Ot.Rft_associative
(assoc_type, _, (key_type, _), (value_type, _)) ->
gen_rft_assoc_field sc ~r_name ~rf_label ~assoc_type ~key_type
~value_type)
r_fields;

(* Unknown fields are simply ignored *)
Expand Down
64 changes: 28 additions & 36 deletions src/compilerlib/pb_codegen_encode_yojson.ml
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,8 @@ let gen_rft_variant sc rf_label { Ot.v_constructors; _ } =

F.linep sc "in (* match v.%s *)" rf_label

let gen_rft_assoc
sc
~rf_label
~assoc_type
~key_type
~value_field:(value_type, value_pk)
=
let gen_rft_assoc sc ~rf_label ~assoc_type ~key_type
~value_field:(value_type, value_pk) =
let var_name = sp "v.%s" rf_label in
let json_label = Pb_codegen_util.camel_case_of_label rf_label in
let key_pat, key_exp =
Expand All @@ -176,44 +171,41 @@ let gen_rft_assoc
let write_assoc_field ~fn ~var_name =
F.line sc "let assoc_field =";
F.sub_scope sc (fun sc ->
F.linep sc "%s" var_name;
(match assoc_type with
| Ot.At_list -> ()
| Ot.At_hashtable ->
F.line sc "|> Hashtbl.to_seq |> List.of_seq");
F.linep sc "|> List.map (fun (%s, value) -> %s, %s value)" key_pat key_exp fn);
F.line sc "in";
F.linep sc "%s" var_name;
(match assoc_type with
| Ot.At_list -> ()
| Ot.At_hashtable -> F.line sc "|> Hashtbl.to_seq |> List.of_seq");
F.linep sc "|> List.map (fun (%s, value) -> %s, %s value)" key_pat
key_exp fn);
F.line sc "in"
in
F.line sc "let assoc =";
F.sub_scope sc (fun sc ->
(match value_type with
| Ot.Ft_unit -> unsupported json_label
| Ot.Ft_basic_type basic_type ->
let runtime_f, map_function =
runtime_function_for_basic_type json_label basic_type value_pk
in
(match map_function with
(match value_type with
| Ot.Ft_unit -> unsupported json_label
| Ot.Ft_basic_type basic_type ->
let runtime_f, map_function =
runtime_function_for_basic_type json_label basic_type value_pk
in
(match map_function with
| None -> write_assoc_field ~fn:("Pbrt_yojson." ^ runtime_f) ~var_name
| Some map_function ->
let fn =
Printf.sprintf
"(fun value -> value |> %s |> Pbrt_yojson.%s)"
map_function
runtime_f
Printf.sprintf "(fun value -> value |> %s |> Pbrt_yojson.%s)"
map_function runtime_f
in
write_assoc_field ~fn ~var_name)
(* TODO Wrapper: add similar case for Ft_wrapper_type *)
(* User defined *)
| Ot.Ft_user_defined_type udt ->
let fn =
let function_prefix = "encode_json" in
Pb_codegen_util.function_name_of_user_defined ~function_prefix udt
in
write_assoc_field ~fn ~var_name;
| _ -> unsupported json_label);
F.linep sc "(\"%s\", `Assoc assoc_field) :: assoc " json_label);
(* TODO Wrapper: add similar case for Ft_wrapper_type *)
(* User defined *)
| Ot.Ft_user_defined_type udt ->
let fn =
let function_prefix = "encode_json" in
Pb_codegen_util.function_name_of_user_defined ~function_prefix udt
in
write_assoc_field ~fn ~var_name
| _ -> unsupported json_label);
F.linep sc "(\"%s\", `Assoc assoc_field) :: assoc " json_label);
F.line sc "in"
;;

let gen_record ?and_ { Ot.r_name; r_fields } sc =
let rn = r_name in
Expand Down
1 change: 0 additions & 1 deletion src/compilerlib/pb_format_util.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
open Format


(* Not available in 4.03 *)
let pp_print_option ?(none = fun _ () -> ()) pp_v ppf = function
| None -> none ppf ()
Expand Down
4 changes: 2 additions & 2 deletions src/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

(env
(_ (flags :standard -warn-error -a+8 -w +a-4-40-41-42-44-48-70)))
(_
(flags :standard -warn-error -a+8 -w +a-4-40-41-42-44-48-70)))
12 changes: 2 additions & 10 deletions src/examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,5 @@
(action
(with-stdout-to
dune.inc.gen
(run
%{gen-dune}
build_server
calculator
example01
example03
example04
example05
file_server
orgchart))))
(run %{gen-dune} build_server calculator example01 example03 example04
example05 file_server orgchart))))
4 changes: 2 additions & 2 deletions src/ocaml-protoc/dune
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
(re_export pbrt)))

(documentation
(package ocaml-protoc)
(mld_files index))
(package ocaml-protoc)
(mld_files index))
1 change: 0 additions & 1 deletion src/runtime-services/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

(library
(name pbrt_services)
(public_name pbrt_services)
Expand Down
9 changes: 5 additions & 4 deletions src/runtime-yojson/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

(library
(public_name pbrt_yojson)
(wrapped false)
(libraries (re_export yojson) base64))
(public_name pbrt_yojson)
(wrapped false)
(libraries
(re_export yojson)
base64))
5 changes: 4 additions & 1 deletion src/runtime/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
(name pbrt)
(public_name pbrt)
(synopsis "Runtime library for ocaml-protoc")
(foreign_stubs (language c) (flags :standard -std=c99 -O2) (names stubs))
(foreign_stubs
(language c)
(flags :standard -std=c99 -O2)
(names stubs))
; we need to increase -inline, so that the varint encoder/decoder can
; be remembered by the inliner.
(ocamlopt_flags :standard -inline 100))
3 changes: 1 addition & 2 deletions src/tests/benchmark/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@

(executable
(name benchmark_single_ml)
(modules benchmark_single_ml ocaml_test_types ocaml_test_runner
benchmark)
(modules benchmark_single_ml ocaml_test_types ocaml_test_runner benchmark)
(libraries pbrt unix))
2 changes: 1 addition & 1 deletion src/tests/expectation/tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ let test_cases =

rpc GetShelfWithSemicolon (GetShelfRequest) returns (GetShelfResponse) {};
}
|}
|};
]

let () = List.iter run test_cases
26 changes: 15 additions & 11 deletions src/tests/google_unittest/dune
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
(test
(name google_unittest)
(flags :standard -w -11)
(package ocaml-protoc)
(libraries pbrt))
(name google_unittest)
(flags :standard -w -11)
(package ocaml-protoc)
(libraries pbrt))

(rule
(targets unittest.ml unittest.mli)
(deps (:file unittest.proto) unittest_import.proto)
(action (run ocaml-protoc --ml_out . -I . %{file})))
(targets unittest.ml unittest.mli)
(deps
(:file unittest.proto)
unittest_import.proto)
(action
(run ocaml-protoc --ml_out . -I . %{file})))

(rule
(targets unittest_import.ml unittest_import.mli)
(deps (:file unittest_import.proto))
(action (run ocaml-protoc --ml_out . -I . %{file})))

(targets unittest_import.ml unittest_import.mli)
(deps
(:file unittest_import.proto))
(action
(run ocaml-protoc --ml_out . -I . %{file})))
Loading