Skip to content

Commit 7f7086d

Browse files
c-cubeLupus
authored andcommitted
format check in ci (mransan#237)
format + check for formatting in CI
1 parent ed08a32 commit 7f7086d

File tree

22 files changed

+147
-154
lines changed

22 files changed

+147
-154
lines changed

.github/workflows/main.yml

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ jobs:
1515
#- macos-latest
1616
#- windows-latest
1717
ocaml-compiler:
18-
- 4.08.x
19-
- 4.12.x
18+
- '4.08'
19+
- '4.14'
20+
- '5.1'
2021
runs-on: ${{ matrix.os }}
2122
steps:
2223
- uses: actions/checkout@v2
@@ -25,10 +26,15 @@ jobs:
2526
- uses: ocaml/setup-ocaml@v2
2627
with:
2728
ocaml-compiler: ${{ matrix.ocaml-compiler }}
29+
allow-prerelease-opam: true
30+
dune-cache: true
2831
#- run: sudo apt install protobuf-compiler libprotobuf-dev
2932
- run: opam pin -n .
3033
- run: opam depext -yt ocaml-protoc
3134
- run: opam install -t . --deps-only
3235
- run: opam exec -- dune build @install
3336
- run: opam exec -- dune runtest
3437
#- run: opam exec -- make integration
38+
- run: opam install ocamlformat.0.24.1
39+
- run: opam exec -- dune build @fmt --auto-promote
40+
- run: git diff -q

benchs/bin/dune

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
(executable
32
(name run)
43
(ocamlopt_flags :standard -inline 100)

benchs/dune

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
(executable
22
(name benchs)
33
(ocamlopt_flags :standard -inline 100)
4-
(foreign_stubs (language c) (flags :standard -std=c99 -O2) (names stubs))
4+
(foreign_stubs
5+
(language c)
6+
(flags :standard -std=c99 -O2)
7+
(names stubs))
58
(libraries ocaml-protoc benchmark))
69

710
(rule
-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
include Pb_codegen_plugin.S
32

43
val plugin : Pb_codegen_plugin.t

src/compilerlib/pb_codegen_decode_yojson.ml

+41-41
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let field_pattern_match ~r_name ~rf_label field_type =
2525
| Ot.Bt_bool -> decode "bool"
2626
| Ot.Bt_bytes -> decode "bytes"
2727
in
28-
("json_value", exp)
28+
"json_value", exp
2929
| Ot.Ft_unit ->
3030
"json_value", sp "Pbrt_yojson.unit json_value \"%s\" \"%s\"" r_name rf_label
3131
(* TODO Wrapper: add similar one for wrapper type (with different
@@ -103,44 +103,42 @@ let gen_rft_assoc_field sc ~r_name ~rf_label ~assoc_type ~key_type ~value_type =
103103
let json_label = Pb_codegen_util.camel_case_of_label rf_label in
104104
F.linep sc "| (\"%s\", `Assoc assoc) ->" json_label;
105105
F.sub_scope sc (fun sc ->
106-
let value_name, value_exp = field_pattern_match ~r_name ~rf_label value_type in
107-
let key_name = "key" in
108-
let key_exp =
109-
match key_type with
110-
| Ot.Bt_string -> "key"
111-
| Ot.Bt_int -> "(Int.of_string key)"
112-
| Ot.Bt_int32 -> "(Int32.of_string key)"
113-
| Ot.Bt_int64 -> "(Int64.of_string key)"
114-
| Ot.Bt_uint32 -> "(`unsigned (Int32.of_string key))"
115-
| Ot.Bt_uint64 -> "(`unsigned (Int64.of_string key))"
116-
| Ot.Bt_bool -> "(Bool.of_string key)"
117-
| Ot.Bt_float ->
118-
Printf.eprintf "float cannot be used as a map key type";
119-
exit 1
120-
| Ot.Bt_bytes ->
121-
Printf.eprintf "bytes cannot be used as a map key type";
122-
exit 1
123-
in
124-
F.line sc "let assoc =";
125-
F.sub_scope sc (fun sc ->
126-
F.line sc "assoc";
127-
F.linep sc "|> List.map (fun (%s, %s) -> (%s, %s)) "
128-
key_name
129-
value_name
130-
key_exp
131-
value_exp;
132-
F.line sc "|> List.to_seq";
133-
(* Passing through [Hashtbl.of_seq] even in the [At_list] case ensures that if there
134-
is a repeated key we take the last value associated with it. *)
135-
F.line sc "|> Hashtbl.of_seq");
136-
F.line sc "in";
137-
let assoc_exp =
138-
match assoc_type with
139-
| Ot.At_hashtable -> "assoc"
140-
| Ot.At_list -> "assoc |> Hashtbl.to_seq |> List.of_seq"
141-
in
142-
F.linep sc "v.%s <- %s" rf_label assoc_exp);
143-
;;
106+
let value_name, value_exp =
107+
field_pattern_match ~r_name ~rf_label value_type
108+
in
109+
let key_name = "key" in
110+
let key_exp =
111+
match key_type with
112+
| Ot.Bt_string -> "key"
113+
| Ot.Bt_int -> "(Int.of_string key)"
114+
| Ot.Bt_int32 -> "(Int32.of_string key)"
115+
| Ot.Bt_int64 -> "(Int64.of_string key)"
116+
| Ot.Bt_uint32 -> "(`unsigned (Int32.of_string key))"
117+
| Ot.Bt_uint64 -> "(`unsigned (Int64.of_string key))"
118+
| Ot.Bt_bool -> "(Bool.of_string key)"
119+
| Ot.Bt_float ->
120+
Printf.eprintf "float cannot be used as a map key type";
121+
exit 1
122+
| Ot.Bt_bytes ->
123+
Printf.eprintf "bytes cannot be used as a map key type";
124+
exit 1
125+
in
126+
F.line sc "let assoc =";
127+
F.sub_scope sc (fun sc ->
128+
F.line sc "assoc";
129+
F.linep sc "|> List.map (fun (%s, %s) -> (%s, %s)) " key_name
130+
value_name key_exp value_exp;
131+
F.line sc "|> List.to_seq";
132+
(* Passing through [Hashtbl.of_seq] even in the [At_list] case ensures that if there
133+
is a repeated key we take the last value associated with it. *)
134+
F.line sc "|> Hashtbl.of_seq");
135+
F.line sc "in";
136+
let assoc_exp =
137+
match assoc_type with
138+
| Ot.At_hashtable -> "assoc"
139+
| Ot.At_list -> "assoc |> Hashtbl.to_seq |> List.of_seq"
140+
in
141+
F.linep sc "v.%s <- %s" rf_label assoc_exp)
144142

145143
(* Generate decode function for a record *)
146144
let gen_record ?and_ { Ot.r_name; r_fields } sc =
@@ -174,8 +172,10 @@ let gen_record ?and_ { Ot.r_name; r_fields } sc =
174172
| Ot.Rft_required _ ->
175173
Printf.eprintf "Only proto3 syntax supported in JSON encoding";
176174
exit 1
177-
| Ot.Rft_associative (assoc_type, _, (key_type, _), (value_type, _)) ->
178-
gen_rft_assoc_field sc ~r_name ~rf_label ~assoc_type ~key_type ~value_type)
175+
| Ot.Rft_associative
176+
(assoc_type, _, (key_type, _), (value_type, _)) ->
177+
gen_rft_assoc_field sc ~r_name ~rf_label ~assoc_type ~key_type
178+
~value_type)
179179
r_fields;
180180

181181
(* Unknown fields are simply ignored *)

src/compilerlib/pb_codegen_encode_binary.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ let gen_rft_variant sc var_name { Ot.v_constructors; _ } =
155155
gen_encode_field_type sc ~with_key:true "x" vc_encoding_number
156156
vc_payload_kind false field_type))
157157
v_constructors;
158-
F.line sc "| None -> ()";
158+
F.line sc "| None -> ()";
159159
F.line sc "end;"
160160

