Skip to content

Commit a93905a

Browse files
authored
format check in ci (#237)
format + check for formatting in CI
1 parent 8e9360f commit a93905a

File tree

21 files changed

+146
-152
lines changed

21 files changed

+146
-152
lines changed

.github/workflows/main.yml

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

benchs/bin/dune

Lines changed: 0 additions & 1 deletion
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

Lines changed: 4 additions & 1 deletion
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
Lines changed: 0 additions & 1 deletion
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

Lines changed: 41 additions & 41 deletions
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_yojson.ml

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,8 @@ let gen_rft_variant sc rf_label { Ot.v_constructors; _ } =
148148

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

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

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

src/compilerlib/pb_format_util.ml

Lines changed: 0 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,5 @@
107107
(action
108108
(with-stdout-to
109109
dune.inc.gen
110-
(run
111-
%{gen-dune}
112-
build_server
113-
calculator
114-
example01
115-
example03
116-
example04
117-
example05
118-
file_server
119-
orgchart))))
110+
(run %{gen-dune} build_server calculator example01 example03 example04
111+
example05 file_server orgchart))))

src/ocaml-protoc/dune

Lines changed: 2 additions & 2 deletions
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))

0 commit comments

Comments
 (0)