161161
let gen_rft_associative sc var_name associative_field =

src/compilerlib/pb_codegen_encode_yojson.ml

+28-36
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,8 @@ let gen_rft_variant sc rf_label { Ot.v_constructors; _ } =
149149

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

152-
let gen_rft_assoc
153-
sc
154-
~rf_label
155-
~assoc_type
156-
~key_type
157-
~value_field:(value_type, value_pk)
158-
=
152+
let gen_rft_assoc sc ~rf_label ~assoc_type ~key_type
153+
~value_field:(value_type, value_pk) =
159154
let var_name = sp "v.%s" rf_label in
160155
let json_label = Pb_codegen_util.camel_case_of_label rf_label in
161156
let key_pat, key_exp =
@@ -177,44 +172,41 @@ let gen_rft_assoc
177172
let write_assoc_field ~fn ~var_name =
178173
F.line sc "let assoc_field =";
179174
F.sub_scope sc (fun sc ->
180-
F.linep sc "%s" var_name;
181-
(match assoc_type with
182-
| Ot.At_list -> ()
183-
| Ot.At_hashtable ->
184-
F.line sc "|> Hashtbl.to_seq |> List.of_seq");
185-
F.linep sc "|> List.map (fun (%s, value) -> %s, %s value)" key_pat key_exp fn);
186-
F.line sc "in";
175+
F.linep sc "%s" var_name;
176+
(match assoc_type with
177+
| Ot.At_list -> ()
178+
| Ot.At_hashtable -> F.line sc "|> Hashtbl.to_seq |> List.of_seq");
179+
F.linep sc "|> List.map (fun (%s, value) -> %s, %s value)" key_pat
180+
key_exp fn);
181+
F.line sc "in"
187182
in
188183
F.line sc "let assoc =";
189184
F.sub_scope sc (fun sc ->
190-
(match value_type with
191-
| Ot.Ft_unit -> unsupported json_label
192-
| Ot.Ft_basic_type basic_type ->
193-
let runtime_f, map_function =
194-
runtime_function_for_basic_type json_label basic_type value_pk
195-
in
196-
(match map_function with
185+
(match value_type with
186+
| Ot.Ft_unit -> unsupported json_label
187+
| Ot.Ft_basic_type basic_type ->
188+
let runtime_f, map_function =
189+
runtime_function_for_basic_type json_label basic_type value_pk
190+
in
191+
(match map_function with
197192
| None -> write_assoc_field ~fn:("Pbrt_yojson." ^ runtime_f) ~var_name
198193
| Some map_function ->
199194
let fn =
200-
Printf.sprintf
201-
"(fun value -> value |> %s |> Pbrt_yojson.%s)"
202-
map_function
203-
runtime_f
195+
Printf.sprintf "(fun value -> value |> %s |> Pbrt_yojson.%s)"
196+
map_function runtime_f
204197
in
205198
write_assoc_field ~fn ~var_name)
206-
(* TODO Wrapper: add similar case for Ft_wrapper_type *)
207-
(* User defined *)
208-
| Ot.Ft_user_defined_type udt ->
209-
let fn =
210-
let function_prefix = "encode_json" in
211-
Pb_codegen_util.function_name_of_user_defined ~function_prefix udt
212-
in
213-
write_assoc_field ~fn ~var_name;
214-
| _ -> unsupported json_label);
215-
F.linep sc "(\"%s\", `Assoc assoc_field) :: assoc " json_label);
199+
(* TODO Wrapper: add similar case for Ft_wrapper_type *)
200+
(* User defined *)
201+
| Ot.Ft_user_defined_type udt ->
202+
let fn =
203+
let function_prefix = "encode_json" in
204+
Pb_codegen_util.function_name_of_user_defined ~function_prefix udt
205+
in
206+
write_assoc_field ~fn ~var_name
207+
| _ -> unsupported json_label);
208+
F.linep sc "(\"%s\", `Assoc assoc_field) :: assoc " json_label);
216209
F.line sc "in"
217-
;;
218210

219211
let gen_record ?and_ { Ot.r_name; r_fields } sc =
220212
let rn = r_name in

src/compilerlib/pb_format_util.ml

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
open Format
22

3-
43
(* Not available in 4.03 *)
54
let pp_print_option ?(none = fun _ () -> ()) pp_v ppf = function
65
| None -> none ppf ()

src/dune

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
21
(env
3-
(_ (flags :standard -warn-error -a+8 -w +a-4-40-41-42-44-48-70)))
2+
(_
3+
(flags :standard -warn-error -a+8 -w +a-4-40-41-42-44-48-70)))

src/examples/dune

+2-11
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,5 @@
119119
(action
120120
(with-stdout-to
121121
dune.inc.gen
122-
(run
123-
%{gen-dune}
124-
build_server
125-
calculator
126-
example01
127-
example03
128-
example04
129-
example05
130-
file_server
131-
orgchart
132-
oneof))))
122+
(run %{gen-dune} build_server calculator example01 example03 example04
123+
example05 file_server orgchart oneof))))

src/ocaml-protoc/dune

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
(re_export pbrt)))
1717

1818
(documentation
19-
(package ocaml-protoc)
20-
(mld_files index))
19+
(package ocaml-protoc)
20+
(mld_files index))

src/runtime-services/dune

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
(library
32
(name pbrt_services)
43
(public_name pbrt_services)

src/runtime-yojson/dune

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
21
(library
3-
(public_name pbrt_yojson)
4-
(wrapped false)
5-
(libraries (re_export yojson) base64))
2+
(public_name pbrt_yojson)
3+
(wrapped false)
4+
(libraries
5+
(re_export yojson)
6+
base64))

src/runtime/dune

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
(name pbrt)
33
(public_name pbrt)
44
(synopsis "Runtime library for ocaml-protoc")
5-
(foreign_stubs (language c) (flags :standard -std=c99 -O2) (names stubs))
5+
(foreign_stubs
6+
(language c)
7+
(flags :standard -std=c99 -O2)
8+
(names stubs))
69
; we need to increase -inline, so that the varint encoder/decoder can
710
; be remembered by the inliner.
811
(ocamlopt_flags :standard -inline 100))

src/tests/benchmark/dune

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66

77
(executable
88
(name benchmark_single_ml)
9-
(modules benchmark_single_ml ocaml_test_types ocaml_test_runner
10-
benchmark)
9+
(modules benchmark_single_ml ocaml_test_types ocaml_test_runner benchmark)
1110
(libraries pbrt unix))

src/tests/expectation/tests.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ let test_cases =
225225

226226
rpc GetShelfWithSemicolon (GetShelfRequest) returns (GetShelfResponse) {};
227227
}
228-
|}
228+
|};
229229
]
230230

231231
let () = List.iter run test_cases

src/tests/google_unittest/dune

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
(test
2-
(name google_unittest)
3-
(flags :standard -w -11)
4-
(package ocaml-protoc)
5-
(libraries pbrt))
2+
(name google_unittest)
3+
(flags :standard -w -11)
4+
(package ocaml-protoc)
5+
(libraries pbrt))
66

77
(rule
8-
(targets unittest.ml unittest.mli)
9-
(deps (:file unittest.proto) unittest_import.proto)
10-
(action (run ocaml-protoc --ml_out . -I . %{file})))
8+
(targets unittest.ml unittest.mli)
9+
(deps
10+
(:file unittest.proto)
11+
unittest_import.proto)
12+
(action
13+
(run ocaml-protoc --ml_out . -I . %{file})))
1114

1215
(rule
13-
(targets unittest_import.ml unittest_import.mli)
14-
(deps (:file unittest_import.proto))
15-
(action (run ocaml-protoc --ml_out . -I . %{file})))
16-
16+
(targets unittest_import.ml unittest_import.mli)
17+
(deps
18+
(:file unittest_import.proto))
19+
(action
20+
(run ocaml-protoc --ml_out . -I . %{file})))

0 commit comments

Comments
 (0)